【发布时间】:2015-04-28 02:41:02
【问题描述】:
我有一个DataGridView(tblLoggedJobs),它显示用户记录的作业列表。我需要管理员能够更新这些作业以显示作业的任何更新或说明作业是否已关闭。
我希望程序将所选行中的数据显示到右侧的文本框中,但是我不确定如何获取此数据并根据所选行显示它。
【问题讨论】:
标签: c# datagridview
我有一个DataGridView(tblLoggedJobs),它显示用户记录的作业列表。我需要管理员能够更新这些作业以显示作业的任何更新或说明作业是否已关闭。
我希望程序将所选行中的数据显示到右侧的文本框中,但是我不确定如何获取此数据并根据所选行显示它。
【问题讨论】:
标签: c# datagridview
您可以使用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;
}
【讨论】:
dataGridView1.SelectedRows[0].Cells[0].Value 返回Object 所以你需要将它转换为String
String 换成int 或DateTime?
在 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#
【讨论】:
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();
}
}
【讨论】: