Click here to Skip to main content
16,022,875 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
How to display data in a textbox from a datagridview.When I select a row in DGV I want the rowdata to be displayed in respective textboxes present in the form. I have few textboxes,datagridview in the form.Datagridview is populated from DB.I use textboxes to Insert,update data.When I select a row in DGV , that row data should be displayed in textboxes so that I can Update the data.
Posted

Try using the RowEnter or CellClick events for DataGridView to put the appropriate values into your text boxes. You can get the Row and/or Cell selected from the event arguments.
 
Share this answer
 
C#
private void dataGridView1_RowHeaderMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            int xyz = e.RowIndex;
            DataRowView dr = (DataRowView)dataGridView1.Rows[xyz].DataBoundItem;
            textBox1.Text = dr["EmpID"].ToString();
            textBox2.Text = dr["EmpName"].ToString();
            textBox3.Text = dr["EmpType_ID"].ToString();
}
 
Share this answer
 
v2
Comments
Herman<T>.Instance 26-Aug-11 7:27am    
and what if the user only singleclicks?
HarinderS 26-Aug-11 7:33am    
then you can simply write the code in RowHeaderMouseClick event handler rather than mousedoubleclick:)
HarinderS 26-Aug-11 7:29am    
This is code of Visual Studio 2008 C# or you can say visual c sharp...
I hope you can figure out easily what i mean to say.... Your DGV is DataGridView1 in my code. For any further queries comment here.

And i wonder this is a question asked in 2009, so why didn't anyone replied this???
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
textbox1.Text = GridView1.Rows[GridView1.SelectedRow.RowIndex].Cells[1].Text;
textbox2.text.Text = GridView1.Rows[GridView1.SelectedRow.RowIndex].Cells[2].Text;

}

By this way u can get the data to TextBox


Regards,

Anilkumar.
 
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