Click here to Skip to main content
11,798,546 members (67,538 online)
Rate this: bad
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
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.
Rate this: bad
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
    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;
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
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)
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 CPallini 585
1 Maciej Los 395
2 OriginalGriff 348
3 Abhinav S 257
4 phil.o 254
0 OriginalGriff 2,150
1 Maciej Los 1,790
2 KrunalRohit 1,456
3 CPallini 1,415
4 Richard MacCutchan 1,016

Advertise | Privacy | Mobile
Web03 | 2.8.151002.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