Here is a brief explanation of my situation.
I have a updatable WinGrid with a dataset as datasource.
When a row is updated (ie: UpdateMode.OnRowChangeOrLostFocus),
I need to send the updated row to a remote server for validations and save to database.
If validations are not OK, I want the user to stay on the same row, so he can either change invalid values or cancel his changes (ESC).
If validations and save OK, then the Grid DataRow is commited (datarow.AcceptChanges), so the Grid dataset come back to an unchanged state.
At all time, i want to have only ONE row which contains modifications. I don't want the user to be able to leave a "Modified & Invalid" row.
Now my question.
I'm not sure in which WinGrid events to send the updated row to the server.
In the BeforeRowUpdate, the Grid row changes are not yet "written" to the datarow (ie: modified row are still RowState = "Unchanged" and added row are still "Detached"). The server process needs the correct RowState to save the changes to database.
In the AfterRowUpdate, the datarow state is updated as i need, and saved correctly on server. However, the problem is when there is some validation that doesn't pass, since we are in the AfterRowUpdate event, it cannot be cancelled... so i cannot force the user to stay in the invalid row to change his values or cancel his modification.
So neither events work as I would like.
Anybody have ideas how I could achieve this ?
I did manage to do what I want using the BeforeRowUpdate in the following way:
public void Grid_BeforeRowUpdate(object sender, CancelableRowEventArgs e) { // Cancel all Grid events and update the datasource row Grid.EventManager.AllEventsEnabled = false; gridRow.Update(); Grid.EventManager.AllEventsEnabled = true; // ... Send the updated datarow to server ... if (!validationOK) { // Simulate a change in any cell so the Row.DataChanged is set back to true string s = gridRow.Cells[0].Text; gridRow.Cells[0].Value = null; gridRow.Cells[0].Value = s; // Cancel the event se the user stay on the row e.Cancel = true; return; } //... }
However, I am not sure how robust is this solution and if there is some better alternative.
Thanks,
Guillaume.
Hi Guillaume,
Data access in DotNet is really not set up to do what you want here. It's really set up to do batch updating. So the grid doesn't really have an event for what you want.
I think you would probably have to use the AfterRowUpdate event and if the data fails to validate, you would have to reload the data somehow into the DataSet to update the grid.
Hi Mike,
Do you think my working solution may be too hazardous, ie: is not supported behavior and may stop working in future version ?
Btw, is there a simpler way to programmaticaly set the row DataChanged property ?
Ok, if it's better to use the AfterRowChange event, instead of trying to prevent the user of leaving the grid row, i could prevent him of modifying other rows, when modifications are pending. Then on the OnAfterRowCancelUpdate of the modified row, I would simply reject the changes of the datarow, to put back the initial values in the grid.