I have a strange behaviour in the binding of some controls in my solution. The control is a TextBox control that handles numeric behaviours. I am working with a decimal textbox for instance. When I do bind the control to the Decimal property (sometimes to the .Text property too) the .NET Framework raises exceptions when the binding occurs. I have created a simple solution that can help understand my problem. The datasource in the solution contains a list of an entity where there are nullable decimal values. When the user goes to one of the rows with a null in the BonoPago column, the program raises an exception. If the user enters a number in any of the two decimal textboxes and presses the command button, multiple exceptions get thrown.
I think I am missing something really simple here, but I can not find anything in google about it.
As AS has stated I am trying now to describe my problem in words. It's a little long but here it goes:
The code I uploaded to
http://expirebox.com/download/5ef0b0047bd988d29a92c3d9dcde7785.html[
^]
contains:
1) DecimalTextBox.cs contains the code for the control I want to bind. That file contains multiple classes as I did not want to put many files inside my project.
2) DecimalTextBox is the class in DecimalTextBox.cs I want to bind.
3) DecimalTextBox inherits from NumericTextBox, NumericTextBox from TextBox, and TextBox from System.Windows.Forms.TextBox.
4) NumericTextBox has a get/set property named Decimal of type decimal.
5) I want to bind that property (Decimal) to a decimal property in an entity.
6) I created a sample entity with four fields (in the ClassEstructura.cs class): a string, an enum value for a day of week, a decimal value and a nullable decimal value.
7) I want to bind the decimal value to an instance of DecimalTextBox through the Decimal property.
8) This is done in Line 41 - Form1.cs with the line
this.montoPagoBinding = this.textBoxPago.DataBindings.Add("Decimal", bindingSource, "MontoPago", true, DataSourceUpdateMode.Never, null);
9) I want to bind the nullable decimal value to another instance of DecimalTextBox through the Decimal property (probably it`s not OK as the property does not accept null values).
10) This is done in line 42 - Form1.cs with the line
this.bonoPagoBinding = this.textBoxBono.DataBindings.Add("Decimal", bindingSource, "BonoPago", true, DataSourceUpdateMode.Never, string.Empty);
11) I put the next 2 lines (43 - 44) - Form1.cs trying to parse the null decimal values, but they do not work.
12) In lines 27-36 I create a list of my entities and assign it as a datasource to a binding source.
13) In line 37 I create a handler for the BindingComplete event in which I can get binding exceptions.
14) The bindingSource_BindingComplete method just adds data to the exception and shows a message box with it.
15) That message box were the screen shots I took and uploaded.
16) The problem with the binding appears when I go from a row with all the values set to one in wich the last column does not have value (it`s null).
17) For instance if I click in the Diego row and later in the Ariel row, the exception is raised and the message box appears.
18) The exception is
Excepción nivel (1):
Tipo de excepción:
System.ArgumentException
ArgumentException.ParamName:
Mensaje:
Object of type 'System.String' cannot be converted to type 'System.Nullable`1[System.Decimal]'.
Source:
System
TargetSite:
Void SetValue(System.Object, System.Object)
StackTrace:
at System.ComponentModel.ReflectPropertyDescriptor.SetValue(Object component, Object value)
at System.Windows.Forms.BindToObject.SetValue(Object value)
at System.Windows.Forms.Binding.PullData(Boolean reformat, Boolean force)
Datos adicionales:
Control.Name = 'textBoxBono'
Control.Type = 'WindowsFormsApplication3.DecimalTextBox'
PropertyName = 'Decimal'
FormatString = ''
FormattingEnabled = '1'
title = NULL
19) The other problem is when I change the value in the decimal control (not the nullable decimal value).
20) For instance if I click in the Diego row, change the value in the third control (labeled A pagar) and press the button1 button, the exception is raised and the message box appears.
21) The exception is
Excepción nivel (1):
Tipo de excepción:
System.ArgumentException
ArgumentException.ParamName:
Mensaje:
Object of type 'System.String' cannot be converted to type 'System.Decimal'.
Source:
System
TargetSite:
Void SetValue(System.Object, System.Object)
StackTrace:
at System.ComponentModel.ReflectPropertyDescriptor.SetValue(Object component, Object value)
at System.Windows.Forms.BindToObject.SetValue(Object value)
at System.Windows.Forms.Binding.PullData(Boolean reformat, Boolean force)
Datos adicionales:
Control.Name = 'textBoxPago'
Control.Type = 'WindowsFormsApplication3.DecimalTextBox'
PropertyName = 'Decimal'
FormatString = ''
FormattingEnabled = '1'
title = NULL
And my problem is I can not understand why the .Net Framework is raising that exception shown by the messagebox.show in line 96 - Form1.cs in the bindingSource_BindingComplete method.
It does not help me with the string value the computer is trying to convert.
And the stack trace sets the error not in my code (I think) but in the framework.
I know I am doing something wrong about it, but honestly I don´t know what it is.
I tried debugging the Framework code, but those methods are optimized and they do not show the values of the variables in debugging time so I can't know what value the program is trying to convert from string to decimal.
I hope this description is more understandable of what my problem is.
Thanks in advance.
Regards.
Ariel.