Question Funny behaviour of WinForms Databinding/BindingSource

jigneshdesai

New member
Joined
Jan 15, 2009
Messages
2
Programming Experience
3-5
I have a tipical BindingSource control on form.
There are several textbox controls bind to a field from BindingSource control.
Few of the fields are of type numeric.
If i do not enter anything into textboxs for numeric type fields, all is fine. it saves records with null values for those fields.

But lets say if i enter a numeric value and then decide to delete it, my cursor is forced to remain in the field, until i enter a numeric value (Zero).
Why is this behaviour?

Can;t it understands that if textbox is left blank it should assign "Null" for that column/field.

Is there any workaround for this behaviour...?

Regards
JIGNESH.
 
Why is this behaviour?
Because there is a difference between an empty string and a null one?

Can;t it understands that if textbox is left blank it should assign "Null" for that column/field.
Not by default

Is there any workaround for this behaviour...?

Go into the [Properties]\(DataBindings)\(Advanced) data bindings for the text box
A window appears showing advanced databindings for the text box
Click the Text in the list
Enter abc123 (or whatever) into the Null value textbox
Close the advanced data bindings window
Open the Find dialog (Ctrl+H) and search the ENTIRE SOLUTION (tick HIDDEN TEXT) for abc123
The YOUR_FORM_NAME.Designer.vb file should be open at a line looking like:
VB.NET:
Me.[B]column2TextBox[/B].DataBindings.Add(New System.Windows.Forms.Binding("Text", this.[B]dataTable1BindingSource[/B], "[B]Column2[/B]", True, System.Windows.Forms.DataSourceUpdateMode.OnValidation, "abc123"))
(everything in bold will be different on yours)

See the abc123? That's what the textbox will show when the underlying data source is Null. It's also what will cause a Null to be written to the table if it is typed into the text box.

Change the "abc123" to just ""



The bug comes via:
If you dont enter anything into Null value textbox (in designer), code like this goes into the designer:

Me.column2TextBox.DataBindings.Add(new System.Windows.Forms.Binding("Text", this.dataTable1BindingSource, "Column2", True))


Microsoft should have implemented a checkbox AND a textbox for the Null Value setting. If the box is not ticked, this would go in the designer:

Me.column2TextBox.DataBindings.Add(new System.Windows.Forms.Binding("Text", this.dataTable1BindingSource, "Column2", True))

If the box is ticked and the textbox left blank, this would go in the designer:

Me.column2TextBox.DataBindings.Add(new System.Windows.Forms.Binding("Text", this.dataTable1BindingSource, "Column2", True, blah.OnValidation, ""))



Once you get savvy with this method I'm sure you can just open the .Designer.vb file, find the right line and edit the relevant bits in. The abc123 method is just to make it easy first time ;)

If you want to get really clever, you can write an app that will find/replace "abc123" (or whatever) with "" in all the designer files before they are compiled. That way you don't have to remember to edit the designer each time you open the advanced bindings text box.
 

Latest posts

Back
Top