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
How do I get a DataRow from a row in a DataGridView
提问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 DataGridView
to the DataRow
of the DataTable
that is its source?
我正在使用数据绑定 Windows Forms DataGridView
。如何从用户选择的行走在DataGridView
到DataRow
的DataTable
是它的来源?
采纳答案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.
回答by Captain Comic
In a DataGridViewRow
is a property called DataBoundItem
of type object.
在 a 中DataGridViewRow
是一个称为DataBoundItem
object 类型的属性。
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;