DataGrid Referesh not working "'Refresh' is not allowed during an AddNew or EditItem transaction."

Answered DataGrid Referesh not working "'Refresh' is not allowed during an AddNew or EditItem transaction."

  • Monday, August 20, 2012 7:00 PM
     
      Has Code

    Hi,

    I have tried every solution available on internet for this problem but to no avail. I'll be really grateful for help. 

    My datagrid has only one column. I have created a CollectionViewSource in xaml  as a resource which is bound to codebehind observablecollection of my data. I am then setting this CollectionViewSource as StaticResource to ItemsSource of datagrid and to CurrentCollection of Validation rule. I am then using this CurrentCollection of Validation rule in code behind of ValidationRule to check for uniqueness.

    When I delete a row I need to refresh DataGrid to update row numbers and to remove/re-evaluate validation errors if any. But I get this error when I refresh the DataGrid in code behind.

    I also tried resetting ItemsSource as follows. It refreshes the grid but then breaks at the routine which I was using to re-validate the grid forcefully. 

                 

    // 1. refresh the grid

     myDatagrid.ItemsSource = null;                Binding myBinding = new Binding();  myBinding.Source = this.FindResource("MyCollectionViewSource"); myDataGrid.SetBinding(DataGrid.ItemsSourceProperty, myBinding);


    Here is the routine in my class drived from datagrid for validating forcefully.

    public void ForceValidation(Object obj)        
    {            
       if (obj == null)                
         return;            
       DataGridRow row =                               (DataGridRow)this.ItemContainerGenerator.ContainerFromItem(obj);
       // row always is null after ItemsSource is reset but don't know why ...
       if (row == null || row.BindingGroup == null)                  
         return;            
       row.BindingGroup.UpdateSources();            
    
       HasError = Validation.GetHasError(row);
    }

    I am calling this in a foreach loop like this:

    // 2. re-evaluate validation errors.

    foreach (dataObjects msg in ListofMyDataObjects)                  myDataGrid.ForceValidation(msg);


    Looking forward to any help.

    Thanks.

    Dami.




    • Edited by Dami Monday, August 20, 2012 8:03 PM
    •  

All Replies