我有一个DataGridView
(tblLoggedJobs),显示用户记录的作业列表。我需要管理员能够更新这些作业以显示作业的任何更新,或者注意作业是否已关闭。
我希望程序将所选ROW中的数据显示到右侧的文本框中,但是我不知道如何获取此数据并根据所选行显示它。
答案 0 :(得分:10)
您可以使用SelectedRows属性。
示例:
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;
}
答案 1 :(得分:8)
在cellclick事件中添加代码
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();
}
有关详细信息,请使用此链接How to Display Selected Row from Datagridview into Textbox using C#
答案 2 :(得分:0)
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();
}
答案 3 :(得分:0)
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();
}
}