This is a migrated thread and some comments may be shown as answers.

"variable" decimal places in decimal column

4 Answers 285 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Roman
Top achievements
Rank 1
Roman asked on 20 Oct 2016, 05:27 PM

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?

 

 

4 Answers, 1 is accepted

Sort by
0
Dimitar
Telerik team
answered on 21 Oct 2016, 06:01 AM
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.
0
Roman
Top achievements
Rank 1
answered on 21 Oct 2016, 11:20 AM

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?

0
Roman
Top achievements
Rank 1
answered on 21 Oct 2016, 02:20 PM

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);
   }
}
0
Dimitar
Telerik team
answered on 24 Oct 2016, 11:28 AM
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.
Tags
GridView
Asked by
Roman
Top achievements
Rank 1
Answers by
Dimitar
Telerik team
Roman
Top achievements
Rank 1
Share this question
or