"Cannot change ObservableCollection during a CollectionChanged event." Exception

Hello!

I use the SfDataGrid control in dialog window with MVVM. If I add new row via AddNewRow, then don't press Enter, close Window and open Window again, then I try add new row again and I have exception  "Cannot change ObservableCollection during a CollectionChanged event." As I understand, I need CancelAddNewRow  programmatically before closing window ( https://help.syncfusion.com/wpf/datagrid/data-manipulation#add-row-programmatically) , but maybe is there more convenient way to do it in MVVM style?


6 Replies

DD Dhivyabharathi Dakshinamurthy Syncfusion Team March 11, 2024 05:11 PM UTC

Hi Aksel Nielsen,

 

We are currently analyzing your reported scenario. We need time to validate the scenario, we will provide an update on or before March 13, 2024.



DD Dhivyabharathi Dakshinamurthy Syncfusion Team March 13, 2024 01:46 PM UTC

Hi Aksel Nielsen,

Could you please mention the details of how you are using the dialog window to show the SfDataGrid control.

As of now, we have only two ways to cancel the AddNewRow, programmatically or simply by pressing the Esc key twice.



AN Aksel Nielsen March 29, 2024 09:18 AM UTC

Hello again. I created example and recorded video. And do next steps:

  1. I open Dialog Window: 2. Then begin adding new row. 3.  then I don't finish adding new row and  close window 3. open Window again and add new row, then press enter.

For example, I can forget to press Esc , so I want to cancel adding new rowprogrammatically, when I close DialogWIndow.


Attachment: datagrid_afad30da.7z


DD Dhivyabharathi Dakshinamurthy Syncfusion Team April 1, 2024 03:10 PM UTC

Hi Aksel Nielsen,

Based on the provided information and the sample you provided, we were able to reproduce the issue.

It seems that in the command for opening the DialogBox, a new instance of DialogWindow is initialized each time, and similarly, a new instance of SfDataGrid is also initialized. However, the data context remains the same instance.

To prevent the issue from occurring, creating a new instance of the viewModel would resolve it.

Additionally, there is currently no other way to cancel the AddNewRow, either by pressing the Esc key or programmatically.



AN Aksel Nielsen April 7, 2024 12:48 PM UTC

I've found the solution myself. Maybe it will be useful for somebody.

I added event handler to Unloaded event of Window (or UC, it works too).

   Unloaded="DialogWindow_OnUnloaded">

 And then I set the DataContext property to null

private void DialogWindow_OnUnloaded(object sender, RoutedEventArgs e)

 {

     ((Window)sender).DataContext = null;

 }




DD Dhivyabharathi Dakshinamurthy Syncfusion Team April 8, 2024 03:22 PM UTC

Hi Aksel Nielsen,


Thanks for the update. 
We are glad to know that the reported problem has been resolved at your end. Please let us know if you have any further queries on this. We are happy to help.


Loader.
Up arrow icon