[英]How to get values from selected row in DataGrid for Windows Form Application?
Title is pretty self-explanatory.标题是不言自明的。 I've got a DataGrid for a Windows Form Application, and I want to be able to store the values of a selected row.
我有一个用于 Windows 窗体应用程序的 DataGrid,我希望能够存储选定行的值。 What is the easiest way to go about doing this?
这样做最简单的方法是什么?
I have found this chunk of code as an example in my search, but doesn't work when the DataGrid is sorted differently:我在搜索中找到了这段代码作为示例,但是当 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];
}
Assuming i understand your question.假设我理解你的问题。
You can get the selected row using the DataGridView.SelectedRows
Collection.您可以使用
DataGridView.SelectedRows
集合获取选定的行。 If your DataGridView allows only one selected, have a look at my sample.如果您的 DataGridView 只允许选择一个,请查看我的示例。
DataGridView.SelectedRows Gets the collection of rows selected by the user.
DataGridView.SelectedRows获取用户选择的行的集合。
if (dataGridView1.SelectedRows.Count != 0)
{
DataGridViewRow row = this.dataGridView1.SelectedRows[0];
row.Cells["ColumnName"].Value
}
你可以使用
DataGridView1.CurrentRow.Cells["ColumnName"].Value
For multiple choise use:对于多选使用:
foreach (DataGridViewRow row in dataGridView1.SelectedRows)
{
// your code
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.