Archive for August, 2009

WPF datagrid Not allowing to edit after validation errors

August 21, 2009 2 comments

Most common issue which has been raised frequently about wpf datagrid editing on validation errors.
Scenario to use following code is :- Whenever user enters a value which fires some validations and user try to reset data in datagrid by hitting reset or user try to rollback the data to previous state.Datagrid still remain in uneditable even after valid data has reseted for datagrid.Reason behind failure is, Datagrid maintains class level Private variable _hasCellValidation which is set whenever there are cell level validations on datagrid.Even though we try to reset data to previous state,Datagrid still remains hascellValidation = true.if it is true,it doesnot allow us to edit grid.if it if false it allows us to edit.
Following is reflection code which allows u to edit datagrid whenever validation fired up on resetting data.
Another Work Around can be Rebinding ItemSource again back to datagrid.
Im having requirement that whenever user edits datagrid cell and enters value in a datagrid
  public void ResetDatagrid()
            Type DttagridType = this.GetType().BaseType;
            PropertyInfo HasCellValidationProperty = DatagridType.GetProperty("HasCellValidationError", BindingFlags.NonPublic | BindingFlags.Instance);
            if (HasCellValidationProperty != null)
                HasCellValidationProperty.SetValue(this, false, null);
I didnot tested code much..Plz copy on ur risk.
WIth Regards,
Categories: C#, WPF Tags:
%d bloggers like this: