question

AmerShokry-5319 avatar image
0 Votes"
AmerShokry-5319 asked AmerShokry-5319 action

pass from data grid to textbox in c#

hi dears,

I have mdifrom contains a form which have more than textbox and a button, I clicked the button to showdailog form which have a datagrid I make event when doubleclickrow header to get the values of cells for this row and send it the last form and set the values to its textbox but untill now the textboxs are empty and donet change.

kindly help me.

windows-forms
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

DanielZhang-MSFT avatar image
0 Votes"
DanielZhang-MSFT answered AmerShokry-5319 commented

Hi AmerShokry-5319,
Based on your description, I made a test.
Please refer to the following for specific steps:
1.First, add a new Form2 and set it to mdiform(set Form1's isMdiContainer property to true ).
Then open the mdiform(Form2) by clicking the New item in MenuStrip control.
Form1.cs

 private void newToolStripMenuItem_Click(object sender, EventArgs e)
 {
 Form2 f2 = new Form2();
 f2.MdiParent = this;
 f2.Show();
 }

2.Add textbox and button controls into Form2.
3.Add a new form(Form3) and add DataGridView control to it. Fill data to DataGridView.
4.Open Form3 by clicking the button in Form2.
Form2.cs

 private void button1_Click(object sender, EventArgs e)
 {
     Form3 f3 = new Form3();
     f3.Show();
 }

5.Pass DataGridView row valus into TextBox in Form2 via DataGridView.RowHeaderMouseDoubleClick event.
Note: you need to select the TextBox control and in Properties window set the Modifiers property to Public and then you can simply access this control using its name from outside of the Form.
Form3.cs

 private void dataGridView1_RowHeaderMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
 {
     Form2 fr= (Form2)Application.OpenForms["Form2"];
     int row = e.RowIndex;
     fr.textBox1.Text = Convert.ToString(dataGridView1[0, row].Value);
     fr.textBox2.Text = Convert.ToString(dataGridView1[1, row].Value);
 }

Here is my test result:
91593-427.gif
If you want to use ShowDialog(), please refer to this thread.
And here is a related thread.
Best Regards,
Daniel Zhang


If the response is helpful, please click "Accept Answer" and upvote it.

Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.



427.gif (291.8 KiB)
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thank you for your great help

0 Votes 0 ·