从另一个表单添加datagridview列 [英] Add a datagridview column from another form
问题描述
Form2
private void button1_Click(object sender,EventArgs e)
{
Form1 form1 = new Form1();
form1.dataGridView1.Columns.Add(test,test);
}
我该怎么做?
Form1 form1 = new Form1();
这将无法正常工作,因为您的真实form1已经存在,我按下。而是在form2中创建对它的引用,并将其加载到form2的构造函数中!
以下是步骤:
- form2变量中form1的本地引用:
Form1 form1
-
打开form2传递在构造函数中引用form1:
form2 = new Form2(this);
-
将它存储在form2上的构造函数中的当地引用中:
public Form2 Form1 form1_)
{
InitializeComponent();
form1 = form1_;
}
现在你都是设置使用Form1及其公共属性和控件。要使用 form1.dataGridView1
,您必须首先将其公开。 (或创建公众参考..)
I have two forms and a datagridview which is in the form1.Im trying to add a new column by clicking in a button from form2.Like that:
Form2
private void button1_Click(object sender, EventArgs e)
{
Form1 form1 = new Form1();
form1.dataGridView1.Columns.Add("test" , "test");
}
How can I do that?
Form1 form1 = new Form1();
This will not work as your real form1 is already there, I pressume. Instead create a reference to it in form2 and load it in form2's constructor!
Here are the steps:
- the local reference to form1 in form2's variables:
Form1 form1
When opening form2 pass a reference to form1 in the constructor:
form2 = new Form2(this);
Store it in the local refence in the constructor on form2:
public Form2(Form1 form1_) { InitializeComponent(); form1 = form1_; }
Now you are all set to use Form1 and its public properties and controls. To use form1.dataGridView1
you must make it public first, though. (Or create a public reference to it..)
这篇关于从另一个表单添加datagridview列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!