這裏是方法,複製數據表到未不工作的DataGridView,此方法只可以添加列和中的DataGridView空行。任何人可以建議我解決這個問題,而不使用DataGridView的DataSource屬性?我可以在DataGridView中添加DataTable中不使用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);
}
}
}
你爲什麼不*想要使用'DataSource'屬性? – gunr2171
@ gunr2171因爲我想在DataGridView上做一些操作,但是DataSource限制我這麼做.. –
不太確定你爲什麼不想使用'DataSource'屬性。目前您正在添加每行的「ToString」實現,而應該爲每行添加一行數據。你可以看到在[這個問題](http://stackoverflow.com/questions/10063770/c-sharp-how-to-add-a-new-row-to-datagridview-programmatically/10063825#10063825)添加行在網格視圖中通過代碼 – Habib