我正在从DataTable中填充DataGridView,但是遇到以下错误:
至少有一列DataGridView控件没有单元格模板
private void PopulateGridView(DataTable dt)
{
if (dt != null)
{
dataGridView1.Columns.Clear();
dataGridView1.DataSource = null;
foreach (DataColumn col in dt.Columns)
{
//Declare the bound field and allocate memory for the bound field.
DataGridViewColumn datacolumn = new DataGridViewColumn();
//Initalize the DataField value.
datacolumn.DataPropertyName = col.ColumnName;
datacolumn.Name = col.ColumnName;
//Initialize the HeaderText field value.
datacolumn.HeaderText = col.ColumnName;
//Add the newly created bound field to the GridView.
this.dataGridView1.Columns.Add(datacolumn); // ** Error **
}
//Initialize the DataSource
this.dataGridView1.DataSource = dt;
}