I have a DataGridView
(tblLoggedJobs) that displays a list of jobs logged by a user. I need the admins to be able to update these jobs to display any updates to the job or note if the job is closed.
I would like the program to display the data in the selected ROW to the textboxes to the right, however I'm not sure how to get this data and display it based on the row that is selected.
DataGridView selected row to display in text boxes
Asked Answered
You can use 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;
}
What if the user selected more than one row? –
Funeral
@Funeral "What if the user selected more than one row? " Properties of the Datagridview -> MultiSelect = False –
Voorhees
Works great! What is the purpose of the "string.Empty" that is added to the end of each variable declaration statement? –
Guest
dataGridView1.SelectedRows[0].Cells[0].Value
return the Object
so you need to cast it to String
–
Blasien Does that mean that I can swap out
String
with int
or DateTime
? –
Guest Yes you can swap it with int, DateTime, etc –
Blasien
Add code in 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 Selected Row from Datagridview into Textbox using C#
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();
}
private void MyDataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
DataGrid dg = (DataGrid)sender;
DataRowView rs = dg.SelectedItem as DataRowView;
if(rs != null)
{
textBoxJobId.Text = rs["JobId"].ToString();
}
}
spied on the channel https://www.youtube.com/user/saf3al2a/videos
Always use CellEnter event because it will work both with keyboard arrow keys and mouse click.
always use dataGridView1.SelectedCells.Count > 0
"SelectedCells otherwise you have to select complete row.
private void dataGridView1_CellEnter(object sender, DataGridViewCellEventArgs e)
{
if (dataGridView1.SelectedCells.Count > 0) // make sure user select at least 1 cell
{
DataGridToTextboxes(e.RowIndex);
}
}
private void DataGridToTextboxes(int x)
{
//try a seperate method to show gridview to text boxes, you can you same method for multiple events.
string Item1 = dataGridView1.Rows[x].Cells[1].Value + string.Empty;
string MinRange = dataGridView1.Rows[x].Cells[2].Value + string.Empty;
string MaxRange = dataGridView1.Rows[x].Cells[3].Value + string.Empty;
string MinStable = dataGridView1.Rows[x].Cells[4].Value + string.Empty;
string MaxStable = dataGridView1.Rows[x].Cells[5].Value + string.Empty;
txtItem.Text = Item1;
TextMinRange.Text = MinRange;
TextMaxRange.Text = MaxRange;
TextMinStable.Text = MinStable;
TextMaxStable.Text = MaxStable;
}
© 2022 - 2025 — McMap. All rights reserved.