简体   繁体   English

如何从另一个表单填充 datagridview 单元格? C#

[英]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)

  • User write "Henri" in textbox_Name , "52" in textbox_Age , etc用户写“亨利”,在textbox_Name ,“52”在textbox_Age
  • He validates by clicking on button1他通过单击button1验证

(Form2) (Form2)

  • When the user has validate by clicking on button1 (Form2), each cell in the last index row (a new row) gets the corresponding value of the textbox.当用户通过单击 button1 (Form2) 进行验证时,最后一个索引行(新行)中的每个单元格都会获得文本框的相应值。 Example : 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(); etc等等

not 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.

相关问题 c#从另一个datagridview填充datagridviewcell - c# Fill datagridviewcell from another datagridview 如何在C#中同时将datagridview的多个单元格值从一种形式传递到另一种形式? - How can I pass multiple cell values of datagridview from one form to another at same time in C#? 将数据从一种形式传递到C#中另一种形式的数据网格视图中的特定单元格 - Pass data from one form to a specific cell in a datagridview from another form in C# C#从不同的线程填充dataGridView1 CELL - C# Fill dataGridView1 CELL from different thread 试图用 JSON 中的值填充 C# 中的 Datagridview 但我只得到 1 个单元格填充 - trying to fill a Datagridview in C# with values from a JSON but i only get 1 cell fill 如何在C#中将文本从From2传输到Form1中的dataGridView单元格 - How to transfer text from From2 to dataGridView cell in Form1 in C# C#将datagridview从一种形式读取到另一种形式 - C# reading datagridview from one form to another form C# Winforms如何基于同一行上的另一个单元格从Datagridview获取文本到label? - C# Winforms how to get text from Datagridview to a label based on another cell on the same row? 如何通过添加自定义列的DataTable填充C#中的Datagridview? - How to fill Datagridview in C# from DataTable adding custom columns? 如何使用datagridivew1中的信息填充datagridview2 c# - how to fill the datagridview2 with information from datagridivew1 c#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM