首页 > 代码库 > datagridview 绑定 datatable

datagridview 绑定 datatable

if (dt != null)
{
DataGridViewColumn dgvc = null;
this.dataGridView1.DataSource = null;
this.dataGridView1.Columns.Clear();
foreach (DataColumn dc in dt.Columns)
{
dgvc = new DataGridViewColumn();
dgvc.Name = dc.ColumnName;
dgvc.HeaderText = dc.ColumnName;
dgvc.DataPropertyName = dc.ColumnName;
dgvc.CellTemplate = new DataGridViewTextBoxCell();
dgvc.Resizable = DataGridViewTriState.True;
dgvc.SortMode = DataGridViewColumnSortMode.Automatic;
this.dataGridView1.Columns.Add(dgvc);
}
this.dataGridView1.AutoGenerateColumns = false;
this.dataGridView1.DataSource = dt;

}