[英]sending data from 1 data grid view to another data grid view in another form c#
我使用以下方法,以在單擊按鈕時將所有行從表單(form1的datagridview1)中的數據網格視圖發送到另一表單(form2的datagridview1)的另一個數據網格視圖。
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();
}
}
}
但是沒有數據發送到form2的datagridview1! 我該如何糾正?
根據情況,我將使用數據源,即這種方式
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;
如果要獨立修改其中之一,則需要第二個數據:
Dim dt2 as New DataTable
dt2 = dt.Copy() ' copies datatable structure AND the data
Form2.DataGridView2.datasource = dt
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.