这是将 DataTable 复制到 DataGridView 中的方法,该方法不起作用,该方法只能在 DataGridView 中添加列和空行。任何人都可以在不使用 DataGridView 的 DataSource 属性的情况下建议我解决这个问题吗?
public void CopyDataTableInDataGridView(DataTable table, DataGridView gdv)
{
if (gdv.Rows.Count > 0)
gdv.Rows.Clear();
if (table != null && table.Rows.Count > 0)
{
foreach (DataColumn _colm in table.Columns)
{
DataGridViewColumn _col;
if (_colm.GetType() == typeof(bool))
_col = new DataGridViewCheckBoxColumn();
else
_col = new DataGridViewTextBoxColumn();
_col.Name = _colm.ColumnName;
_col.HeaderText = string.Concat(_colm.ColumnName.Select(x => Char.IsUpper(x) ? " " + x : x.ToString())).TrimStart(' ');
gdv.Columns.Add(_col);
}
foreach (DataRow _row in table.Select())
{
//Rows getting added in dgv but not data
// By adding following line in Code my problem get solved
//object[] _items = _row.ItemArray;
gdv.Rows.Add(_row);
}
}
}