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
Hello,
To learn more about the InitializeRow Event, please visit the online documentation found here. Please let us know if you have additional questions regarding the InitializeRow event
I guess maybe I just don't understand what you are trying to do. :)
I don't have anything to check when the contents of a cell changed by the user.(Actually I do already check that the new value can be parsed into the underlying table column data type, but that is a 'local' change)
The loop is for after Update() is called on the data adapter.If the database returns errors I need to assign those messages to the appropriate row.That has nothing to do with changes to the cells contents. (They changed earlier)
Or are you saying that the InitializeRow event fires when the underlying data rows FAIL to commit a change?
Hi Mike,
mikedempsey said:I don't see how the InitializeRow event figures into trapping errors from an Update.
InitializeRow will fire any time any cell in the row has been changed. So it's a lot more efficient to use InitializeRow to set the Tag or do whatever you want to do with the row than it is to loop through every tow in the grid every time you make a change.
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