datagridview dgv绑定到datatable dt。
dgv_copy是新的datagridview.dgv_copy是使用row.clone向dgv添加行。
my code is
if (dgv_copy.Columns.Count == 0)
{
foreach (DataGridViewColumn dgvc in dgv_org.Columns)
{
dgv_copy.Columns.Add(dgvc.Clone() as DataGridViewColumn);
}
}
DataGridViewRow row = new DataGridViewRow();
for (int i = 0; i < dgv_org.Rows.Count; i++)
{
row = (DataGridViewRow)dgv_org.Rows[i].Clone();
int intColIndex = 0;
foreach (DataGridViewCell cell in dgv_org.Rows[i].Cells)
{
row.Cells[intColIndex].Value = cell.Value;
intColIndex++;
}
dgv_copy.Rows.Add(row);
}
dgv_copy.AllowUserToAddRows = false;
dgv_copy.Refresh();
来源是https://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewrow.clone.aspx
答案 0 :(得分:0)
很确定你可以做dgv_copy.rows.add(dgv_org.Rows [i] .ItemArray)
答案 1 :(得分:0)
由于原始DataGridView
与DataTable
绑定,您可以创建DataTable
的副本并将其发送给第二个DataGridView
。
var original = (DataTable)originalDataGridView.DataSource;
var copy = original.Copy();
copyDataGridView.DataSource = copy;