Click here to Skip to main content
15,941,293 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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
Comments
Member 11934348 22-Sep-16 5:45am    
haiiii i want the same thing to work in javascript can any one help

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.
C#
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.
 
Share this answer
 
v3
Comments
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.
Hi, its taking fine now
C#
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)
 
Share this answer
 
Comments
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 ...
Register MouseClick event of grid and use following code.
C#
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
C#
dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
 
Share this answer
 
v2
Comments
jaipe 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
Member 15509784 23-Jan-22 10:46am    
thanks wrkd fr me well
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();
}
}
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900