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

Value data type

4 Answers 166 Views
NumericUpDown
This is a migrated thread and some comments may be shown as answers.
Hugo Aristizabal
Top achievements
Rank 2
Hugo Aristizabal asked on 20 Jan 2010, 10:12 PM
Hi, is there a way to change the data type returrned by the NumericUpDown's value? I need it to be int, not double, because it's mapped to a database field which has an int type, and I'm getting a conversion error. Thanks.

4 Answers, 1 is accepted

Sort by
0
Konstantina
Telerik team
answered on 22 Jan 2010, 11:52 AM
Hello Hugo,

Thank you for contacting us.

You can set the data type of the NumericUpDown control to be integer by using the IsInteger property.
<telerikInput:RadNumericUpDown x:Name="numeric" IsInteger="True"/>

If you have further questions please feel free to contact us again.

Greetings,
Konstantina
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
Hugo Aristizabal
Top achievements
Rank 2
answered on 22 Jan 2010, 01:18 PM
Thanks, I saw the property. With it set to true, will I get the value as an int, or will I stillget it as a decimal?

Thanks again.
0
Konstantina
Telerik team
answered on 28 Jan 2010, 11:29 AM
Hi Hugo,

Straight to your  question.

With property IsInteger="True" you will get the values of the NumericUpDown as an integer.

If you have further questions please feel free to contact us again.

All the best,
Konstantina
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
alex
Top achievements
Rank 2
Bronze
Iron
Iron
answered on 22 Oct 2022, 04:18 PM

Hello,

I was set the IsInteger property to True with two way binding to a property of type Object. When I add the property to the watch, I can see that the value is Double. I am using the latest Telerik version. Can you help me?

Martin Ivanov
Telerik team
commented on 25 Oct 2022, 06:19 AM

Telerik don't execute any specific code for the conversion. The IsInteger property only format the value to look like an integer. The value in the editor is still double. If you bind the Value property to an integer property, the WPF framework will just do the conversion automatically. The framework is probably something very similar to this:

double myDouble = 5d;
int myInt = (int)myDouble;

This won't work when you data bind the property to a view model property of type Object. In this case the view model will get the property as a plain Double. To achieve your requirement, you can cast the value on the property setter. For example:

public object MyValue
{
	get { return myValue; }
	set 
	{
		myValue = (int)value; 
		OnPropertyChanged("MyValue");
	}
}

Or just use an integer typed property.

alex
Top achievements
Rank 2
Bronze
Iron
Iron
commented on 28 Oct 2022, 05:40 AM | edited

Hey,

If telerik is doing some conversion when IsInteger is set to True, it shall be doing that anyway, without checking the target type. My type is Object because I am using it in many view models. For each view model I have a DataTemplateSelector. In the case I need int type, I put the RadNumericUpDown. My value can also be null because it is a not mandatory field but it is not relevant to the question. I will not add casting to int because it is the same view model for all types and I dont think that I should add specific functionality for UI. Tomorrow if I want to change the RadNumericUpDown with another control, I will not need that logic. Can you add that basic funcionality please? 

I wlll appreciate if you open a ticket for that, my company is using Telerik license of course.

Martin Ivanov
Telerik team
commented on 28 Oct 2022, 09:40 AM | edited

Telerik do not do any conversion for the Value of the control. The IsInteger property serves only to format the display text. The value goes as integer to the view model (when the bound property is of type Int32), because the native WPF data binding used to link the Value with the view model property can handle this type of conversions automatically. Even if you don't set the IsInteger property to True, this conversion will happen and you will end up with an integer value in the view model (in case the data bound property is of type int). 

Because the bound property on your side is of type Object, the WPF binding will use the original value coming from the Value property of RadNumericUpDown, thus it will store it as Double (boxed in the Object property field).

Changing this behavior and converting the value of the Value property to int when IsInteger is true will cause a breaking change in the behavior of the control.

If you expect nullable data, I would suggest you to use the nullable primitive type, instead of Object. For example:  public int? MyInt { get; set; }. Or if you can't make changes in your model, then you can data bind the Value property using an IValueConverter. This will allow you to convert the Double value to an integer in the ConvertBack() method.

About the support ticket, you can open one using the account to which the Telerik license is assigned.

Tags
NumericUpDown
Asked by
Hugo Aristizabal
Top achievements
Rank 2
Answers by
Konstantina
Telerik team
Hugo Aristizabal
Top achievements
Rank 2
alex
Top achievements
Rank 2
Bronze
Iron
Iron
Share this question
or