Currently having issues with referencing row data in a databound datatable. The binding is configured as follows: DataGridView => BindingSource => DataTable
The datagridview allows multiple row selection and sorting.
I have a button which initiates some methods based on the selected datagridview rows:
ItemsBindingSource.DataSource = dataSets.Tables["ItemList"];
foreach(DataGridViewRow SelectedItem in datagridview1.SelectedRows)
{
string SelectedItemName = SelectedItem.Cells[1].Value.ToString();
int rowPosition = ItemsBindingSource.Find("Name",SelectedItemName);
RunSomeMethod(dataSets.Tables["ItemList"].Rows[RowPosition]);
}
void RunSomeMethod(DataRow row)
{
// run some code and modify the values of particular cells within the
// row and save the changes
}
My issue is that when I sort the Datagridview, the rowPosition for the selected item no longer matches the row position of the BindingSource's underlying datasource. I need some advice on getting the bindingsource and it's underlying datasource to sync, or perhaps a better way of accomplishing this task altogether. Appreciate any advice.
*EDIT
I have realised I can reference the row based on Primary Key value, example:
foreach(DataGridViewRow selectedItem in datagridview1.SelectedRows)
{
int selectedItemPrimaryKey = selectedItem.Cells[0].value;
RunSomeMethod(dataSets.Tables["ItemList"].Rows.Find(selectedItemPrimaryKey));
}
Not as pretty as I had hoped, but seems to be doing the job. Still open to other suggestions. I am quite new to databinding.