如何从DataGrid中选择行值Windows窗体应用程序? [英] How to get values from selected row in DataGrid for Windows Form Application?
问题描述
标题是不言自明。我有一个Windows窗体应用程序一个DataGrid,我希望能够存储一个选定行的值。什么是去这样做最简单的方法?
我发现这个块的代码作为我的搜索的例子,但在DataGrid进行排序不起作用不同的:
私人无效grdPatients_CurrentCellChanged(对象发件人,EventArgs五)
{
INT行= grdPatients。 CurrentRowIndex;
grdPatients.Select(行);
的ArrayList ArrayList的=新的ArrayList();
的for(int i = 0;我3;;我++)
{
arrayList.Insert(I,(patientsDS.Tables [PatientList] .Rows [行] .ItemArray.GetValue(I)));
}
textBox1.Text =+ ArrayList的[0];
textBox2.Text =+ ArrayList的[1];
textBox3.Text =+ ArrayList中[2];
}
说明
假设我明白你的问题。
你可以使用 DataGridView.SelectedRows
收藏选定行。如果您的DataGridView只允许一个选择,看看我的样本。
DataGridView.SelectedRows 获取集合的由用户选择的行
示例
如果(dataGridView1.SelectedRows.Count!= 0)
{
的DataGridViewRow行= this.dataGridView1.SelectedRows [0];
row.Cells [的ColumnName]。价值
}
更多信息
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. 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:
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];
}
Description
Assuming i understand your question.
You can get the selected row using the DataGridView.SelectedRows
Collection. If your DataGridView allows only one selected, have a look at my sample.
DataGridView.SelectedRows Gets the collection of rows selected by the user.
Sample
if (dataGridView1.SelectedRows.Count != 0)
{
DataGridViewRow row = this.dataGridView1.SelectedRows[0];
row.Cells["ColumnName"].Value
}
More Information
这篇关于如何从DataGrid中选择行值Windows窗体应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!