这样的窗体传值不执行吗?
form1:
private void button1_Click(object sender, EventArgs e)
{
form2 fr = new form2();
fr.ShowDialog();
}
form2:
private void btCheck_Click(object sender, EventArgs e)
{
form1 frm = new form1();
foreach (DataGridViewRow row in frm.dataGridView1.Rows)//不执行?
{
MessageBox.Show( "重复...... ");
return;
}
}
为什么在form2里不执行form1.dataGridView1.Rows??
------解决方案--------------------当然不行!
你应把Form1变量引用传到Form2中,而不能在Form2中又New出一个新的窗体变量.
最简单的你可以这样的来做:
form1:
private void button1_Click(object sender, EventArgs e)
{
form2 fr = new form2();
fr.Form1 = this;
fr.ShowDialog();
}
form2:
private Form1 m_Form1;
public Form1 Form1
{
set
{
this.m_Form1 = value;
}
}
private void btCheck_Click(object sender, EventArgs e)
{
foreach (DataGridViewRow row in this.m_Form1.dataGridView1.Rows)//不执行?
{
MessageBox.Show( "重复...... ");
return;
}
}
------解决方案--------------------form1:
private void button1_Click(object sender, EventArgs e)
{
form2 fr = new form2();
fr.showDialog(this);
}
form2:
private void btCheck_Click(object sender, EventArgs e)
{
form1 frm = (form1)Owner;
foreach (DataGridViewRow row in frm.dataGridView1.Rows)//不执行?
{
MessageBox.Show( "重复...... ");
return;
}
}
------解决方案--------------------form1 frm = new form1();
改为:
form1 frm = (form1)this.Owner;