Articles in this section
Category / Section

How to delete the cell value by using the delete key in display mode in WinRT ?

4 mins read

In the SfDataGrid, you can delete the selected row by pressing the Delete key when the SfDataGrid.AllowDeleting is set to true. Likewise, you can delete the selected cell value without entering into the edit mode when the delete key is pressed by deriving a new class from the GridSelectionController. In the customized GridSelectionController, you need to override the ProcessKeyDown virtual method that handles all the key navigations. With the help of the PropertyDescriptorExtensions.SetValue helper method, you can set the cell value as null. Before setting the value, you need to check whether the selected cell value is null as follows.

C#

public class GridSelectionControllerExt : GridSelectionController
{        
    public GridSelectionControllerExt(SfDataGrid grid)
        : base(grid)
    {            
    }
    protected override void ProcessKeyDown(KeyEventArgs args)
    {
        //Customizes the Delete key operation.
        if (args.Key == Key.Delete)
        {
            //Gets the cell value of current column.
            var record = this.DataGrid.View.Records[this.DataGrid.SelectedIndex].Data;                               
            var cellVal = this.DataGrid.View.GetPropertyAccessProvider().GetValue(record, this.DataGrid.Columns[this.CurrentCellManager.CurrentCell.ColumnIndex].MappingName);
            //Returns the cell value when the current column's cell is not set to null.
            if (cellVal != null)
            {
                var columnName = this.DataGrid.Columns[this.CurrentCellManager.CurrentCell.ColumnIndex].MappingName;               PropertyDescriptorExtensions.SetValue(this.DataGrid.View.GetItemProperties(), record, null, columnName);
            }
        }
        else
            base.ProcessKeyDown(args);
    }       
}

 

Refer to the following code example to assign the customized GridSelectionControllerExt to the SfDataGrid.SelectionController to override the existing SelectionController.

C#

public MainWindow()
{
    InitializeComponent();
    // Assigns the customized GridSelectionControllerExt.
    this.grid.SelectionController = new GridSelectionControllerExt(grid);
}

 

Sample Links:

WPF

WinRT

WP

UWP


Conclusion

I hope you enjoyed learning about how to delete the cell value by using the delete key in display mode in WinRT.

You can refer to our WinRT DataGrid featuretour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our WinRT DataGrid example to understand how to create and manipulate data.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forumsDirect-Trac, or feedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied