Hello World

C#(WINFORM, WINCE, WINDOWS MOBILE) 실전 활용 예제소스 및 프로그램 개발

[C#/PDA]DataGrid 컬럼 추가 예제

댓글 0

C#

2016. 2. 11.


DataTable dt = new DataTable();

dt.Columns.Add(new DataColumn("COL1"));
dt.Columns.Add(new DataColumn("COL2"));

DataGridTableStyle gridHead = new DataGridTableStyle();
DataGridColumnStyle headColumn = null;

headColumn = new DataGridTextBoxColumn();
headColumn.HeaderText = "컬럼1";
headColumn.MappingName = "COL1";
headColumn.NullText = "";
headColumn.Width = 100;
gridHead.GridColumnStyles.Add(headColumn);

headColumn = new DataGridTextBoxColumn();
headColumn.HeaderText = "컬럼2";
headColumn.MappingName = "COL2";
headColumn.NullText = "";
headColumn.Width = 120;
gridHead.GridColumnStyles.Add(headColumn);

dataGrid1.TableStyles.Add(gridHead);

for (int i = 0; i < array.Count; i++)
{
    DataRow dr = dt.NewRow();

    dr[0] = array[i][0].Tostring();

    dr[1] = array[i][1].Tostring();

    dt.Rows.Add(dr);
}

dataGrid1.DataSource = dt;