问题:
有两个窗体Form1(父窗体)和Form2(子窗体),
其中Form1(父窗体)中有个DataTable对象(dt1),
当我在子窗体中通过Form2中代码访问父窗体的dt1对象时,
提示错误为:未将对象引用设置到对象的实例Form1(父窗体)代码:
public DataTable dt1=null;
dt1=InitDatable();public DataTable InitDatable()
{
DataTable dt = new DataTable();
DataColumn column;
DataRow row;
...//以下代码省略 return dt;
}
--------------------------------------
Form2(子窗体)代码:
Form1 parentForm=(Form1)this.Owner;DataTable dt=parentForm.dt1//这句提示未将对象引用设置到对象的实例请各位帮忙,谢谢!
有两个窗体Form1(父窗体)和Form2(子窗体),
其中Form1(父窗体)中有个DataTable对象(dt1),
当我在子窗体中通过Form2中代码访问父窗体的dt1对象时,
提示错误为:未将对象引用设置到对象的实例Form1(父窗体)代码:
public DataTable dt1=null;
dt1=InitDatable();public DataTable InitDatable()
{
DataTable dt = new DataTable();
DataColumn column;
DataRow row;
...//以下代码省略 return dt;
}
--------------------------------------
Form2(子窗体)代码:
Form1 parentForm=(Form1)this.Owner;DataTable dt=parentForm.dt1//这句提示未将对象引用设置到对象的实例请各位帮忙,谢谢!
结果出现以上问题,还请帮忙看看以上这句代码问题出在哪儿?DataTable dt=parentForm.dt1//这句提示未将对象引用设置到对象的实例
Form2 f2 = new Form2();
f2.Owner = this;
f2.Show();
谢谢回复!我显示FORM2的时候是如下代码:
Form2 f2 = new Form2();
F2.ShowDialog(this) 与您给出代码的功能是一样的,不知哪里出问题
这种方法肯定是正确的,看看哪儿有没有写错。
调试看看是this.Owner是什么?
在InitDatable()中的列和行没有实例化吧
这句代码我原先是放在FORM2窗体的构造函数里,
public Form2()
{
InitializeComponent();
Form1 f1=(Form1)this.Owner;
}
最后把位置调整到FORM2_Load(object sender, EventArgs e)事件里就可以了。