我只想将数据从另一个表单传递给DataGridView?如何从其他表单传递数据到DataGridView?
我有2点窗口的形式:
form1
包含DataGridView1
和button_frm1
。DataGridView
有3列,已经有一些数据(6行),DataGridView1
modifiers = Public。form2
包含textBox1
和button_frm2
。现在
,当我点击button_frm1
窗口2出现,接下来当我点击button_frm2
在文本框中的值应该选择行插入DataGridView1
在column0。但是,相反,我得到这个错误:
Index was out of range. Must be non-negative and less than the size of the collection.
请帮助我如何从Form2的文本框中的值插入DataGridView1在Form1。遵循什么步骤? 非常感谢您提前。
这里是我尝试的代码:
Form1中:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button_frm1_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2();
frm2.Show();
}
}
窗体2:
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void button_frm2(object sender, EventArgs e)
{
Form1 frm1 = new Form1();
textBox1.Text= frm1.dataGridView1.SelectedRows[0].Cells[0].Value.ToString();
}
}
如何填充您的DataGridView? – StepUp
@naouf你试过我的方法吗?我理解正确吗? – StepUp
嗨StepUp。谢谢您的回复。我仍然没有尝试它,因为我是c#的新手,你的代码对我来说是新的东西,所以我仍然试图理解它。但是,一旦我尝试它,我会让你知道。你能告诉我在c#的哪个区域应该搜索来理解你的代码吗?谢谢。 – naouf