Databinding Exception Cannot format the value to t

2019-05-31 22:48发布

问题:

We're using a lot of databinding in our winforms application and we've recently hit some hard to debug scenario's.

On calling ResumeBinding() on our BindingSource we receive an Exception:

ex.Type FormatException ex.Message Cannot format the value to the desired type:

at System.Windows.Forms.Binding.FormatObject(Object value) at System.Windows.Forms.Binding.PullData(Boolean reformat, Boolean force) at System.Windows.Forms.BindingManagerBase.PullData(Boolean& success) at System.Windows.Forms.CurrencyManager.CurrencyManager_PullData() at System.Windows.Forms.CurrencyManager.EndCurrentEdit() at System.Windows.Forms.CurrencyManager.ChangeRecordState(Int32 newPosition, Boolean validating, Boolean endCurrentEdit, Boolean firePositionChange, Boolean pullData) at System.Windows.Forms.CurrencyManager.UpdateIsBinding(Boolean raiseItemChangedEvent) at System.Windows.Forms.CurrencyManager.UpdateIsBinding() at System.Windows.Forms.CurrencyManager.ResumeBinding() at System.Windows.Forms.BindingSource.ResumeBinding() at SomeProject.SomePanel.FlightData_DataReady() in C:\SomeDir\SomePanel.cs:line 94

We're using a simple object model with some Nullable properties. Not all of them have try catches around the setters/getters though

We're using DevExpress components, this particular exception started after changing a property into a Nullable property and adding a NullText property to the DevExpress textbox.

Can anyone shine a light on how to debug this situation?


After working on this problem a bit more I've noticed some particulars creating this problem.

One of the Nullable double properties was binded to a control that has a Single property. The databinding engine threw some First change exceptions about not being able to convert a Nullable double into a Single.

Debugging this and finding which control/property was to blame is still impossible to do, anyone got some clues how to do this?

回答1:

Try enabling formatting

this.myTextBox1.DataBindings.Add(new System.Windows.Forms.Binding("EditValue", this.bindingSource1, "MyProperty", true));

Notice the true-parameter

Setting this property to true also enables error-handling behavior and causes the BindingComplete event to be raised. The handler of this event can take the appropriate action, based on the success, error, or exceptions in the binding process, by examining the BindingCompleteState property of the BindingCompleteEventArgs parameter.

http://msdn.microsoft.com/en-us/library/system.windows.forms.binding.formattingenabled.aspx