שתף באמצעות


Determine a DataTable Row index from a DataGridView

Question

Sunday, May 7, 2006 2:20 PM

I have a datatable with 10 records linked to a datagridview. When i intially load the datagridview, row 0 in the datagridview = row 0 in the datatable - based purely on the order they load, and the datatable's default sort order.

I then filter my datagridview and am left with 5 records.

Is there a way of determining programatically, the actual datatable row number of a row displayed in a datagridview, considering that row 0 in the datagridview is now not necessarily row 0 in the datatable?

Cheers

Mc

All replies (6)

Wednesday, May 10, 2006 1:52 AM ✅Answered

Thanks Spotty

Didn't end up using this, but it did wake up my ailing brain.

I ended up creating a datarow object for each row in the gridview, thus enabling me to create a new datatable based on a gridview simply by adding the newly created rows to it, which was the whole point of the exercise.

I've stored the databounditem docs away in my folders... it will probably be useful in the future :)

Cheers

Mc


Sunday, May 7, 2006 8:19 PM | 1 vote

It may or may not be what you want but the

DataGridViewRow.DataBoundItem Property
http://msdn2.microsoft.com/en-us/library/system.windows.forms.datagridviewrow.databounditem.aspx

may be what you can use.   This enables you to Get the data-bound object that populated the row.   ie.  The row in the datatable.    So even when you filter the datagridview - you can still get access to the datatable row members.

As this provides you with a reference from the datagridview row to the datatable row used for the databinding, you can get access to each of the fields in the source datatable - even for those fields that exist in the datatable that are not used in the datagridview.

So this same approach will work and enable you to link the datagridview rows with the data source rows whether the datagridview is filtered or not and does not depend upon matching index values.

 

 


Wednesday, November 1, 2006 11:14 PM

I know you found a different solution but possibly this may be of help..

// SETS THE DATASET PRIMARY KEY
DataCoulumn[] keys = new DataColumn[1];
keys[0] = DataSet.Tables["mytable"].Column["i_ID"];
DataSet.Tables["mytable"].PrimaryKey = keys;

// THIS IS FOR THE DATAGRID VIEW
BindingManagerBase bm;
DataGridView1.DataSource = DataSet.Tables["mytable"].DefaultView;
bm = this.DataGridView1.BindingContext[this.DataGridView1.DataSource, this. DataGridView1.DataMember];

//TO FIND INDEX OF ROW SELECTED
DataRow findRow = DataSet.Tables["mytable"].Rows.Find(((DataRowView)bm.Current).Row[0]); // Row[0] is the collumn to seach in in this case ID column
int indexNumber = DataSet.Tables["mytable"].Rows.IndexOF(findRow);

And There you have it.
The indexNumber should be your index in the dataset regardless of Sorting DataView...

I've Tested this and seems to work for me.. Tell me what you think. I'm sure the coding can be cleaned up a bit but it works..


Tuesday, November 6, 2007 10:38 AM | 1 vote

Or try this much simpler code:

 

Dim DGV As System.Windows.Forms.DataGridView

Dim index As Integer

'Dim MyTable As DataTable = CType(DGV.DataSource, DataTable)

'Dim MyRow As System.Windows.Forms.DataGridViewRow = DGV.CurrentRow

'Dim obj As Object = MyRow.DataBoundItem

'Dim MyDataRowView As DataRowView = CType(obj, DataRowView)

'Dim MyDataRow As DataRow = MyDataRowView.Row

'All the above in one line:

index = CType(DGV.DataSource, DataTable).Rows.IndexOf(CType(DGV.CurrentRow.DataBoundItem, DataRowView).Row)

 

It works fine!

 


Wednesday, July 23, 2008 7:34 PM

ok..I tried the simpler code did not work...here is my version of the code

TheCurrentRow = CType(ssDGV.DataSource, DataTable).Rows.IndexOf(CType(ssDGV.CurrentRow.DataBoundItem, DataRowView).Row)

The error I get is 
Unable to cast object of type 'System.Windows.Forms.BindingSource' to type 'System.Data.DataTable'

 

I'm using a binding source....to bind the data


Monday, April 23, 2012 7:05 PM

It worked fine..

C#

RowIndex = ((

DataTable) dgvItem.DataSource).Rows.IndexOf(((DataRowView) dgvItem.Rows[e.RowIndex].DataBoundItem).Row);