Click here to Skip to main content
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 6-Oct-11 0:30am
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  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

if u use bindingsource with linq to sql no neeed for any code.
  Permalink  
Comments
CHill60 at 10-Jun-13 8:25am
   
You do realise that this question is a year and a half old and already marked as solved

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 575
1 Kornfeld Eliyahu Peter 409
2 Maciej Los 369
3 DamithSL 196
4 OriginalGriff 188
0 OriginalGriff 6,353
1 DamithSL 4,854
2 Maciej Los 4,476
3 Kornfeld Eliyahu Peter 4,058
4 Sergey Alexandrovich Kryukov 3,917


Advertise | Privacy | Mobile
Web02 | 2.8.141220.1 | Last Updated 16 Jan 2014
Copyright © CodeProject, 1999-2014
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