Hello,
I am working with version 20141.2035 and am trying to bind an UltaNumericEditor to a nullable property (NullableProperty). I am doing the following
// designer settings
myUltraNumericEditor.FormatString = "#,###,###.#######";myUltraNumericEditor.MaskDisplayMode = Infragistics.Win.UltraWinMaskedEdit.MaskMode.IncludeLiterals;myUltraNumericEditor.MaskInput = "{double:9.6}";myUltraNumericEditor.MaxValue = 1E+16D;myUltraNumericEditor.MinValue = -1E+16D;myUltraNumericEditor.Name = "ultraNumericEditorWireFxRate";myUltraNumericEditor.Nullable = true;myUltraNumericEditor.NumericType = Infragistics.Win.UltraWinEditors.NumericType.Decimal;myUltraNumericEditor.PromptChar = ' ';
//code behind
public decimal? NullableProperty {get; set;}
myUltraNumericEditor.DataBindings.Add("Value", this, "NullableProperty", false, DataSourceUpdateMode.OnPropertyChanged);
I have no problems using this binding approach when using a non nullable decimal.
Thanks in advance for any help
Hi,
Okay, I see what's happening now.
The way binding works in DotNet is that when you bind to a property of a control, like "Value" the BindingManager looks at the control and tries to find a matching event that fires when that property changes. They do that by looking for an event with the name of the property followed by "Changed". So in this case, the BindingManager hooks into the ValueChanged event of the UltraNumericEditor.
If you look at the call stack of the exception, you will see this:
mscorlib.dll!System.Convert.DefaultToType(System.IConvertible value, System.Type targetType, System.IFormatProvider provider) + 0x4e5 bytes mscorlib.dll!decimal.System.IConvertible.ToType(System.Type type, System.IFormatProvider provider) + 0x25 bytes mscorlib.dll!System.Convert.ChangeType(object value, System.Type conversionType, System.IFormatProvider provider) + 0x45a bytes System.Windows.Forms.dll!System.Windows.Forms.Binding.ParseObject(object value) + 0x1d7 bytes System.Windows.Forms.dll!System.Windows.Forms.Binding.PullData(bool reformat, bool force) + 0xb7 bytes System.Windows.Forms.dll!System.Windows.Forms.Binding.Target_PropertyChanged(object sender, System.EventArgs e) + 0x2c bytes > Infragistics4.Win.UltraWinEditors.v14.1.dll!Infragistics.Win.UltraWinEditors.UltraNumericEditorBase.FireValueChanged(System.EventArgs e) Line 3498 + 0x11 bytes C# Infragistics4.Win.UltraWinEditors.v14.1.dll!Infragistics.Win.UltraWinEditors.UltraNumericEditorBase.OnValueChanged(System.EventArgs args) Line 3474 + 0x11 bytes C# Infragistics4.Win.UltraWinEditors.v14.1.dll!Infragistics.Win.UltraWinEditors.UltraNumericEditorBase.OnValueChanged(object sender, System.EventArgs args) Line 3450 + 0x11 bytes C# Infragistics4.Win.v14.1.dll!Infragistics.Win.EmbeddableEditorBase.RaiseValueChangedEvent() Line 2481 + 0x2f bytes C# Infragistics4.Win.v14.1.dll!Infragistics.Win.UltraWinMaskedEdit.EditInfo.OnTextChanged() Line 424 + 0x22 bytes C#
So what's happening is that the UltraNumericEditor fires it's ValueChanged event and the BindingManager gets the value, which is a decimal, and tries to convert it into a Nullable decimal and it fails to do so.
Since it's the BindingManager code which is failing here, there's really nothing we can do about it.
The good news is that since this is an issue with the BindingManager, I figured that you can't possibly be the first person to experience this, so I did a google search and came up with this article:
http://stackoverflow.com/questions/376361/best-way-to-databind-a-winforms-control-to-a-nullable-type
Basically, the solution is very simple. All you have to do is pass in true for 'formattingEnabled' on your binding:
this.ultraNumericEditor1.DataBindings.Add("Value", this, "NullableProperty", true, DataSourceUpdateMode.OnPropertyChanged);
To be honest, I'm not sure exactly what that parameter does, but if you pass in true, the BindingManager is able to correctly convert the decimal into a Nullable decimal and everything works fine. I also verified that the property setting is getting called correctly.
Absolutely,
i'm getting the following. (in order to get it i needed to disable the "just my code" setting in the debug options Debug -> Options and Settings -> Debugging -> General -> uncheck "Enable just my code") Hopefully this isn't unique to vs 2013.
{"Invalid cast from 'System.Decimal' to 'System.Nullable`1[[System.Decimal, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]'."}
it's being thrown at the Application.Run(new Form1()) line in main method of the Program.cs file.
Thanks for all the help.
I'm not seeing any exceptions. You must be doing something differently than I am.
Can you post your sample where the exception occurs?
After breaking on all exceptions,
I get the following exception when manually entering a value in the UltraNumericEditor:
Invalid cast from 'System.Decimal' to 'System.Nullable`1[[System.Decimal, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]'.
So I'm guessing that somewhere along the way the
ultraNumericEditor1.NumericType = Infragistics.Win.UltraWinEditors.NumericType.Decimal;
setting gets converted into a System.Decimal, and casts back to the bound property, and since there's no Infragistics.Win.UltraWinEditors.NumericType.NullableDecimal it freaks out.
Is there any way that there could be a work around for this?
Sorry if I was unclear.
I expect the breakpoint on set to be hit when I click on the UltraNumericEditor and enter a number.
thank you very much.