C# 如何在DataGridView中获取选中的DataRow?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/895778/
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 the Selected DataRow in a DataGridView?
提问by Damien
I have a DataTable bound to a DataGridView. I have FullRowSelect enabled in the DGV. Is there a way to get the selected row as a DataRow so that I can get strongly typed access to the selected row's values?
我有一个绑定到 DataGridView 的 DataTable。我在 DGV 中启用了 FullRowSelect。有没有办法将所选行作为 DataRow 获取,以便我可以对所选行的值进行强类型访问?
采纳答案by Henk Holterman
I'm not sure how to do it w/o a BindingSource, here is how to do it with one:
我不知道如何在没有 BindingSource 的情况下做到这一点,以下是如何做到这一点:
var drv = bindingSoure1.Current as DataRowView;
if (drv != null)
var row = drv.Row as MyRowType;
回答by Dillie-O
You should be able to directly cast your selected row into the strongly typed row that was bound to the DataGridView.
您应该能够直接将您选择的行转换为绑定到 DataGridView 的强类型行。
回答by stuartd
DataRowView currentDataRowView = (DataRowView)dgv1.CurrentRow.DataBoundItem
DataRow row = currentDataRowView.Row
回答by Bronek
It is possible by getting following property:
可以通过获得以下属性:
this.dataGridView.SelectedRows
One obtains a collection of type: DataGridViewSelectedRowCollection. It contains items of type: DataGridViewRow.
一个获取类型的集合:DataGridViewSelectedRowCollection。它包含以下类型的项目:DataGridViewRow。
Then one can get bounditem with ones own type in following way:
然后可以通过以下方式获得具有自己类型的 bounditem :
DataGridViewSelectedRowCollection list = this.dataGridViewInventoryRecords.SelectedRows;
MyType selectedItem = (MyType)list[0].DataBoundItem; //[0] ---> first item
回答by AdamLy
Try this:
尝试这个:
DataRow row = gridView1.GetDataRow(gridView1.FocusedRowHandle); `
if (row != null)
{
XtraMessageBox.Show(row["ID"].ToString());
}
回答by T. Fritz
If you have bound your datagridview to a table or view in a database, you can get the data out as a strongly-typed object.
如果您已将 datagridview 绑定到数据库中的表或视图,则可以将数据作为强类型对象取出。
This answer is for a Windows form that connected to a database using a DataSet at design time. Example name is DataSet1, and example table name is Customer_Info.
此答案适用于在设计时使用 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;
This link is the answer. I hope I have added clarity and an example above. 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://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
This link shows data programmatically added to the data source, not pulling that data from an existing database. This got me part of the way to the answer: https://msdn.microsoft.com/en-us/library/4wszzzc7(v=vs.110).aspx
此链接显示以编程方式添加到数据源的数据,而不是从现有数据库中提取该数据。这让我找到了答案:https: //msdn.microsoft.com/en-us/library/4wszzzc7(v=vs.110).aspx
回答by Antonio Leite
It is not possible to have strongly typed access to a datagridview if a tableadapter is not being used inside the dataset.
如果数据集内未使用 tableadapter,则不可能对 datagridview 进行强类型访问。
To have access to strongly typed variables when a datagridview is bound to a datatable through a bindingsource, do as follow:
要在 datagridview 通过绑定源绑定到数据表时访问强类型变量,请执行以下操作:
Create a New Project.
创建一个新项目。
Insert a DataSet named ds1 and a DataTable named dt99 with columns named DataColumn1 and DataColumn2, both string type.
插入名为 ds1 的数据集和名为 dt99 的数据表,其中列名为 DataColumn1 和 DataColumn2,均为字符串类型。
Add a datagridView to the main form and bind it to the dt99
在主窗体中添加一个datagridView并绑定到dt99
So that the dt99BindingSource connects the datagridview and the datatable
使dt99BindingSource连接datagridview和datatable
Add and event handler for the Selection Change of the datagridview, and insert the following piece of code
为datagridview的Selection Change添加事件处理程序,插入如下代码
private void dataGridView1_SelectionChanged(Object sender, EventArgs e) {
private void dataGridView1_SelectionChanged(Object sender, EventArgs e) {
ds1.dt99Row d= ((ds1.dt99Row)((DataRowView)dt99BindingSource.Current).Row);
Debug.WriteLine(d.DataColumn1 + " " + d.DataColumn2);
}
}
Now you have strongly typed variables (d.DataColumn1 and d.DataColumn2) to access the cells being selected on the datagridview
现在您有强类型变量(d.DataColumn1 和 d.DataColumn2)来访问在 datagridview 上选择的单元格
Another interesting feature is that a datatable inserted inside a dataset provides a set of public classes that helps a lot when handling datatables, for example
另一个有趣的特性是插入到数据集中的数据表提供了一组公共类,在处理数据表时有很大帮助,例如
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);
}