실행중 생성되어 동적으로 변하는 필드의 적용방법입니다.
동적인 필드 변경은 AutoGenerationColumn 속성을 true로 하여 바인딩처리 하기 때문에
일반적인 디자인 방법으로 처리할수 없습니다.
그럴땐 Custom된 control 방식으로 처리할수 있습니다. (제가 아는 방법으로는요 ^^;;;)
일단 GridView 기준으로 이녀석의 커스터마이징을 합니다.
아래 소스예제는 모두 MSDN 의 녀석을 카피하여 수정한것입니다.
(ms-help://MS.VSCC.v80/MS.MSDN.v80/MS.NETDEVFX.v20.ko/cpref16/html/T_System_Web_UI_WebControls_AutoGeneratedField.htm)
public class CustomGridView : GridView
{
protected override AutoGeneratedField CreateAutoGeneratedColumn(AutoGeneratedFieldProperties fieldProperties)
{
// Create an AutoGeneratedField object.
AutoGeneratedField field = new AutoGeneratedField(fieldProperties.DataField);
// Set the properties of the AutoGeneratedField using
// the values from the AutoGeneratedFieldProperties
// object contained in the fieldProperties parameter.
((IStateManager)field).TrackViewState();
field.HeaderText = fieldProperties.Name;
field.SortExpression = fieldProperties.Name;
field.ItemStyle.Width = Unit.Pixel(100); // 제가 추가한 부분...
field.ReadOnly = fieldProperties.IsReadOnly;
field.DataType = fieldProperties.Type;
return field;
}
}
그리고 해당 페이지로 가서
CustomGridView my = new CustomGridView();
my.AutoGenerateColumns = true;
DataTable example = MakeDataTable(); // 이부분은 테스트를 위해 제가 만든 부분입니다. :D
my.DataSource = example;
my.DataBind();
form1.Controls.Add(my);
이런식으로 사용합니다.
실행하면 자동 생성되는 필드의 폭이 변했겠죠??
이걸 응용하시면 자유롭게 처리가 가능하실겁니다.
첫댓글 Custom된 Control 방식에 대해서 처음 알게되었네요.. 정말로 감사드립니다. 열심히 해봐야죠 ㅎㅎ