简体   繁体   English

将值从Form2 ListView传递到Form1 DataGridView

[英]Pass Value from Form2 ListView to Form1 DataGridView

I've 2 forms in C# Application, Form1 is main form containing DataGridView, columns are AccountCode, AccountName, Amount, Narration. 我在C#应用程序中有2个表单,Form1是包含DataGridView的主表单,列是AccountCode,AccountName,Amount,Narration。 At present user can enter AccountCode manually and using Sql query AccountName is populated with relevant title. 目前,用户可以手动输入AccountCode并使用Sql查询AccountName填充相关标题。

Now I am looking for a way to that user will search AccountCode in Form2 containing ListView holding AccountCodes and AccountName, after user DoubleClick or press OK button return selected value to Active row in Form1 DataGridView. 现在,我正在寻找一种方法,该方法是在用户DoubleClick或按OK按钮将选定的值返回到Form1 DataGridView中的“活动”行之后,该用户将在Form2中搜索包含拥有AccountCodes和AccountName的ListView的AccountCode。 Active Row is the main concern here, because look up Form2 will not only be used to enter new record but also to update other records which also require look up values from Form2 ListView. Active Row是这里的主要问题,因为查找Form2不仅将用于输入新记录,而且还将用于更新其他记录,这些记录也需要从Form2 ListView中查找值。

I know how to pass values between Form2 ListView to Form1 TextBoxes using delegates, but don't know how to pass it to DataGridView's Active Row. 我知道如何使用委托在Form2 ListView之间将值传递到Form1 TextBoxes,但不知道如何将其传递到DataGridView的活动行。

Seeking for expert guidance/advices 寻求专家的指导/建议

Ahmed 艾哈迈德

in the Form2 you can create public property and set value to that according to your logic 在Form2中,您可以根据您的逻辑创建公共属性并为其设置值

 public string SelectedCode { get; set; }

and in Form1 you can access that public property and set value to datagrid for example 在Form1中,您可以访问该公共属性并将值设置为datagrid,例如

        Form2 frm2 = new Form2();
        frm2.ShowDialog(this);
        dataGridView1.SelectedCells[0].Value = frm2.SelectedCode;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM