ADD A DATAGRIDVIEW COLLUMN FROM ANOTHER FORM

jayprakash sharma

Total Post:117

Points:821
Posted by  jayprakash sharma
C# 
C#
 1181  View(s)
Ratings:
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:

 Form2

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

How can I do that?

  1. Pravesh Singh

    Post:412

    Points:2888
    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_)
    {
        InitializeComponent();
        form1 = form1_;
    }


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

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!