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
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

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

Platform: WinRT |
Control: SfDataGrid
Tags: editing

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

ADD COMMENT
You must log in to leave a comment

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Up arrow icon

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile