I am a beginner in c#, I have two forms mainForm and subForm. The mainForm has DataGridView1 and the subForm has richTextBox1.
I want to add rows and columns to DataGridView1 from subForm through FormClosingEvent
I made the DataGridView1 modifier to public . It is showing in intellisense of subForm but maybe I am doing something wrong.
I have tried the below code in my subForm FormClosingEvent but I cant see any update to my DataGridView1.
if (richTextBox1.Text != "")
{
mainForm newMainForm = new mainForm();
newMainForm.dataGridView1.ColumnCount = 3;
newMainForm.dataGridView1.Columns[0].Name = "Product ID";
newMainForm.dataGridView1.Columns[1].Name = "Product Name";
newMainForm.dataGridView1.Columns[2].Name = "Product Price";
string[] row = new string[] { "1", "Product 1", "1000" };
newMainForm.dataGridView1.Rows.Add(row);
row = new string[] { "2", "Product 2", "2000" };
newMainForm.dataGridView1.Rows.Add(row);
row = new string[] { "3", "Product 3", "3000" };
newMainForm.dataGridView1.Rows.Add(row);
row = new string[] { "4", "Product 4", "4000" };
newMainForm.dataGridView1.Rows.Add(row);
}
Thanks in advance
So assuming your mainForm opens your subForm:
In your example, you need the subForm to be able to change something on the mainform, so, create a Public/Internal method on the subForm, similar to this:
In the mainform, after
subForm.Show(), add something like:Now your subForm can manipulate the correct mainform instance:
I hope that helps!