从表单中的文本框添加到另一种形式的数据视图C#

时间:2015-10-31 08:17:39

标签: c# datagridview

我有1个带有4个文本框和1个按钮的form1,我有form2,其中包含数据集中4列填充的dataGridView,我想从另一个表单向datagridview添加新行。

但它给了我这个错误:

Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index

这是我点击按钮上的代码。

Main frm = new Main();
frm.dataGridView1.Rows[0].Cells[0].Value = textBox1.Text;
frm.dataGridView1.Rows[0].Cells[1].Value = textBox2.Text;
frm.dataGridView1.Rows[0].Cells[2].Value = textBox4.Text;
frm.dataGridView1.Rows[0].Cells[3].Value = dateTimePicker1.Value;
frm.dataGridView1.Rows.Add();
this.Hide();
frm.Show();

0 个答案:

没有答案