18

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?

Damien
  • 1,463
  • 1
  • 18
  • 30

7 Answers7

28
DataRowView currentDataRowView = (DataRowView)dgv1.CurrentRow.DataBoundItem
DataRow row = currentDataRowView.Row
stuartd
  • 70,509
  • 14
  • 132
  • 163
7

I'm not sure how to do it w/o a BindingSource, here is how to do it with one:

var drv = bindingSoure1.Current as DataRowView;
if (drv != null)
  var row = drv.Row as MyRowType;
H H
  • 263,252
  • 30
  • 330
  • 514
3

It is possible by getting following property:

this.dataGridView.SelectedRows

One obtains a collection of type: DataGridViewSelectedRowCollection. It contains items of type: DataGridViewRow.

Then one can get bounditem with ones own type in following way:

DataGridViewSelectedRowCollection list = this.dataGridViewInventoryRecords.SelectedRows;
MyType selectedItem = (MyType)list[0].DataBoundItem; //[0] ---> first item
Bronek
  • 10,722
  • 2
  • 45
  • 46
1

You should be able to directly cast your selected row into the strongly typed row that was bound to the DataGridView.

Dillie-O
  • 29,277
  • 14
  • 101
  • 140
0

Try this:

DataRow row = gridView1.GetDataRow(gridView1.FocusedRowHandle); `        
if (row != null)
{
     XtraMessageBox.Show(row["ID"].ToString());
}
Broken_Window
  • 2,037
  • 3
  • 21
  • 47
AdamLy
  • 1
  • 1
0

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.

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.

// 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

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

T. Fritz
  • 1
  • 1
0

It is not possible to have strongly typed access to a datagridview if a tableadapter is not being used inside the dataset.

To have access to strongly typed variables when a datagridview is bound to a datatable through a bindingsource, do as follow:

Create a New Project.

Insert a DataSet named ds1 and a DataTable named dt99 with columns named DataColumn1 and DataColumn2, both string type.

enter image description here

Add a datagridView to the main form and bind it to the dt99

enter image description here

So that the dt99BindingSource connects the datagridview and the datatable

enter image description here

Add and event handler for the Selection Change of the datagridview, and insert the following piece of code

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

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

    }
Antonio Leite
  • 460
  • 3
  • 7