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

PropertyGrid Rounding

6 Answers 265 Views
PropertyGrid
This is a migrated thread and some comments may be shown as answers.
Alex
Top achievements
Rank 1
Alex asked on 03 Dec 2013, 09:45 PM
OS WINXP
C# .Net 4.0
Telerik 2012.2.726.40

I am displaying/editing RadTreeView Nodes in a RadPropertyGrid.
I'm having a problem with the properties being rounded to two decimal places, where I want to support up to four decimal places.

So lets say I have a property named Length:
/**
 *\brief Gets or sets the name variable.
*\
*/
[DefaultValue ("New User Home"),
DescriptionAttribute ("Name of this User Home Object."),
CategoryAttribute ("(General)")]
public Double Length
{
      get { return Math.Round(_length, 4); }
      set { _length = value; }
}

If I enter 23.4567 into the property grid it rounds it to 23.46.

From this msdn thread I found that you can wrap it into a string and handle the number of decimals from there.
The problems I have with this are:

  1. I shouldn't have to use a cheap trick to determine the accuracy of a number.
  2. When I save the node there are now two lines of XML just for one property, the property that is called in my code, and the wrapper.

What is the correct way to determine the rounding performed on all doubles/floats.

6 Answers, 1 is accepted

Sort by
0
Alex
Top achievements
Rank 1
answered on 04 Dec 2013, 01:51 PM
Using [XMLIgnore] I no longer save the wrapper in XML when saving the node, but I would still like to know how to change the rounding for propertyGrid to keep the code cleaner.
0
Accepted
Dess | Tech Support Engineer, Principal
Telerik team
answered on 06 Dec 2013, 08:17 PM
Hello Alex,

Thank you for contacting Telerik Support.

In order to avoid rounding of Double type properties in the certain version (2012.2.726.40), you can specify the DecimalPlaces for the editor (which value is 2 by default):
public Form1()
{
    InitializeComponent();
 
    radPropertyGrid1.SelectedObject = new DoubleItem(23.4567);
 
    radPropertyGrid1.EditorInitialized += radPropertyGrid1_EditorInitialized;
}
 
private void radPropertyGrid1_EditorInitialized(object sender, PropertyGridItemEditorInitializedEventArgs e)
{
    PropertyGridTableElement te = sender as PropertyGridTableElement;
    PropertyGridSpinEditor editor = e.Editor as PropertyGridSpinEditor;
    if (editor != null && te != null)
    {
        ((BaseSpinEditorElement)editor.EditorElement).DecimalPlaces = 4;
    }
}
 
public class DoubleItem
{
    public double DecimalValue { get; set; }
 
    public DoubleItem(double decimalValue)
    {
        this.DecimalValue = decimalValue;
    }
}

I hope this information helps. Should you have further questions, I would be glad to help.

Regards,
Desislava
Telerik
TRY TELERIK'S NEWEST PRODUCT - EQATEC APPLICATION ANALYTICS for WINFORMS.
Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
Sign up for Free application insights >>
0
Alex
Top achievements
Rank 1
answered on 06 Dec 2013, 09:14 PM
This does make it so I can have 4 decimals on double values, but it also displays 4 decimal places on
integer values when you select it to edit. I find that this may mislead a user to think it is a double.
Is there a way to prevent integer values from showing the decimals when selected.

As a side note, what is the DoubleItem class you included for? I don't see what it might pertain to.

radPropertyGrid1.SelectedObject = new DoubleItem(23.4567);
  
public class DoubleItem
{
    public double DecimalValue { get; set; }
  
    public DoubleItem(double decimalValue)
    {
        this.DecimalValue = decimalValue;
    }
}

Thanks,
Alex
0
Accepted
Dess | Tech Support Engineer, Principal
Telerik team
answered on 11 Dec 2013, 02:20 PM
Hello Alex,

Thank you for writing back.

In order to apply four decimal places only for Double properties, you may use the following example: 
private void radPropertyGrid1_EditorInitialized(object sender, PropertyGridItemEditorInitializedEventArgs e)
{
    PropertyGridTableElement te = sender as PropertyGridTableElement;
    PropertyGridSpinEditor editor = e.Editor as PropertyGridSpinEditor;
    if (editor != null && te != null )
    {
        Type type = radPropertyGrid1.SelectedObject.GetType().GetProperty(e.Item.Name).PropertyType;
        if (type == typeof (System.Double) )
        {
            ((BaseSpinEditorElement)editor.EditorElement).DecimalPlaces = 4;
        }
         
    }
}

As to the question about DoubleItem class, it is used for RadPropertyGrid.SelectedObject, which gets or sets the object which properties the RadPropertyGrid is displaying.

Please do not hesitate to contact us if you have any additional questions.

Regards,
Desislava
Telerik
TRY TELERIK'S NEWEST PRODUCT - EQATEC APPLICATION ANALYTICS for WINFORMS.
Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
Sign up for Free application insights >>
0
Florrie
Top achievements
Rank 1
answered on 15 Oct 2019, 08:43 PM

I am having a problem with double values in my RadPropertyGridView not displaying properly in the Italian Regional setting.  The decimal separator by default is the comma.  However, the user set it to period.  The numbers in my properties grid are displaying with the comma.  I was under the assumption that this Telerik control has a builtin data converter for common types, such as double.  Why is this happening?  Do I need to write a value conversion class?  Here is my declaration in the xaml:

                    <telerik:RadPropertyGrid x:Name="framePropertyGrid"
                                             Margin="5"
                                             AutoGeneratingPropertyDefinition="framePropertyGrid_AutoGeneratingPropertyDefinition"  
                                             HorizontalAlignment="Stretch" 
                                             VerticalAlignment="Stretch" 
                                             SortAndGroupButtonsVisibility="Hidden" 
                                             SearchBoxVisibility="Hidden" 
                                             LabelColumnWidth="200" 
                                             IsGrouped="False" 
                                             IsTabStop="False"
                                             CanUserResizeDescriptionPanel="False" 
                                             DescriptionPanelVisibility="Collapsed"/>

 

0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 18 Oct 2019, 07:53 AM
Hello, Florrie,      

I would like to note that this forum is related to RadPropertyGrid from the Telerik UI for WinForms suite. However, according to the provided code snippet, it seems that you are not using the WinForms product. 

Feel free to post your inquiries in the relevant forum: https://www.telerik.com/forums

Thus, the appropriate community will gladly assist you. Thank you for your understanding.

 

Regards,
Dess | Tech Support Engineer, Sr.
Progress Telerik

Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
Tags
PropertyGrid
Asked by
Alex
Top achievements
Rank 1
Answers by
Alex
Top achievements
Rank 1
Dess | Tech Support Engineer, Principal
Telerik team
Florrie
Top achievements
Rank 1
Share this question
or