0

What I am trying to do is when a user double clicks on a row a separate form will pop up. Then populates the the TextBoxes in the new form with values from the DataGridView. I can get the Form to appear from a double click, after that im not sure what to do? My code :

private void dgvTable_CellContentDoubleClick(object sender, DataGridViewCellEventArgs e)
{
    FrmInfo frmInfo = new FrmInfo();

    frmInfo.ShowDialog();
    frmInfo.Dispose();
    var notes = dgvTable.Rows[e.RowIndex].Cells["Notes"].Value;   
}

Thanks

Bijington
  • 3,661
  • 5
  • 35
  • 52
AndroidAL
  • 1,111
  • 4
  • 15
  • 35
  • I dont know what code you have so far, but couldn't you create a constructor for the new form which takes the needed data from the dataGridView as parameters, and create the form using that constructor on the double click event? – spersson Jan 13 '16 at 14:06
  • 3
    Possible duplicate of [Send values from one form to another form](http://stackoverflow.com/questions/1559770/send-values-from-one-form-to-another-form) – stuartd Jan 13 '16 at 14:08

1 Answers1

1

It's Easy! First of all you can't reach other forms controls in c# in the normal way that vb.net does, so you can do the following:

  • Preparing form to receive data:

1- In the form constructor void add parameters to handle the received data.

2- After the InitializeComponent(); void add the lines of code like below:

public frm1(string txt1, string txt2, string txt3)
{
     InitializeComponent();
     textBox1.Text = txt1;
     textBox2.Text = txt2;
     textBox3.Text = txt3;
}
  • Handle the datagridview dg_RowHeaderMouseDoubleClick event:

    -Note: you can also handle the CellDoubleClick event in your example, but my event is make more sense.

1- Create variables to hold the sent data.

2- Create a new form instance for the form that will handle the data and pass the row cells data to the method parameters in the exact desired order.

private void dg_RowHeaderMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
{
    //Collect the row cells values:
    string val1 = dg.Rows[e.RowIndex].Cells[0].Value.ToString() //data of the first cell in the row
    string val2 = dg.Rows[e.RowIndex].Cells[1].Value.ToString() //data of the second cell in the row
    string val3 = dg.Rows[e.RowIndex].Cells[2].Value.ToString() //data of the third cell in the row
    //Initialize a new instance of the data handle form and send the row data to it:
    var newFrm = new frm1(val1, val2, val3);
    newFrm.Show();                
}

And so on, you can create as many parameters as you can.

Ahmed Suror
  • 439
  • 6
  • 17
  • Thanks, put im stuck on your step 1? can you give an example – AndroidAL Jan 13 '16 at 15:23
  • @AndroidAL In the form that will have the textboxes -frmInfo in your example- view the code of it, you should see in the top a void like: `public frmInfo()` this is the form constructor, modify it like the answer: `public frmInfo(string txt1, string txt2, string txt3)` Try and reply... – Ahmed Suror Jan 13 '16 at 15:43