简体   繁体   English

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

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

I have a DataTable bound to a DataGridView.我有一个绑定到 DataGridView 的 DataTable。 I have FullRowSelect enabled in the DGV.我在 DGV 中启用了 FullRowSelect。 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?有没有办法将所选行作为 DataRow 获取,以便我可以对所选行的值进行强类型访问?

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

I'm not sure how to do it w/oa 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;

It is possible by getting following property:可以通过获得以下属性:

this.dataGridView.SelectedRows

One obtains a collection of type: DataGridViewSelectedRowCollection .一个获取类型的集合: DataGridViewSelectedRowCollection It contains items of type: DataGridViewRow .它包含以下类型的项目: 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

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

Try this:试试这个:

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

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.此答案适用于在设计时使用 DataSet 连接到数据库的 Windows 窗体。 Example name is DataSet1, and example table name is Customer_Info.示例名称为 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

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);

    }

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

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