c#.net에서 그리드뷰(GridView)의 필드를 동적으로 생성하는 방법은?
/*********************************************************/
// aspx
/*********************************************************/
<div id="divList" style="overflow-x: scroll; width: 100%;">
<asp:GridView ID="gvList" runat="server" AutoGenerateColumns="false" CssClass="table_content" Width="100%" AllowPaging="false"
ShowHeaderWhenEmpty="true" DataKeyNames="ProjectDevelopmentId">
<Columns>
</Columns>
<HeaderStyle CssClass="cell_center" />
<RowStyle />
<EmptyDataRowStyle CssClass="data_AC row25" />
<EmptyDataTemplate>
데이터가 존재하지 않습니다.
</EmptyDataTemplate>
</asp:GridView>
</div>
/*********************************************************/
// aspx.cs
/*********************************************************/
DataTable dt = new DataTable();
#region 그리드 칼럼 정의
DataColumn dcol = new DataColumn("ProjectDevelopmentId", typeof(System.String));
dt.Columns.Add(dcol);
dcol = new DataColumn("개발자", typeof(System.String));
dt.Columns.Add(dcol);
// dcol = new DataColumn("1월", typeof(System.String));
// dt.Columns.Add(dcol);
DataColumn dcoWeek = new DataColumn("1주", typeof(System.String));
dt.Columns.Add(dcoWeek);
dcoWeek = new DataColumn("2주", typeof(System.String));
dt.Columns.Add(dcoWeek);
dcoWeek = new DataColumn("3주", typeof(System.String));
dt.Columns.Add(dcoWeek);
dcoWeek = new DataColumn("4주", typeof(System.String));
dt.Columns.Add(dcoWeek);
#endregion
#region 그리드 데이터 Row 정의
for (int nIndex = 0; nIndex < 10; nIndex++)
{
//Create a new row
DataRow drow = dt.NewRow();
//Initialize the row data.
drow["ProjectDevelopmentId"] = "Row-" + Convert.ToString((nIndex + 1));
drow["개발자"] = "Row-" + Convert.ToString((nIndex + 1));
//Add the row to the datatable.
dt.Rows.Add(drow);
}
#endregion
//Iterate through the columns of the datatable to set the data bound field dynamically.
foreach (DataColumn col in dt.Columns)
{
//Declare the bound field and allocate memory for the bound field.
BoundField bfield = new BoundField();
//Initalize the DataField value.
bfield.DataField = col.ColumnName;
//Initialize the HeaderText field value.
bfield.HeaderText = col.ColumnName;
//Add the newly created bound field to the GridView.
gvList.Columns.Add(bfield);
}
//Initialize the DataSource
gvList.DataSource = dt;
//Bind the datatable with the GridView.
gvList.DataBind();
'IT > C#' 카테고리의 다른 글
로딩바 화면 구현 (0) | 2017.01.17 |
---|---|
csv 만들기 (0) | 2016.12.27 |
팀파운데이션 매핑 제거 (0) | 2015.06.19 |
Managedpipelinehandler error (0) | 2015.01.20 |
UserControl 사용시 PostBack 일어날때 대처 (0) | 2015.01.07 |