Csharp/C#教程:如何从DataGrid for Windows Form Application中的选定行获取值?分享


如何从DataGrid for Windows Form Application中的选定行获取值?

标题非常明显。 我有一个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]; } 

描述

假设我理解你的问题。

您可以使用DataGridView.SelectedRows集合获取所选行。 如果您的DataGridView只允许选择一个,请查看我的示例。

DataGridView.SelectedRows获取用户选择的行集合。

上述就是C#学习教程:如何从DataGrid for Windows Form Application中的选定行获取值?分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!

样品

 if (dataGridView1.SelectedRows.Count != 0) { DataGridViewRow row = this.dataGridView1.SelectedRows[0]; row.Cells["ColumnName"].Value } 

更多信息

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/cdevelopment/1019676.html

(0)
上一篇 2022年1月5日
下一篇 2022年1月5日

精彩推荐