Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
685
Updatable WinGrid: datasource validations & save on remote server
posted

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. 

Parents Reply Children
No Data