Articles in this section
Category / Section

How to customize the Edit mode behavior of GridNumericColumn's Cell in WinRT DataGrid?

2 mins read

In SfDataGrid, you can edit the GridNumericColumn cell by pressing any key as input from the keyboard. But you cannot edit the GridNumericColumn cell by pressing the Minus sign (-) key. You can overcome this by deriving the GridCellNumericRenderer and override its ShouldGridTryToHandleKeyDown () method.

The following code example illustrates, how to derive the GridNumericColumn’s renderer and override the ShouldGridTryToHandleKeyDown () method in SfDataGrid.

C#

public class GridCellNumericRendererExt : GridCellNumericRenderer
{
    //ShouldGridTryToHandleKeyDown () is responsible for all key navigation associated with GridNumericColumn.
    protected override bool ShouldGridTryToHandleKeyDown(Windows.UI.Xaml.Input.KeyRoutedEventArgs e)
    {
        //Check whether the GridNumericColumn's cell is not already in Edit mode 
        if (!IsInEditing)
        {
            //Edit mode will be based on the ProcessPreviewTextInput() method 
            ProcessPreviewTextInput(e);
            if (!(CurrentCellRendererElement is SfNumericTextBox))
                return true;
        }       
        return base.ShouldGridTryToHandleKeyDown(e);
    }

In the above code example, the ShouldGridTryToHandleKeyDown () method is fired for all key navigations, associated with GridNumericColumn. GridNumericColumn processes the input text in ProcessPreviewTextInput () method. You can customize the editing behavior of GridNumericColumn by specifying the required conditions within it.

The following code example illustrates the default behavior of ProcessPreviewTextInput ().

C#

private void ProcessPreviewTextInput(Windows.UI.Xaml.Input.KeyRoutedEventArgs e)
{    
    //Here you can customize the edit mode behavior of GridNumericColumn while pressing the key from the Keyboard.
    if ((!char.IsLetterOrDigit(e.Key.ToString(), 0) || !DataGrid.AllowEditing || DataGrid.NavigationMode != NavigationMode.Cell) || CheckControlKeyPressed() || (!(e.Key >= VirtualKey.A && e.Key <= VirtualKey.Z) && !(e.Key >= VirtualKey.Number0 && e.Key <= VirtualKey.Number9) && !(e.Key >= VirtualKey.NumberPad0 && e.Key <= VirtualKey.NumberPad9)))
        return;
    if (DataGrid.SelectionController.CurrentCellManager.BeginEdit())
        PreviewTextInput(e);
}

In the above code example, editing is allowed when the pressed key value is equal to letters or numbers or numberpad otherwise, editing is skipped.

You can allow Edit mode for Minus sign (-) key by skipping its default condition as shown in the following code example.

C#

private void ProcessPreviewTextInput(Windows.UI.Xaml.Input.KeyRoutedEventArgs e)
{
    //Here you can customize the edit mode behavior whether it is based on letters or digits or any key
    if ((!char.IsLetterOrDigit(e.Key.ToString(), 0) || !DataGrid.AllowEditing || DataGrid.NavigationMode != NavigationMode.Cell) || CheckControlKeyPressed() || (e.Key==VirtualKey.F2))
        return;
    //The Editing for current cell of GridNuermicColumn is processed here.
    if (DataGrid.SelectionController.CurrentCellManager.BeginEdit())
        PreviewTextInput(e);
}

You can refer to the following code example to remove default the GridCellNumericRenderer and add the customized GridCellNumericRendererExt to the CellRenderers collection in SfDataGrid.

C#

public MainPage()
{           
    this.InitializeComponent();
    //Key argument for removing renderer of GridNumericColumn is "Numeric"
    sfdatagrid.CellRenderers.Remove("Numeric");
    //Customized Renderer is added in to SfDataGrid.CellRenderers collection
    sfdatagrid.CellRenderers.Add("Numeric", new GridCellNumericRendererExt());           
}

 

Sample Link: 

WRT

UWP

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