We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Disable SfDataGrid after Commit New Row

Hi,

I'm trying to achieve this issue but I'm not really sure the best way to approach it and the way I tried seems not working properly.

I got a SfDataGrid that is bind to an ObservableCollection. 2 of the columns are double properties and after commit a new row (or edit one of the already existents) should check an sum of those properties is zero and in that case disable the SfDataGrid and not allow, in that edit moment, add new rows or edit other rows.

I have tried doing that with the CollectionChanged Event on the ObservableCollection (as SfDataGrid adds the new "row" to the collection after is complete) but an exception is thrown in Syncfusion.Data.CollectionViewAdv.CommitNew().

Full StackTrace:

en Syncfusion.Data.CollectionViewAdv.CommitNew() en Syncfusion.UI.Xaml.Grid.GridAddNewRowController.CommitAddNew(Boolean changeState) en Syncfusion.UI.Xaml.Grid.GridSelectionController.CommitAddNew(Boolean changeState) en Syncfusion.UI.Xaml.Grid.GridSelectionController.ProcessKeyDown(KeyEventArgs args) en Syncfusion.UI.Xaml.Grid.GridSelectionController.HandleKeyDown(KeyEventArgs args) en Syncfusion.UI.Xaml.Grid.SfDataGrid.OnPreviewKeyDown(KeyEventArgs e) en System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target) en System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs) en System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised) en System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args) en System.Windows.UIElement.RaiseTrustedEvent(RoutedEventArgs args) en System.Windows.Input.InputManager.ProcessStagingArea() en System.Windows.Input.InputManager.ProcessInput(InputEventArgs input) en System.Windows.Input.InputProviderSite.ReportInput(InputReport inputReport) en System.Windows.Interop.HwndKeyboardInputProvider.ReportInput(IntPtr hwnd, InputMode mode, Int32 timestamp, RawKeyboardActions actions, Int32 scanCode, Boolean isExtendedKey, Boolean isSystemKey, Int32 virtualKey) en System.Windows.Interop.HwndKeyboardInputProvider.ProcessKeyAction(MSG& msg, Boolean& handled) en System.Windows.Interop.HwndSource.CriticalTranslateAccelerator(MSG& msg, ModifierKeys modifiers) en System.Windows.Interop.HwndSource.OnPreprocessMessage(Object param) en System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs) en System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Int32 numArgs, Delegate catchHandler)

Is there a way to approach this using SfDataGrid Events?

Thanks in advance.

3 Replies

GT Gnanasownthari Thirugnanam Syncfusion Team February 6, 2017 11:52 AM UTC

Hi David, 

We have analyzed your query, you can achieve your requirement “disable the SfDataGrid while adding new row or editing existing row (Sum of two properties is 0)” in below mentioned ways. 

Query 1: Disable SfDataGrid while Editing existing row. 

While editing existing row you can able to cancel the commit action and set the SfDataGrid.AllowEditing property as false for disable editing action in SfDataGrid. You can achieve this requirement by using CurrentCellValidatedEvent of SfDataGrid. 

Query 2: Disable SfDataGrid while Adding new row. 
 
You can cancel the AddNewRow by using CancelAddNew( ) method as like below code example. 

C# 
private void Datagrid_CurrentCellValidated(object sender, CurrentCellValidatedEventArgs e) 
    var dataGrid = e.OriginalSender as SfDataGrid; 
    var record = (e.RowData) as OrderInfo; 
           
    var total = record.OrderID + record.Amount;             
    if (total == 0 && this.datagrid.View.CurrentEditItem!= null && !datagrid.View.IsAddingNew) 
    { 
        //cancel the EndEdit while sum of two properties is zero            
        this.datagrid.View.CancelEdit(); 
        //disable the editing mode in SfdataGrid while sum of two properties is zero 
        this.datagrid.AllowEditing = false;                 
    } 
    else if (datagrid.View.IsAddingNew) 
    { 
        var addNewRecord = this.datagrid.GetRecordAtRowIndex(this.datagrid.SelectionController.CurrentCellManager.CurrentRowColumnIndex.RowIndex) as OrderInfo; 
        var addNewRowTotal = addNewRecord.OrderID + addNewRecord.Amount; 
        if (addNewRowTotal == 0) 
        { 
            //cancel the EndEdit while sum of two properties is zero for AddNewRowControl 
            datagrid.GetAddNewRowController().CancelAddNew(); 
            //disable the editing mode in SfdataGrid while sum of two properties is zero for AddNewControl 
            this.datagrid.AllowEditing = false; 
        }                   
    }            
}        

