0

This is what i want to do . There is a datagridview inside a panel, this panel is inside a tabPage within a tabcontrol and this tabcontrol is in the Form1.

   Form1 --> TabControl  --> tabPage  --> panel  --> **DATAGRIDVIEW**

From my main form, I call Form2 such as Form2.Show() in which the user will input something then if the user click the button lets say "save changes" , the Form 2 will close and in the Form1, i want the datagrid to be updated. when the user clicked the button in Form2 Save Changes. Here's the code:

In Form1:

private void btnEditItem_Click(object sender, EventArgs e)
{
    Form3 form3 = new Form3();
    Form1 frm = this.MdiParent as Form1;
    form3.Show();

    this.Hide();  
}

In the second Form:

private void flatButton1_Click(object sender, EventArgs e)
{
    DialogResult dr = MessageBox.Show("Save Changes?", "Confirmation", MessageBoxButtons.YesNo);
    if (dr == DialogResult.Yes)
    {

        Form form1 = (Form)this.MdiParent;
        DataGridView dt = (DataGridView)form1.Controls["flatTabControl1"].Controls["tabPage5"].Controls["panelUpdateRequest"].Controls["dataGridRequestItemsUpdate"];
        dt.Rows[0].Cells[0].Value = "Plsss";**

        this.Hide();
        form1.Show();

    }
}

The Error:

Datagridview dt = (DataGridView)..Null Reference Exception.

Orkhan Alikhanov
  • 9,122
  • 3
  • 39
  • 60
  • 1
    [Interaction between Forms - How to Change a Control of a Form from Another Form?](http://stackoverflow.com/a/38769212/3110834) – Reza Aghaei Sep 23 '16 at 09:53

2 Answers2

0
Form form1 = (Form)this.MdiParent;

Your this is your form 3 and not form 1. You need a reference of your form 1.

In Form 2 you need something like this:

public From3(Form1 form)
{
 // save the reference of the form 1 in form 3 to use it
}

Then you can call it from form1 with this:

form3.show(this);

hope it can help you

RisuRyu
  • 134
  • 7
  • Sir, i kinda dont get it? Can you please explain further more? – PoorGrammer Sep 23 '16 at 07:48
  • @PoorGrammer "this" is a keyword to get the current instance of the class. You call "this.MdiParent" so the this is the reference of your form3. So it is clear that you get a null reference exception cause there are no gridview in form3. You need a reference of your form1 where your grid is. To get the grid you need to pass the reference of your form 1 to your form 3. – RisuRyu Sep 23 '16 at 07:53
  • So sir, what am i going to put inside public Form3(form1 form){} – PoorGrammer Sep 23 '16 at 08:00
  • @PoorGrammer Yeah, kinda. You add variable Form1(for example here var) to your Form3 and the method below public Form3(form1 form){ this.var = form; } Then you use this "var" variable for this line DataGridView dt = (DataGridView)var.Controls["flatTabControl1"].Controls["tabPage5"].Controls["panelUpdateRequest"].Controls["dataGridRequestItemsUpdate"]; Now you open From3 and give it form1: form3.show(this); Hope you can understand it somehow. – RisuRyu Sep 23 '16 at 08:08
0

Define a function in your first form:

public DataGridView GetDataGridView()
{
    return this.dataGridRequestItemsUpdate;
}

And retrieve it in the second form:

Form form1 = (Form)this.MdiParent;
var dataGridView = form1.GetDataGridView();
stefankmitph
  • 3,236
  • 2
  • 18
  • 22