Stop value from rounding in GridNumericColumn

Hello,

we show multiple decimal points in the cell, but how can we stop the value from rounding at all?


Example:

Cell A has a Value of 0,001549, with "gridNumericColumn.NumberDecimalDigits = 6" we show all the decimal points, but when we click into the cell the value gets rounded.

How can we stop this behavior?



7 Replies

VS Vijayarasan Sivanandham Syncfusion Team July 13, 2023 01:57 PM UTC

Hi Harald Stapfer,

We have checked the reported issue with the provided details, but we are unable to reproduce the reported issue “Cell value gets rounded when clicking on the cell in GridNumericColumn” from our end. Here we have attached the tested sample and video demo. Please have a look at this. If you are still facing the reported issue, modify the attached sample based on your scenario. It will be helpful for us to proceed further.

Regards,
Vijayarasan S


Attachment: Sample_And_Video_demo_91a25fa1.zip


HS Harald Stapfer July 18, 2023 06:29 AM UTC

Hello,


we have a very specific usecase for the GridNumericColumn where we load the decimal points dynamic.

Example:

WeightHeight
1,30490,1493134
1,340,1549


The problem is, the numbers get rounded when we click into the column.

So:

When clicking into the Weight column -> 1,3049 gets rounded to 1,31

And when clicking into the Height column -> 0,1493134 gets rounded to 0,15


How can we disable the rounding of the numbers with dynamic decimal points when clicking into the cell?



DM Dhanasekar Mohanraj Syncfusion Team July 19, 2023 12:12 PM UTC

Harald Stapfer,

We have checked the reported scenario with the provided details. Unfortunately, the reported scenario does not reproduce on our end. The decimal digits are displayed properly as expected. Here, we have attached the tested sample and video demo. Please have a look at this. If you are still facing the reported issue, please share the below things,

  1. Details about the underlying DataType for that NumericColumns

  2. And share the GridNumericColumn settings that you are using.

  3. Modify the attached sample based on your scenario and share the video illustration for the reported issue.

It will be helpful for us to check on it and provide you with a solution as soon as possible.


Attachment: Sample__TestedVideo_a67a1dd9.zip


HS Harald Stapfer July 20, 2023 11:07 AM UTC

Hello,

sorry for the confusion.

We only want the show the decimal places that are in the value.


For Example:

If Cell A has a Value of 1045,1849 we want to show 1045,1849

If Cell B has a Value of 1543,159403 we want to show 1543,159403

If Cell C has a Value of 1545,14 we want to show 1545,14

If Cell D has a value of 1543,00 we want to show 1543


And if we click into the Cell to edit the Value the view should not turn the Value into 2 decimal places.



VS Vijayarasan Sivanandham Syncfusion Team July 21, 2023 02:14 PM UTC

Harald Stapfer,

Your requirement to edit the value the view should not turn the value into 2 decimal places in SfDataGrid can be achieved by using DisplayBinding and override the GridCellNumericRenderer. Customize the OnInitializeEditElement method. Refer the below code snippet,


XAML Code snippet:

<Window.Resources>

    <local:DisplayFormatConverter x:Key="displayFormatConverter" />

</Window.Resources>

 

<syncfusion:GridNumericColumn MappingName="OrderID"                                             

                               NumberDecimalSeparator=","

                               HeaderText="Order ID"                                           

                               DisplayBinding="{Binding Path=OrderID, Converter={StaticResource displayFormatConverter}}" />


C# Code snippet related to Converter:

// This class is used to display the value in the cell based on the format specified in the converter.

public class DisplayFormatConverter : IValueConverter

{

    NumberFormatInfo nfi = new NumberFormatInfo();

   

    object IValueConverter.Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)

    {

        nfi.NumberDecimalSeparator = ",";

        double argument = Convert.ToDouble(value);

        //here set the displaying format for display value

        return argument.ToString("0.######",nfi);

    }

 

    object IValueConverter.ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)

    {

        return value;

    }

}


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

https://help.syncfusion.com/wpf/datagrid/column-types?cs-save-lang=1&cs-lang=xaml#column-formatting-using-converter

C# Code snippet related to
GridCellNumericRenderer:

//Removes default renderer.

this.sfDataGrid.CellRenderers.Remove("Numeric");

//Add custom renderer.

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

 

