Pass Value from Form2 ListView to Form1 DataGridVi

2019-09-02 03:58发布

问题:

I've 2 forms in C# Application, Form1 is main form containing DataGridView, columns are AccountCode, AccountName, Amount, Narration. At present user can enter AccountCode manually and using Sql query AccountName is populated with relevant title.

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

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.

Seeking for expert guidance/advices

Ahmed

回答1:

in the Form2 you can create public property and set value to that according to your logic

 public string SelectedCode { get; set; }

and in Form1 you can access that public property and set value to datagrid for example

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