0
votes

I'm using a DataForm in Silverlight 4. Several DataForms, actually, and they share a common RIA Services context. Here's the problem I'm having...

If I start editing a DataField in one of the DataForms, then click on a button that calls Context.SubmitChanges(), I get the following error:

�Entity 'foo' is currently being edited and has uncommitted changes. 
  A call to BeginEdit must be followed by a call to EndEdit or CancelEdit 
  before changes can be submitted.� 

Note that the "Submit Changes" button is external to the DataForms and applies to the RIA Services context that is shared between the DataForms (which are each in separate tabs of a tab control).

I'd like to be able to avoid this problem by detecting, in code, when a DataForm is in editing mode. When I'm in debug mode, I can see that the DataForm has properties such as IsEditing and CanCancelEdit that indicate that it is in editing mode, but these properties do not seem to be available for use in my code.

Any ideas?

5

5 Answers

3
votes

You can call DataForm.CommitEdit() before calling DomainContext.SubmitChanges() and avoid the error entirely.

3
votes

I used the CommitEdit method on my datagrid (should be the same on DataForm) before I called SubmitChanges() in order to fix the problem with a one liner.

myDataGrid.CommitEdit(DataGridEditingUnit.Row, true);
1
votes

I'm not sure what IsEditing and CanCancelEdit are, they aren't documented.

However there is a Mode property that is DataFormMode enumeration, ReadOnly, Edit and AddNew. There is also an IsItemChanged property which indicates if any actual changes have been made editing began.

0
votes

Since none of the available properties provided the information I needed, I ended up creating my own private field (in the UserControl that contains the DataForm) called _isEditing which is initialized to "false". In the DataForm.BeginningEdit event handler I set it to "true" and in the DataForm.EditEnded event handler I set it to "false". I then created a read-only public property called IsEditing to make the value available externally.

0
votes

I used this code just now to solve the issue I was having. This is located in my codebehind, when the Save button is clicked:

        bool editresult = true;
        this.FindChildrenByType<DataGrid>().ToList().ForEach(grid =>
        {
            editresult = editresult && grid.CommitEdit(DataGridEditingUnit.Row, true) && grid.IsValid;
        });

        if (!editresult)
        {
            _messager.Alert("There is a problem with one of the items shown on the screen. This will usually be highlighted in red.\r\nPlease correct the item(s) and try saving again.");
        }
        else
        {
            // Save changes
        }

DataGrid.CommitEdit(DataGridEditingUnit.Row, true) tells the datagrid to commit the row, the last boolean tells the grid to exit editing mode. This will return a true or false depending on whether the row should have exited editing mode. Normally in a grid where you are editing a row and there are validation errors, the grid won't let you exit editing mode by clicking another item, but this call will allow exit but return false. The DataGrid.IsValid checks all items in the grid to see if they are valid.