// Override the GridCellNumericRenderer class

 public class GridCellNumericRendererExt : GridCellNumericRenderer

 {

     // Override the OnInitializeEditElement method

     public override void OnInitializeEditElement(DataColumnBase dataColumn, DoubleTextBox uiElement, object dataContext)

     {

         base.OnInitializeEditElement(dataColumn, uiElement, dataContext);

 

         //Here customize based on your sceanrio

         if (dataColumn.GridColumn.MappingName == "OrderID")

         {

             // get cell value

             var cellValue = (dataContext as OrderInfo).OrderID.ToString("0.######");

 

             // Split the cell value based on decimal Separator

             var getdecimalcount = cellValue.Split('.');

 

             // Set the decimal count based on the value contains after the decimal separator

             uiElement.NumberDecimalDigits = getdecimalcount.Count() > 1 ? getdecimalcount[1].Count() : 0;

         }

     }

 }


Find the sample demo in the attachment.

UG Link:
https://help.syncfusion.com/wpf/datagrid/column-types?cs-save-lang=1&cs-lang=xaml#customize-column-renderer


Attachment: Sample_43e324ba.zip


HS Harald Stapfer July 25, 2023 06:58 AM UTC

Hello,


thank you now it works.

The only problem we still have is that we cannot add more decimal points.


Example:

We can type: 20,45

We cannot type: 20,456712


How can we achieve this?



VS Vijayarasan Sivanandham Syncfusion Team July 26, 2023 06:49 PM UTC

Harald Stapfer,

Your requirement to add more decimal points at runtime can be achieved by setting the DoubleTextBox.MinimumNumberDecimalDigits property in OnInitializeEditElement and customize the PreviewKeyDown event in SfDataGrid. Refer to the below code snippet,

C# Code snippet related to OnInitializeEditElement:

// Override the GridCellNumericRenderer class

public class GridCellNumericRendererExt : GridCellNumericRenderer

{

    // Override the OnInitializeEditElement method

    public override void OnInitializeEditElement(DataColumnBase dataColumn, DoubleTextBox uiElement, object dataContext)

    {           

        base.OnInitializeEditElement(dataColumn, uiElement, dataContext);

 

        //Here customize based on your scenario

        if (dataColumn.GridColumn.MappingName == "OrderID")

        {

            // get cell value

            var cellValue = (dataContext as OrderInfo).OrderID.ToString("0.######");

 

            // Split the cell value based on decimal Separator

            var getdecimalcount = cellValue.Split('.');

            // Set the decimal count based on the value contains after the decimal separator

            uiElement.MinimumNumberDecimalDigits = getdecimalcount.Count() > 1 ? getdecimalcount[1].Count() : 0;         

 

        }

    }

}


C# Code Snippet related to PreviewKeyDown event:

//Event subscription

sfDataGrid.PreviewKeyDown += OnPreviewKeyDown;

 

// Event customization

private void OnPreviewKeyDown(object sender, KeyEventArgs e)

{

    //Here customize based on your scenario

    if((e.Key >= Key.D0 && e.Key <= Key.D9))

    {

        DoubleTextBox doubleTextBox = (DoubleTextBox)e.OriginalSource;

        int newCursorPosition = doubleTextBox.SelectionStart;

 

        if (doubleTextBox.Text.IndexOf(',') < newCursorPosition)

        {

            int newDecimalDigits = GetDecimalDigitsCount(doubleTextBox.Text + (e.Key - Key.D0).ToString());

            NumberFormatInfo nfi = new NumberFormatInfo();

            nfi.NumberDecimalSeparator = ",";                   

            doubleTextBox.NumberDecimalDigits = newDecimalDigits;

            var getValue = doubleTextBox.Text.Insert(newCursorPosition, (e.Key - Key.D0).ToString());

            doubleTextBox.Value = double.Parse(getValue, nfi);

            doubleTextBox.SelectionStart = doubleTextBox.Text.Length;

            e.Handled = true;                   

        }

    }

}

 

// Helper method to count the number of decimal digits in a string

private int GetDecimalDigitsCount(string value)

{

    int decimalSeparatorIndex = value.IndexOf(',');

    return decimalSeparatorIndex >= 0 ? value.Length - decimalSeparatorIndex - 1 : 0;

}


Find the modified sample demo in the attachment.

Note: The provided solution is just a workaround to achieve your requirement with some limitations.


Attachment: ModifiedSample_8571acdb.zip

Loader.
Up arrow icon