【发布时间】:2017-08-23 09:35:00
【问题描述】:
当单击按钮时,我使用以下方法将所有行从表单中的数据网格视图(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!我该如何纠正这个问题?
【问题讨论】:
-
为什么不直接从一个数据源复制到另一个数据源?
-
@BugFinder 我该怎么做?
-
因为你没有打电话给
f2.Show()我会粗略地假设你有错误的实例。单击button2按钮时,您的Form2 是否已经打开? -
@MongZhu not form2 目前已关闭
-
@MongZhu 在我放 f2.show() 后出现错误“int n = f2.dataGridView1.Rows.Add();”行
标签: c# button datagridview click