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
close icon

Previewing minus sign immediately in GridNumericColumn

Hi,

When editing in GridNumericColumn the minus sign is not immediately shown. I am aware of previous threads and solutions such as these:

Start editing not only with numbers, but also with minus sign | WPF Forums | Syncfusion

Customize the edit mode behavior of cells | WPF - SfDataGrid (syncfusion.com)

Editing in WPF DataGrid control | Syncfusion


BUT ​they are only part of the solution because it works for GridTextColumn but not for ​GridNumericColumn.


Reproduction steps:

  1. Click on a cell in numeric column (do not enter edit mode).
  2. Type '-'.
  3. Note that "0.00" is shown in the cell.


Expected behavior:

  1. Click on a cell in numeric column (do not enter edit mode).
  2. Type '-'.
  3. Minus sign should be shown in the cell.


Regards

Carlo


3 Replies 1 reply marked as answer

VS Vijayarasan Sivanandham Syncfusion Team February 21, 2023 02:51 PM UTC

Hi Carlo da Veiga Monteiro,

As you said in the previous update, entering the edit mode via minus in SfDataGrid can be achieved by customizing the SfDataGrid class and overriding its OnTextInput method. For more information related to entering edit via minus key, please refer to the below user guide documentation link,

UG Link: https://help.syncfusion.com/wpf/datagrid/editing#allow-editing-when-pressing-minus-key

KB Link: https://www.syncfusion.com/kb/3823/how-to-customize-edit-mode-behavior-of-gridcell-in-sfdatagrid

However, your requirement to display a minus key value while the enter edit mode in SfDataGrid can be achieved by overriding the GridCellNumericRenderer and customizing the OnEditElementLoaded method. Please refer to the below code snippet,

//Removes Default renderer.

sfDataGrid.CellRenderers.Remove("Numeric");

//Adds Custom renderer.

sfDataGrid.CellRenderers.Add("Numeric", new GridCellNumericRendererExt());

 

 

public class GridCellNumericRendererExt : GridCellNumericRenderer

 {

     protected override void OnEditElementLoaded(object sender, RoutedEventArgs e)

     {

         var uiElement = ((DoubleTextBox)sender);

         uiElement.ValueChanged += OnValueChanged;

 

         uiElement.Focus();

 

         if (uiElement.IsReadOnly)

         {

             PreviewInputText = null;

             uiElement.SelectAll();

             return;

         }

 

         if ((this.DataGrid.EditorSelectionBehavior == EditorSelectionBehavior.SelectAll || this.DataGrid.IsAddNewIndex(this.CurrentCellInde

         {

             uiElement.SelectAll();

         }

         else

         {

             if (PreviewInputText == null || char.IsLetter(PreviewInputText.ToString(), 0))

             {

                 var index = uiElement.Text.Length;

                 uiElement.Select(index + 1, 0);

                 return;

             }

             double value;

             double.TryParse(PreviewInputText.ToString(), out value);

             uiElement.Value = value;

             uiElement.Text = PreviewInputText.ToString();

             var caretIndex = uiElement.Text.IndexOf(PreviewInputText.ToString(CultureInfo.InvariantCulture), StringComparison.Ordinal);

             uiElement.Select(caretIndex + 1, 0);

         }

         PreviewInputText = null;

     }

 

     private void OnValueChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)

     {

         base.CurrentRendererValueChanged();

     }

 

     protected override void OnWireEditUIElement(DoubleTextBox uiElement)

     {

         uiElement.ValueChanged -= OnValueChanged;

     }

 }


UG Link: https://help.syncfusion.com/wpf/datagrid/column-types#customize-column-renderer

Find the sample in the attachment.


Regards,

Vijayarasan S


If this post is helpful, please consider Accepting it as the solution so that other members can locate it more quickly.


Attachment: Sample_1719fe16.zip

Marked as answer

CD Carlo da Veiga Monteiro replied to Vijayarasan Sivanandham February 21, 2023 03:07 PM UTC

Hi Vijayarasan Sivanandham,

It works great. Thank you for the fast response and solution!


Regards,

Carlo



VS Vijayarasan Sivanandham Syncfusion Team February 22, 2023 06:45 AM UTC

Carlo da Veiga Monteiro,

We are glad to know that the reported problem has been resolved at your end. Please let us know if you have any further queries on this. We are happy to help you😊.


Loader.
Live Chat Icon For mobile
Up arrow icon