Bonaca
Bonaca

Reputation: 313

FormClosing does not see DataGridView Rows and Columns

Please, what is wrong with this:
Form2_Closing:

Form1.DataGridView1.Rows[0].Cells[1].Value = "323";

Error: Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index

DGV on Form1 has 10 Rows and 14 Columns

Upvotes: 0

Views: 171

Answers (2)

Angshuman Agarwal
Angshuman Agarwal

Reputation: 4866

Create a new Winforms Project and add a button & its click handler and a TextBox [make it accessible, such that the child can set value. I have made it public in the designer for now] too. Then add the following code on this Form. Additionally, add a new Form (Form2) in the Project.

private void button1_Click(object sender, EventArgs e)
        {
            var child = new Form2();
            child.FormClosing += new FormClosingEventHandler(ChildFormClosing);
            this.Enabled = false;
            child.Show(this);
        }

        void ChildFormClosing(object sender, FormClosingEventArgs e)
        {
            var child = sender as Form2;
            if (child != null)
            {
                if (child.DialogResult == DialogResult.None)
                {
                    // do data grid view manipulation here 
                    // for ex:
                   (child.Owner as Form1).textBox1.Text = "Hi";
                }
            }
            Enabled = true;
        }

Upvotes: 1

NominSim
NominSim

Reputation: 8511

From your comments it looks like you are trying to create a custom Dialog that will manipulate a particular value within a DataGridView on the calling form. I suggest looking at this example of creating a custom message box.

You'll be able to return say the value that you want the DataGridViewCell updated to, then set it on your Form1.

Upvotes: 1

Related Questions