从另一个表单添加datagridview列 [英] Add a datagridview column from another form

查看:139
本文介绍了从另一个表单添加datagridview列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个表单和一个datagridview,它是在form1.Im尝试添加一个新的列,通过点击一个按钮从form2.Like:

  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的构造函数中!



以下是步骤:


  1. form2变量中form1的本地引用: Form1 form1

  2. 打开form2传递在构造函数中引用form1:



    form2 = new Form2(this);


  3. 将它存储在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:

  1. the local reference to form1 in form2's variables: Form1 form1
  2. When opening form2 pass a reference to form1 in the constructor:

    form2 = new Form2(this);

  3. 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