I'm a beginer in C# and for now the most difficult thing to me is to understand how to pass data and modify objects from a form/class to another form/class. I understand that I should use event or delegate but I'm not sure how to use it yet.
For now, I have a datagridview (with Column_Name, Column_Age etc) in my Form1, and in my Form2, I have some textboxs that the user will fill and validate with a button.
I want that when the user press the validate button in Form2, the textbox text goes to to a cell of my datagridview in Form1, on the last row.
Example:
(Form1)
textbox_Name
, "52" in textbox_Age
, etcbutton1
(Form2)
datagridview1[*newrow.Index*,Column_Name.Index] = textbox_Name.Text.ToString();
datagridview1[*newrow.Index*,Column_Age.Index] = textbox_Age.Text.ToString();
etcnot sure about the newrow.Index
Thanks ! If you have some good links to help me on that, I'll take!
Ok so I have found a solution. Let me know if you would have done otherwise.
In Form1.cs
:
private static Form1 _Form1;
public static Form1 GetInstance()
{
if (_Form1 == null)
{
_Form1 = new Form1();
}
return _Form1;
}
In Form2.cs
:
private void button1_Click(object sender, EventArgs e)
{
Form1.GetInstance().dataGridView1[Form1.GetInstance().Column_Name.Index, Form1.GetInstance().dataGridView1.Rows.Add()].Value = textbox_Name.Text.ToString();
Form1.GetInstance().dataGridView1[Form1.GetInstance().Column_Age.Index, Form1.GetInstance().dataGridView1.Rows.Add()].Value = textbox_Age.Text.ToString();
}