"variable" decimal places in decimal column

5 posts, 0 answers
  1. Roman
    Roman avatar
    15 posts
    Member since:
    Jan 2011

    Posted 20 Oct Link to this post

    Hello,

    May be this is a dumb question, but I cannot find how to make decimal column in grid to display and edit decimal data as it is, i.e

    2
    2.5
    0.371

    without any trailing zeros and not limiting to DecimalPlaces. For display purposes this may be not so big issue, but column spin editor rounding data to the DecimalPlaces, that is inacceptable at all.

    And I still need to limit users on typing only numbers (minus, decimal separator) in the editor, and bind column to a decimal property, i.e. I cannot use plain text column, I think. Could someone suggest a solution?

     

     

  2. Dimitar
    Admin
    Dimitar avatar
    1415 posts

    Posted 21 Oct Link to this post

    Hi Roman,

    Thank you for writing.

    You can use GridViewMaskBoxColumn. For example:
    GridViewMaskBoxColumn maskBoxColumn = new GridViewMaskBoxColumn();
    maskBoxColumn.Name = "Price";
    maskBoxColumn.FieldName = "Dosage";
    maskBoxColumn.HeaderText = "Unit Price";
    maskBoxColumn.MaskType = MaskType.Numeric;
    maskBoxColumn.Mask = "G";
    maskBoxColumn.TextAlignment = ContentAlignment.BottomRight;     
    maskBoxColumn.DataType = typeof(decimal);
    radGridView1.MasterTemplate.Columns.Add(maskBoxColumn);

    I hope this will be useful. Let me know if you have additional questions.

    Regards,
    Dimitar
    Telerik by Progress
    Check out the Windows Forms project converter, which aids the conversion process from standard Windows Forms applications written in C# or VB to Telerik UI for WinForms.For more information check out this blog post and share your thoughts.
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Roman
    Roman avatar
    15 posts
    Member since:
    Jan 2011

    Posted 21 Oct in reply to Dimitar Link to this post

    Hello Dimitar,

    It was not working for me with "G" (doesn't allow to type decimal separator), but with "N" mask it behaves not as I would expect from the numeric editor. First of all it displays zero mask value for null value, i.e. user sees nothing in the cell, but on editing the value becomes 0.00. Then it looks like the number of decimal places is still limited, because I cannot enter more than 2 digits there. And I cannot enter less than 2 digits as well. Then typing of values are really weird, as soon as I type decimal separator, cursor jumps to the end of the value and next typed digit goes to the last place, i.e. if I type "1", "2", "." and "3", the value will be 12.03, but not 12.30.

    So overall this doesn't look like a solution for my issue, it makes things even worse.

    That I need is the behavior of the decimal column editor (I mean user experience of typing values there) but without any limitation on decimal places (especially without rounding). May be I may just set a big value for decimal places in the decimal column and then trim trailing zeros on editor initialization, but this will be quite a strange workaround for such a trivial requirement to display and edit decimal values as they are in the grid. Is it really the case that nobody need this feature and it is not supported at all?

  5. Roman
    Roman avatar
    15 posts
    Member since:
    Jan 2011

    Posted 21 Oct in reply to Roman Link to this post

    Well, in case someone interested, I ended up following the custom editor solution, thanks God Telerik controls has so well structured internal design. Here is the code

    private void GridViewEditorRequired(object sender, EditorRequiredEventArgs e)
    {
       if (gridView.CurrentColumn is GridViewDecimalColumn)
       {
          var ed = new UnlimitedDecimalGridSpinEditor();
          ed.CopySettings((GridViewDecimalColumn)gridView.CurrentColumn);
          e.Editor = ed;
       }
    }
     
    public class UnlimitedDecimalGridSpinEditor : GridSpinEditor
    {
       protected override RadElement CreateEditorElement()
       {
          return new UnlimitedDecimalSpinElement();
       }
     
       public void CopySettings(GridViewDecimalColumn column)
       {
          DecimalPlaces = column.DecimalPlaces;
          MaxValue = column.Maximum;
          MinValue = column.Minimum;
          Step = column.Step;
          ThousandsSeparator = column.ThousandsSeparator;        
       }
    }
     
    public class UnlimitedDecimalSpinElement : RadSpinEditorElement
    {
          protected override void ValidateCore()
          {
             var num = Constrain(DecimalPlaces == 0
                         ? Math.Round(internalValue, 0, MidpointRounding.AwayFromZero)
                         : internalValue);
     
             if (num == internalValue)
                return;
             internalValue = num;
             TextBoxItem.Text = GetNumberText(Value);
       }
     
       protected override string GetNumberText(decimal num)
       {
          return CustomGetNumberText(num, Hexadecimal, ThousandsSeparator, DecimalPlaces);
       }
     
       private static string CustomGetNumberText(decimal num, bool hex, bool thousands, int decimalPlaces)
       {
          if (hex)
             return string.Format("{0:X}", (long)num);
     
          if (thousands)
             return num.ToString("N" + decimalPlaces.ToString(CultureInfo.CurrentCulture), CultureInfo.CurrentCulture);
     
          return num.ToString("G", CultureInfo.CurrentCulture);
       }
    }
  6. Dimitar
    Admin
    Dimitar avatar
    1415 posts

    Posted 24 Oct Link to this post

    Hi Roman,

    It appears that the "G" mask is not behaving as expected (you should be able to enter the decimal separator by default). I have logged this issue in our Feedback Portal and I have added a vote for it on your behalf. You can track its progress, subscribe for status changes and add your comment to it here

    There is no workaround for the masked edit box, and you can use your solution with the spin editor for this. 

    In addition, thank you for sharing your solution with the community. I am sure that it will be of help for others. This is why I have updated your Telerik Points.

    Should you have any other questions do not hesitate to ask.

    Regards,
    Dimitar
    Telerik by Progress
    Check out the Windows Forms project converter, which aids the conversion process from standard Windows Forms applications written in C# or VB to Telerik UI for WinForms.For more information check out this blog post and share your thoughts.
Back to Top
UI for WinForms is Visual Studio 2017 Ready