Home > DeveloperSection > Forums > Add a datagridview collumn from another form
jayprakash sharma
jayprakash sharma

Total Post:117

Posted on    August-18-2014 3:12 AM

 C# C# 

 1 Reply(s)
 990  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?

Pravesh Singh

Total Post:412

Posted on    August-18-2014 3:35 AM

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_;


Don't want to miss updates? Please click the below button!

Follow MindStick