PropertyGrid Rounding

5 posts, 2 answers
  1. Alex
    Alex avatar
    4 posts
    Member since:
    Dec 2013

    Posted 03 Dec 2013 Link to this post

    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.
  2. Alex
    Alex avatar
    4 posts
    Member since:
    Dec 2013

    Posted 04 Dec 2013 Link to this post

    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.
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Answer
    Dess
    Admin
    Dess avatar
    1601 posts

    Posted 06 Dec 2013 Link to this post

    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 >>
  5. Alex
    Alex avatar
    4 posts
    Member since:
    Dec 2013

    Posted 06 Dec 2013 Link to this post

    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
  6. Answer
    Dess
    Admin
    Dess avatar
    1601 posts

    Posted 11 Dec 2013 Link to this post

    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 >>
Back to Top
UI for WinForms is Visual Studio 2017 Ready