We have prepared the sample based on your requirement, you can download the same from below mentioned location. 

Sample location: 

Note: You must implement the IEditable interface in your BuisnessObject Model class to achieve above requirement. 

Refer the below UG link for implementing IEditableObject interface. 

Regards, 
Gnanasownthari T. 
 



DG David García February 8, 2017 11:58 AM UTC

Hi,

Thank you so much for the example provided and the guide to achieve my goal, but that is not exactly what I'm trying to do. The sum or calculation that determine when the SfDataGrid has been disabled is not based in just one row but in the whole collection bind to the SfDataGrid. That is why I tried to use the CollectionChanged event of the ObservableCollection what apparently works ok for my goal but after disable the SfDataGrid (and not during it) I got a NullReferenceException when the SfDataGrid calls the method Syncfusion.Data.CollectionViewAdv.CommitNew().

I guess is because Syncfusion.Data.CollectionViewAdv is null but why is it null? The business objects are persisted into a database, can be possible that because when the object is persisted part of the object is changed (like the Key as is autogenerated). I tried to replicate the exception in the example provided but I wasn't able to.

en Syncfusion.Data.CollectionViewAdv.CommitNew() en Syncfusion.UI.Xaml.Grid.GridAddNewRowController.CommitAddNew(Boolean changeState) en Syncfusion.UI.Xaml.Grid.GridSelectionController.CommitAddNew(Boolean changeState) en Syncfusion.UI.Xaml.Grid.GridSelectionController.ProcessKeyDown(KeyEventArgs args) en Syncfusion.UI.Xaml.Grid.GridSelectionController.HandleKeyDown(KeyEventArgs args) en Syncfusion.UI.Xaml.Grid.SfDataGrid.OnPreviewKeyDown(KeyEventArgs e) en System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target) en System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs) en System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised) en System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args) en System.Windows.UIElement.RaiseTrustedEvent(RoutedEventArgs args) en System.Windows.Input.InputManager.ProcessStagingArea() en System.Windows.Input.InputManager.ProcessInput(InputEventArgs input) en System.Windows.Input.InputProviderSite.ReportInput(InputReport inputReport) en System.Windows.Interop.HwndKeyboardInputProvider.ReportInput(IntPtr hwnd, InputMode mode, Int32 timestamp, RawKeyboardActions actions, Int32 scanCode, Boolean isExtendedKey, Boolean isSystemKey, Int32 virtualKey) en System.Windows.Interop.HwndKeyboardInputProvider.ProcessKeyAction(MSG& msg, Boolean& handled) en System.Windows.Interop.HwndSource.CriticalTranslateAccelerator(MSG& msg, ModifierKeys modifiers) en System.Windows.Interop.HwndSource.OnPreprocessMessage(Object param) en System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs) en System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Int32 numArgs, Delegate catchHandler)

Once again thanks.


GT Gnanasownthari Thirugnanam Syncfusion Team February 10, 2017 04:07 AM UTC

Hi David, 
 
We have analyzed your query and tested the reported issue, “Disable the SfDataGrid while adding new row or editing existing row (Sum of two properties is 0),”  in CollectionChanged event in SfDataGrid at our end. The reported issue is not reproduced. NullReferenceException is not thrown, its works fine at our end.

Please refer to the sample and video from the following location: 
 
Sample location: 
 
Video Location :  
 
If you are still facing the issue, could you please send us the modified sample to replicate this issue which will help us to provide you the better solution. 
 
Regards, 
Gnanasownthari T. 


Loader.
Up arrow icon