I have a grid that is bound to a DataTable via a DataAdapter.
After applying updates to the table I spin through the table rows checking to see if any of the rows failed to update. If they did then I copy the error message to the Tag of the corresponding grid row so that I can display it when that row becomes active.
This works fine if the data remains in its original order.If the user sorts the data grid however it seems that the indexes of the the grid are no longer the same as the indexes in the underlying datatable.The error messages get assigned to the wrong grid rows.
I though sorting effected only the display - not the index required to reference the grid rows.How do I get the sorted index from the original (as in the underlying table) index?
My code is as follows:
adapter.Update(table)
Dim hasErrors As BooleanFor r As Integer = 0 To table.Rows.Count - 1 If table.Rows(r).RowError = String.Empty Then 'Remove the color that indicates pending changes Grid.Rows(r).Tag = Nothing Grid.Rows(r).Appearance.BackColor = Grid.DisplayLayout.Appearance.BackColor Else 'Add the error message to what we will display for the row hasErrors = True Grid.Rows(r).Tag = table.Rows(r).RowError() End IfNext
Hi,
Sorting the rows in the grid will change the indices of those rows. That is the intended behavior.
It's very hard to get the grid row from the table row, but it's very easy to get the table row from the grid row using the ListObject property. So what I would do is use the InitializeRow event of the grid and use e.Row.ListObject. It will probably return a DataRowView and you can use the Row property on that to get the actual DataRow from your table.
I don't see how the InitializeRow event figures into trapping errors from an Update.
However your explaination of the ListObject property allowed me to change the code as follows:
This seems to work correctly.
Thanks