0
votes

I have two forms Form1 and Form2, Form 1 with a dataGridView and a button where Form2 has one button.

When i click the button1 in Form1, it will open Form2 (overlapping).

Now i need to clear the values of the datagridview in Form1 when i press the button in Form2 also the same button click event should close the Form2.

Any idea how to do it?

Any help is appreciated

4
I use Form1.Close(); but the instance is closed before dataGridView.Rows.Clear(); executes... and i don't know how else to do this...Account Unknown

4 Answers

2
votes

This is Form 2:

private DataGridView _dgv;

public Form2(DataGridView dgv)
{
    _dgv = dgv;
    InitializeComponent();
}


private void buttonClearRows_Click(object sender, EventArgs e)
{
     _dgv.Rows.Clear();
     Close();
}

This is Form1:

 private void buttonOpenForm2_Click(object sender, EventArgs e)
 {
   new frm2(dataGridView1).ShowDialog();
 }
1
votes

You need to pass a reference to Form1 into Form2, either by consctructor or by a property. Sample uses constructor. Change the names of the controls to the ones you have. Consider this as a pseudo code sample.

Form1 (some logic removed for brewity)

public class Form1
{
   ...
   public void Clear() 
   {
       DataGridView1.Rows.Clear();
   }

   public void btnOpenForm2_Click(object sender, EventArgs e)
   {
      var form2 = new Form2(this); // create a new form2, and pass a reference to form1
      form2.Show(); // show the form.
   }
   ...
}

Form2 (some logic removed for brewity)

public class Form2
{
   private Form1 _parent; // this will hold the parent until Form2 is disposed.
   ...
   public void Form2(Form1 parent) 
   {
       _parent = parent; // assign Form1 instance to a field.
   }

   public void btnClearGrid(object sender, EventArgs e)
   {
      _parent.Clear(); // clear the rows in the datagridview instance within form1.
   }
   ...
}
1
votes

If I understand what you're asking, I strongly suggest to hide access to DataGridView in Form1 from outside the class, to avoid unexpected behavior in future.

You could instead add a function in Form1 and manage button1 click event in this way:

public partial class Form1 : Form
{
    private void button1_Click(object sender, EventArgs e)
    {
        Form2 dialog = new Form2();
        dialog.ShowDialog(this);
    }

    public void ClearRows() { dataGridView1.Rows.Clear(); }
}

Then in Form2 you can easily handle click on button in this way:

public partial class Form2 : Form
{
    private void button1_Click(object sender, EventArgs e)
    {
        ((Form1)this.Owner).ClearRows();
        this.Close();
    }
}
0
votes

It's very simply, in the form1 you can create an instance of Form2 and after the show you can set the reference of dataGridView in child form2.

Fundamental is who you set the dataGridProperty modifier=public(visual property F4) in the form1

This is Form1:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        Form2 f = new Form2();
        f.dataGridViewPassed = this.dataGridView1;
        f.ShowDialog();
    }
}

This is Form 2:

public partial class Form2 : Form
{

    public DataGridView dataGridViewPassed = null;

    public Form2()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        this.dataGridViewPassed.Rows.Clear();
    }
}