DataGridView selected row to display in text fields

I have a DataGridView (tblLoggedJobs) that displays a list of jobs registered by the user. I need admins to be able to update these tasks, to display any task updates or to mark if the task is closed. I would like the program to display the data in the selected ROW in the text fields on the right, however I'm not sure how to get this data and display it based on the selected row.

Get Selected Row to text boxes

+5
source share
3 answers

You can use the SelectedRows property.

Example:

 if(dataGridView1.SelectedRows.Count > 0) // make sure user select at least 1 row { string jobId = dataGridView1.SelectedRows[0].Cells[0].Value + string.Empty; string userId = dataGridView1.SelectedRows[0].Cells[2].Value + string.Empty; txtJobId.Text = jobId; txtUserId.Text = userId; } 
+8
source

Select cellclick event of datagridview

Add code to cellclick event

 if (e.RowIndex >= 0) { //gets a collection that contains all the rows DataGridViewRow row = this.dataGridView1.Rows[e.RowIndex]; //populate the textbox from specific value of the coordinates of column and row. txtid.Text = row.Cells[0].Value.ToString(); txtfname.Text = row.Cells[1].Value.ToString(); txtlname.Text = row.Cells[2].Value.ToString(); txtcourse.Text = row.Cells[3].Value.ToString(); txtgender.Text = row.Cells[4].Value.ToString(); txtaddress.Text = row.Cells[5].Value.ToString(); } 

For more information, use this link. How to display the selected row from Datagridview into a text field using C #

+6
source
 private void orderpurchasegridview_CellClick(object sender, DataGridViewCellEventArgs e) { int ind = e.RowIndex; DataGridViewRow selectedRows = orderpurchasegridview.Rows[ind]; txtorderid.Text = selectedRows.Cells[0].Value.ToString(); cmbosuppliername.Text = selectedRows.Cells[1].Value.ToString(); cmboproductname.Text = selectedRows.Cells[2].Value.ToString(); txtdate.Text = selectedRows.Cells[3].Value.ToString(); txtorderquantity.Text = selectedRows.Cells[4].Value.ToString(); } 
0
source

All Articles