Find rowindex in dataset based on datarow, Set primary key to dataset

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

// To Bind in THE DATAGRID VIEW
BindingManagerBase bm;
DataGridView1.DataSource = DataSet.Tables["mytable"].DefaultView;
bm = this.DataGridView1.BindingContext[this.DataGridView1.DataSource, this. DataGridView1.DataMember];

//You have a datarow and u need to find the rowindex of the row in dataset
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);

 

1 comments:

Loic 87 July 22, 2011 at 3:00 AM  

Hi Gregor,
thinks a lot for this sample
IndexOf is powerfull
Loic Brengues

Listen to nirmaln - sakara - playlist audio songs at MusicMazaa.com