The following method is used by me to send all rows from a data grid view in a form(datagridview1 of form1) to another data grid view of another form(datagridview1 of form2) when a button is clicked.
private void button2_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2();
DataTable dt1 = new DataTable();
f2.dataGridView1.DataSource = dt1;
foreach (DataGridView row in dataGridView1.Rows)
{
int n = f2.dataGridView1.Rows.Add();
foreach (DataGridViewColumn col in dataGridView1.Columns)
{
f2.dataGridView1.Rows[n].Cells[col.Index].Value = dataGridView1.Rows[row.Index].Cells[col.Index].Value.ToString();
}
}
}
But no data is sent to datagridview1 of form2! how can i correct this?
Depending on the circumstances, I'd work with a data source, ie this way
VB.NET
Dim dt as New DataTable
dt = ds.Tables(0)
Me.DataGridView1.datasource = dt
Form2.DataGridView2.datasource = dt
C#
DataTable dt = new DataTable();
dt = ds.Tables(0);
this.DataGridView1.datasource = dt;
Form2.DataGridView2.datasource = dt;
If you wanted to modify one of them independently, you need a second datatatable:
Dim dt2 as New DataTable
dt2 = dt.Copy() ' copies datatable structure AND the data
Form2.DataGridView2.datasource = dt
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.