Click here to Skip to main content
11,716,249 members (88,914 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#3.5 DataGridView
Hi, need help on getting the values from datagridview to textboxes, i am having a datagridview getting data's from SQL which counts 4 column and number of rows retrieved from sql, i am having 4 textboxes, i want the datagridview selected row's column values to the textboxes.

Kindly advice on the same.
Posted 5-Oct-11 23:30pm
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

You will probably want to respond the to the DataGridView.SelectionChanged Event[^].
In its handler check if a single row has been selected then get the values from that row and place them in the text boxes.
private void DataGridView1_SelectionChanged(object sender, EventArgs e)
{
  if( DataGridView1.SelectedRows.Count = 0 )
  {
    TextBox1.Value = DataGridView1.SelectedRows(0).Cells(0).Value;
    TextBox2.Value = DataGridView1.SelectedRows(0).Cells(1).Value;
    TextBox3.Value = DataGridView1.SelectedRows(0).Cells(2).Value;
    TextBox4.Value = DataGridView1.SelectedRows(0).Cells(3).Value;
  }
}
The code has not been tested and may contain typos.

Make sure the DataGridView.SelectionMode Property[^] is set to FullRowSelect for this to work.

You can use DataGridView.MultiSelect Property[^] to make sure only one row is selected a time.
  Permalink  
v3
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Register MouseClick event of grid and use following code.
private void dataGridView1_MouseClick(object sender, MouseEventArgs e)
{
    DataGridViewRow dr = dataGridView1.SelectedRows[0];
    textBox1.Text = dr.Cells[0].Value.ToString();
     // or simply use column name instead of index
    //dr.Cells["id"].Value.ToString();
    textBox2.Text = dr.Cells[1].Value.ToString();
    textBox3.Text = dr.Cells[2].Value.ToString();
    textBox4.Text = dr.Cells[3].Value.ToString();
}
And add the following line in your load event
dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
  Permalink  
v2
Comments
jaypee1979 at 6-Oct-11 6:14am
   
Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index

kindly advice
Xeshan Ahmed at 7-Oct-11 1:41am
   
index of cell is out of range or index of row ? Send us your code and error statement if possible
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Hi, its taking fine now
private void dgviewSearch_CellcontentClick(object sender, DataGridViewCellEventArgs e)
{
int i;
i = dgViewSearch.SelectedCells[0].RowIndex;
textBox3.Text = dgViewSearch.Rows[i].Cells[1].Value.ToString();
textBox4.Text = dgViewSearch.Rows[i].Cells[3].Value.ToString();
}

Thank you very much for your help, i am looking forward for mulitple selection on datagridview to textbox with ,(comma) like (2001,2002,2003)
  Permalink  
Comments
Member 10248514 at 29-May-15 4:05am
   
Its working ...
Ralf Meier at 27-Jun-15 16:42pm
   
A little Addon to this :
Instead of "i = dgViewSearch.SelectedCells[0].RowIndex;" you could also use the DataGridViewCellEventArgs, which gives you also the information about the currently selected cell and/or row ...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 664
1 Maciej Los 340
2 Richard MacCutchan 280
3 F-ES Sitecore 240
4 OriginalGriff 175
0 Sergey Alexandrovich Kryukov 1,029
1 Maciej Los 464
2 Richard MacCutchan 430
3 CHill60 415
4 OriginalGriff 415


Advertise | Privacy | Mobile
Web01 | 2.8.150901.1 | Last Updated 27 Jun 2015
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100