pass from data grid to textbox in c#

Amer Shokry 21 Reputation points
2021-04-26T23:05:06.753+00:00

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
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,811 questions
0 comments No comments
{count} votes

Accepted answer
  1. Daniel Zhang-MSFT 9,611 Reputation points
    2021-04-27T03:14:06.277+00:00

    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.

    2 people found this answer helpful.

0 additional answers

Sort by: Most helpful