Reputation: 21
I want to transfer data in one row from datagridview1
to datagridview2
, but when I run this code it show like this
(Additional information: No row can be added to a DataGridView control that does not have columns. Columns must be added first).
foreach (DataGridViewRow dgvr in dataGridView1.SelectedRows)
{
DataGridViewRow r = dgvr.Clone() as DataGridViewRow;
foreach (DataGridViewCell cell in dgvr.Cells)
{
r.Cells[cell.ColumnIndex].Value = cell.Value;
}
dataGridView1.Rows.Remove(dgvr);
dataGridView2.Rows.Add(r);
}
Upvotes: 2
Views: 14600
Reputation: 15923
You are getting this error because you have to add columns before inserting the rows into the GridView. Use table.Columns.Add()
before foreach
loop with your columns as its parameters .
Upvotes: 3