Search code examples
c#sqlforms

Pass Datagridview row data to new form


I have two forms. Let's say form A and form B. I want to pass datagridview row data when clicked, from A to new form B. Note, datagridview is in a UserControl in form A.

I've tried using below:

Form frm3 = new Form3();

        frm3.textBox1.Text = dataGridView1.CurrentRow.Cells[0].Value.ToString();
        frm3.textBox5.Text = dataGridView1.CurrentRow.Cells[2].Value.ToString();
        frm3.textBox6.Text = dataGridView1.CurrentRow.Cells[3].Value.ToString();
       

However, I get a warning. "Form does not contain definition for textbox1 and no acceptable extension method ....."

I need ideas. Thanks!


Solution

  • Your variable declaration is wrong.

    Form frm3 = new Form3();
    

    should be

    Form3 frm3 = new Form3();
    

    And, modify text objects in Form3 to have them public accessible.

    //private System.Windows.Forms.TextBox textBox1;
    //private System.Windows.Forms.TextBox textBox5;
    //private System.Windows.Forms.TextBox textBox6;
    public System.Windows.Forms.TextBox textBox1;
    public System.Windows.Forms.TextBox textBox5;
    public System.Windows.Forms.TextBox textBox6;
    

    The base Form class has no notion of any text boxes or any other controls because they were declared in the Form3 class, not the Form class OR they were private in Form3 not accessible publicly from other classes.