I am trying to update an old project that uses the base WPF DataGrid control to Syncfusion and it manages the edit state of a row using two buttons in a template column that are enabled when the row/item is editing. One to commit the row changes and one to cancel.
In the old datagrid you simply call DataGrid.CommitEdit() or DataGrid.CancelEdit() respectively.
I can't seem to find an equivalent for the CancelEdit function on the Syncfusion SfDataGrid, however committing an edit seems to work using Datagrid.SelectionController.CurrentCellManager.EndEdit();
Is there a way of doing this?
Hi Dave,
SfDataGrid does not contain the direct support to cancel the edit by calling the function. However,
you can overcome this behavior by using any one of the below mentioned ways.
Way 1:
SfDataGrid supports committing and rolling back the changes in row level when the underlying
data object implements the IEditableObject interface. For more information related
to working with IEditableObject interface, please refer to the below user guide
documentation link,
UG Link: https://help.syncfusion.com/wpf/datagrid/editing#working-with-ieditableobject-interface
Way 2:
Your requirement to cancel the editing in SfDataGrid can be achieved by using CurrentCellBeginEdit
event. For more information related to CancelEdit, please refer to the below
user guide documentation link,
UG Link: https://help.syncfusion.com/wpf/datagrid/editing#canceledit
If we misunderstood your requirement, please provide more information regarding
the requirement. This would help us to proceed further.
Regards,
Vijayarasan S
I think you've understood my requirement correctly, however the example for CancelEdit using the CurrentCellBeginEdit event in the documentation does not appear to be very clear. It seems to prevent going into edit mode in the first place rather than cancel the existing edit mode. Please correct me if I'm wrong?
I basically need to be able to call a function that essentially emulates the same behaviour as pressing Escape when editing a row/cell.
Hi Dave,
We can understand your scenario. Could you please confirm when did you call the CancelEdit method (ex: Based on which operation for example by clicking the button). Based on your confirmation we will proceed further.
Regards,
Vijayarasan S.
Hi Vijayarasan,
The model the datagrid is bound to uses IEditableObject which also has an IsEditing flag. The datagrid contains a template column with 2 buttons that are enabled when IsEditing is true. The green "check" button tells the datagrid to commit the edit and the red "cross" tells it to cancel the edit so any changes are reverted. See image example below:
Hopefully that clarifies.
Regards,
Dave Taylor.
Hi Dave Taylor,
Your requirement to cancel the edit through the button that loaded inside
GridTemplateColumn can be achieved by calling the CancelEdit method in SfDataGrid.View.
Please refer to the below code snippet,
|
private void OnCancelClicked(object sender, RoutedEventArgs e) { if (this.sfDataGrid.View != null && this.sfDataGrid.View.IsEditingItem) { //here cancel the Editing by calling the CancelEdit method this.sfDataGrid.View.CancelEdit();
//here call the EndEdit for the CurrentCell from editing to Normal sfDataGrid.SelectionController.CurrentCellManager.EndEdit(false);
} } |
Please find the sample in the attachment and let us know if you have any
concerns in this.
Regards,
Vijayarasan S
That seems to have done the trick, thank you! It might be worth updating the documentation with this detail.
Regards,
Dave Taylor.
Hi Dave Taylor,
We have considered your suggestion. If you are satisfied with our response,
please mark it as an answer. Otherwise, please let us know if you have any
further queries on this. We are happy to help you.
Regards,
Vijayarasan S