[英]How to fill a datagridview cell from another form ? C#
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.我是 C# 的初学者,现在对我来说最困难的事情是了解如何将数据和修改对象从一个表单/类传递到另一个表单/类。 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.现在,我的 Form1 中有一个 datagridview(带有 Column_Name、Column_Age 等),而在我的 Form2 中,我有一些文本框,用户将使用按钮填充和验证这些文本框。
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.我希望当用户按下 Form2 中的验证按钮时,文本框文本转到 Form1 中最后一行的 datagridview 的单元格。
Example:例子:
(Form1) (Form1)
textbox_Name
, "52" in textbox_Age
, etctextbox_Name
,“52”在textbox_Age
等button1
button1
验证(Form2) (Form2)
datagridview1[*newrow.Index*,Column_Name.Index] = textbox_Name.Text.ToString();
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不确定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
:在
Form1.cs
:
private static Form1 _Form1;
public static Form1 GetInstance()
{
if (_Form1 == null)
{
_Form1 = new Form1();
}
return _Form1;
}
In Form2.cs
:在
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();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.