繁体   English   中英

如何在DataGridView中获取选中的DataRow?

[英]How Do I Get the Selected DataRow in a DataGridView?

我有一个绑定到 DataGridView 的 DataTable。 我在 DGV 中启用了 FullRowSelect。 有没有办法将所选行作为 DataRow 获取,以便我可以对所选行的值进行强类型访问?

DataRowView currentDataRowView = (DataRowView)dgv1.CurrentRow.DataBoundItem
DataRow row = currentDataRowView.Row

我不知道如何在没有 BindingSource 的情况下做到这一点,以下是如何做到这一点:

var drv = bindingSoure1.Current as DataRowView;
if (drv != null)
  var row = drv.Row as MyRowType;

可以通过获得以下属性:

this.dataGridView.SelectedRows

一个获取类型的集合: DataGridViewSelectedRowCollection 它包含以下类型的项目: DataGridViewRow

然后可以通过以下方式获得具有自己类型的 bounditem :

DataGridViewSelectedRowCollection list = this.dataGridViewInventoryRecords.SelectedRows;
MyType selectedItem = (MyType)list[0].DataBoundItem; //[0] ---> first item

您应该能够直接将您选择的行转换为绑定到 DataGridView 的强类型行。

试试这个:

DataRow row = gridView1.GetDataRow(gridView1.FocusedRowHandle); `        
if (row != null)
{
     XtraMessageBox.Show(row["ID"].ToString());
}

如果您已将 datagridview 绑定到数据库中的表或视图,则可以将数据作为强类型对象取出。

此答案适用于在设计时使用 DataSet 连接到数据库的 Windows 窗体。 示例名称为 DataSet1,示例表名称为 Customer_Info。

// cast the data bound item to DataRowView so you have
// access to "Row", which
// has the actual data for the row in typed fields. 
DataRowView drv = dgv.SelectedRows(0).DataBoundItem as DataRowView;
// run the code and look at the debugger value of drv.Row -- 
// the type will be shown
// which is the type created by the data binding, representing 
// your table or view
//{YourDataSetName.YourTableOrViewType} tmpTableData = drv.Row as {YourDataSetName.YourTableOrViewType};
DataSet1.Customer_InfoRow tmpTableData = drv.Row as DataSet1.Customer_InfoRow;

这个链接就是答案。 我希望我在上面增加了清晰度和示例。 https://social.msdn.microsoft.com/Forums/windows/en-US/f252e395-58e6-4703-ba7b-0740efcbecf3/can-i-convert-the-selected-row-in-a-bound-datagridview- to-a-typed-datarow?forum=winformsdatacontrols

此链接显示以编程方式添加到数据源的数据,而不是从现有数据库中提取该数据。 这让我找到了答案: https : //msdn.microsoft.com/en-us/library/4wszzzc7(v=vs.110).aspx

如果数据集内未使用 tableadapter,则不可能对 datagridview 进行强类型访问。

要在 datagridview 通过绑定源绑定到数据表时访问强类型变量,请执行以下操作:

创建一个新项目。

插入名为 ds1 的数据集和名为 dt99 的数据表,其中列名为 DataColumn1 和 DataColumn2,均为字符串类型。

在此处输入图片说明

在主窗体中添加一个datagridView并绑定到dt99

在此处输入图片说明

使dt99BindingSource连接datagridview和datatable

在此处输入图片说明

为datagridview的Selection Change添加事件处理程序,插入如下代码

private void dataGridView1_SelectionChanged(Object sender, EventArgs e) {

  ds1.dt99Row d= ((ds1.dt99Row)((DataRowView)dt99BindingSource.Current).Row);

  Debug.WriteLine(d.DataColumn1 + " " + d.DataColumn2);

}

现在您有强类型变量(d.DataColumn1 和 d.DataColumn2)来访问在 datagridview 上选择的单元格

另一个有趣的特性是插入到数据集中的数据表提供了一组公共类,在处理数据表时有很大帮助,例如

        private void Form1_Load(Object sender, EventArgs e)
    {
        ds1.dt99.Adddt99Row("K", "B");
        ds1.dt99.Adddt99Row("L", "D");
        ds1.dt99.Adddt99Row("M", "F");
        ds1.dt99.Adddt99Row("N", "H");

        ds1.dt99Row dr = ds1.dt99.Newdt99Row();
        dr.DataColumn1 = "X";
        dr.DataColumn2 = "Y";
        ds1.dt99.Adddt99Row(dr);

    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM