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:
- I shouldn't have to use a cheap trick to determine the accuracy of a number.
- 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
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
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 >>
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
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
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 >>
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"/>
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