标题非常明显.我有一个Windows窗体应用程序的DataGrid,我希望能够存储所选行的值.这样做最简单的方法是什么?
我在搜索中找到了这段代码作为示例,但在DataGrid的排序方式不同时不起作用:
private void grdPatients_CurrentCellChanged(object sender, EventArgs e)
{
int row = grdPatients.CurrentRowIndex;
grdPatients.Select(row);
ArrayList arrayList = new ArrayList();
for (int i = 0; i < 3; i++)
{
arrayList.Insert(i, (patientsDS.Tables["PatientList"].Rows[row].ItemArray.GetValue(i)));
}
textBox1.Text = "" + arrayList[0];
textBox2.Text = "" + arrayList[1];
textBox3.Text = "" + arrayList[2];
}
Run Code Online (Sandbox Code Playgroud)
dkn*_*ack 33
假设我理解你的问题.
您可以使用DataGridView.SelectedRowsCollection 获取所选行.如果您的DataGridView只允许选择一个,请查看我的示例.
DataGridView.SelectedRows获取用户选择的行集合.
if (dataGridView1.SelectedRows.Count != 0)
{
DataGridViewRow row = this.dataGridView1.SelectedRows[0];
row.Cells["ColumnName"].Value
}
Run Code Online (Sandbox Code Playgroud)
小智 5
你可以用
DataGridView1.CurrentRow.Cells["ColumnName"].Value
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
94365 次 |
| 最近记录: |