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

18

我有一个与DataGridView绑定的DataTable。在DGV中启用了FullRowSelect。是否有一种方法可以将选定的行作为DataRow获取,以便我可以强类型访问所选行的值?

7个回答

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

1
嗯...对我来说,所选行的“DataBoundItem”为空。 - Hi-Angel
2
@Hi-Angel 你没有使用数据绑定。 - stuartd

7

如果没有绑定源,我不确定如何做到这一点,以下是使用绑定源的方法:

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

3
可以通过获取以下属性来实现:

this.dataGridView.SelectedRows

你可以获得一个类型为DataGridViewSelectedRowCollection的集合。它包含DataGridViewRow类型的项目。

然后,你可以通过以下方式获取绑定项:

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

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

0

如果在数据集中没有使用TableAdapter,则无法强制类型访问DataGridView。

当DataGridView通过BindingSource绑定到DataTable时,要访问强类型变量,请按照以下步骤操作:

创建一个新项目。

插入名为ds1的DataSet和名为dt99的DataTable,其中DataColumn1和DataColumn2都是字符串类型的列。

enter image description here

在主窗体中添加一个DataGridView并将其绑定到dt99。

enter image description here

这样dt99BindingSource就连接了DataGridView和DataTable

enter image description here

为 datagridview 添加选择更改的事件处理程序,并插入以下代码: 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);

    }

0

如果您已将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


0

试试这个:

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接