Click here to Skip to main content
13,095,921 members (54,159 online)
Rate this:
Please Sign up or sign in to vote.
See more:
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
Member 11934348 22-Sep-16 5:45am
haiiii i want the same thing to work in javascript can any one help
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.
Rojalin Sahoo 22-Jan-16 0:24am
may the if condition will be like: if(DataGridView1.SelectedRows.Count > 0) then it will display data on text-box.
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 29-May-15 4:05am
Its working ...
Ralf Meier 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 ...
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 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 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 8

private void dgvAllUsers_CellContentClick(object sender, DataGridViewCellEventArgs e)

if (e.RowIndex >= 0)
DataGridViewRow row = this.dgvAllUsers.Rows[e.RowIndex];

textId.Text = row.Cells["idUser"].Value.ToString();
textFullName.Text = row.Cells["FullName"].Value.ToString();
textUserName.Text = row.Cells["UserName"].Value.ToString();
textPassword.Text = row.Cells["Password"].Value.ToString();
textAddress.Text = row.Cells["Address"].Value.ToString();
textGender.Text = row.Cells["Gender"].Value.ToString();
textContactNumber.Text = row.Cells["ContactNumber"].Value.ToString();
ComboUserType.SelectedText = row.Cells["UserType"].Value.ToString();

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web02 | 2.8.170813.1 | Last Updated 21 Jan 2016
Copyright © CodeProject, 1999-2017
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