DataGrid Referesh not working "'Refresh' is not allowed during an AddNew or EditItem transaction."
-
Monday, August 20, 2012 7:00 PM
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
-
Tuesday, August 21, 2012 7:06 AM
you do not implement correct validation on DataGrid, please refer to this thread to implement your validation:
http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/90e71120-c09f-4519-ab74-5dedb16e88e5
http://msdn.microsoft.com/en-us/library/ee622975.aspx
http://www.codeproject.com/Articles/30905/WPF-DataGrid-Practical-Examples#valid_dataset
Stay hungry, stay foolish
- Marked As Answer by Sheldon _XiaoModerator Tuesday, September 04, 2012 9:08 AM
-
Wednesday, August 22, 2012 5:58 AMModerator
Hi Dami,
How about your issue, if it persists, could you share me a simple sample to repro this issue.
best regards,
Sheldon _Xiao[MSFT]
MSDN Community Support | Feedback to us
Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.

