Advertise with us

Add a datagridview collumn from another form

Posted by  jayprakash sharma
 1563  View(s)
Rate this:

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:


    private void button1_Click(object sender, EventArgs e)
        Form1 form1 = new Form1();
        form1.dataGridView1.Columns.Add("test" , "test");

How can I do that?

  1. Re: Add a datagridview collumn from another form

    Hi Jay, 

    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_)
        form1 = form1_;

      Modified On Apr-04-2018 05:51:50 AM