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,
Is the code you have here in the designer.cs file for the form? Or are you running it in the Form.cs itself or some other method?
I tried it out and the code you have here works fine if it's int he Form designer code. But if you take the same code and put it in Form_Load, it raises an exception.
The exception is "The specified mask is not compatible with the current value of the 'NumericType' property."
The reason for this exception appears to be that you are setting the MaskInput before you set the NumericType property. If you do this in the designer code, then the control handles this, but if you set it in your code, it won't work. The fix is very simple in that case, of course. Just set the MaskInput AFTER setting the NumericType property.
I have attached my sample here so you can check it out. In my sample, I set the MaskInput and other properties inside the designer code and it's working fine for me.
Hi Mike, thank you for the response.
Yes, all the design and layout centric stuff were all in the designer.
After running your example I'm confused, why wouldn't the setter for the bound property be called. Is this a "Two Way" binding?
I've modified your property to the following.
private void Form1_Load(object sender, EventArgs e){ this.NullableProperty = 5; this.ultraNumericEditor1.DataBindings.Add("Value", this, "NullableProperty", false, DataSourceUpdateMode.OnPropertyChanged);}private decimal? _nullableProperty;public decimal? NullableProperty { get { return _nullableProperty; } set { _nullableProperty = value; } }
given the binding you set up, i would expect a breakpoint at the set would be hit.
Your example and another example that was provided by the infragistics support team, both contained examples of binding functionality they were intending to display. Both are having this binding issue. Could there be something strange on my computer preventing these from working correctly?
thank you.
thanks in advance
I responded to your other post and explained why the property setter was not getting hit in that example.
I'm confused about this case, though. When I change the code like you have it here and put a breakpoint in the property setting, it DOES get hit. It gets hit from the Form_Load when the NullableProperty is explicitly set.
It doesn't get hit at any other point because the value is not changing anywhere else. When are you expecting it to change?
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.
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?
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?