C# 如何从 DataGridView 中的一行获取 DataRow

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1822314/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 21:07:26  来源:igfitidea点击:

How do I get a DataRow from a row in a DataGridView

c#winformsdatagridviewdatatable

提问by Dan Is Fiddling By Firelight

I'm using a databound Windows Forms DataGridView. how do I go from a user selected row in the DataGridViewto the DataRowof the DataTablethat is its source?

我正在使用数据绑定 Windows Forms DataGridView。如何从用户选择的行走在DataGridViewDataRowDataTable是它的来源?

采纳答案by Neil Barnwell

DataRow row = ((DataRowView)DataGridViewRow.DataBoundItem).Row

Assuming you've bound an ordinary DataTable.

假设你绑定了一个普通的DataTable.

MyTypedDataRow row = (MyTypedDataRow)((DataRowView)DataGridViewRow.DataBoundItem).Row

Assuming you've bound a typed datatable.

假设您已经绑定了一个类型化的数据表。

See the article on MSDNfor more information.

有关更多信息,请参阅MSDN 上文章

回答by Captain Comic

In a DataGridViewRowis a property called DataBoundItemof type object.

在 a 中DataGridViewRow是一个称为DataBoundItemobject 类型的属性。

This will contain a DataRowView(for certainty you can check this)

这将包含一个DataRowView(为了确定你可以检查这个)

回答by Lev Z

DataTable table = grdMyGrid.DataSource as DataTable;
DataRow row = table.NewRow();
row = ((DataRowView)grdMyGrid.SelectedRows[0].DataBoundItem).Row;

回答by Pallavi

In Visual Studio 2017 .NET 4.5, I had success with

在 Visual Studio 2017 .NET 4.5 中,我取得了成功

 var row = (DataRowView) e.Row.DataItem;