Why do I keep trying?

I see it's been almost 2 years since I lashed out on Data Binding in .NET.  Judging from the comments I received, it seems I'm not the only one who thinks this technology is not implemented very well.

Nevertheless, I had a situation today where I thought Data Binding might be able to help me.  Sure, I was very apprehensive, but I really thought data binding could help me out and reduce my work.  Plus, despite my loathing of Data Binding and my continued bad experiences with it, I keep thinking it's me and I just don't know how to use it.

The Situation

I'm writing an application and I need a UI to display a bunch of pressure ranges (min/max pairs) for the user to edit.  So to make things easier, I created a UserControl that contained a label (the name of the pressure range) along with two text boxes -- one for the min and one for the max.  The user control exposed two integer properties, LowValue and HighValue.  These were just wrappers to access the Text property of the two textboxes.

Data Binding

Now I want to data bind a couple of properties in a business object to these values:

rangeControl1.DataBindings.Add("LowValue", config, "PressureMin");
rangeControl1.DataBindings.Add("HighValue", config, "PressureMax");

Seems simple enough.

I start my app and the "config" business object defaults PressureMin to 20 and PressureMax to 200.  When I pull up the UI to edit the high and low value, they are both properly displayed as 20 and 200.  I change the low pressure to 10 and press TAB to go to the next field.  Everything is fine.  I change the high pressure to 330 and press TAB.  That's when I get bit by databinding!

The "330" changes back to 200 after I leave the field!

Huh?  Why would one property work (the low pressure) but not the high one?  I double-checked my data binding (it's just defined through strings so I thought I might have made a copy/paste error).  Nope, everything looks good.

The Problem

I messed with this and Google'd for over an hour thinking this should work fine.  I figure it had to be me doing something wrong.  Once I realized what was happening, maybe it is me.  Maybe I just don't grok how Microsoft implemented data binding.  In any case, here's what I think is happening:

1. I enter "10" for the low pressure and hit TAB.  I'm still inside the user control so nothing related to data binding happens.

2. I enter "330" for the high pressure and hit TAB.  Now we're leaving the user control so data binding takes over.  It starts looping though it's data bindings collection.

3. The first binding to "LowValue" causes the "10" to be sent to my business object via the "PressureMin" setter.

4. Data binding now detects that my business object has changed and updates all bound controls!  This means the value for "PressureMin" (the value 10 that was updated in step 3) is moved to the LowValue control (no problem there) and the value for "PressureMax" (which is still 200) is placed in the HighValue control.

At this point, I'm, well, you know -- up someone's creek without a paddle.  My edited high pressure value of "330" is now gone.  Sure, data binding continues looping throughthe bound properties and "200" is passed to my business object via the PressureMax setter.  But that's moot at this point.

Again, it's probably me.  Maybe Microsoft never intended for multiple properties to be bound to a single object (why?).  I played around with setting DataSourceUpdateMode on my data bindings to Never.  That seemed to work as my edited high value didn't revert back to 200 when I tabbed out of the field.  But as soon as I used the binding source's "WriteValue" method, it caused step 4 to fire again, thus wiping out my other edits.

I do have a workaround, although I don't like it.  I expose the individual TextBox controls for the high and low values.  I do the data binding on each individual textbox instead of the usercontrol as a whole.  I feel dirty exposing those text boxes only to support data binding, but it does save me some work.

I think this is the last time I'm going to try messing with data binding.

1 Comment

  • I see you're point but I don't agree completely. In my experience, using two-way binding and/or declarative binding generally sucks. By this I am referring to using datasource controls and using the Bind() method in markup. It never works properly because things bind at the wrong times or don't bind at all. Not only that but you're stuck with the limitations of the internal "binding stack" that the datasources utilize. Ever try to add a cascading dropdown (not the toolkit kind, the old fashioned manual kind) to a FormView, DetailsView, or GridViewRow? It just doesn't work. Because of this limitation, I've wound up writing more work-around code that I would have had to do just do all the binding programmatically - how frustrating.

    That being said, databinding can still be done programmatically by wiring up and event handler to the DataBinding event of a control and writing some code to set the control's datasource in the event handler. That actually works great and can give you much more fine grained control over when things bind and don't bind and you don't have to deal with the binding stack at all. In addition, you can more easily do things like programmatically constuct SQL statements and dynamically set parameter values.

    Bottom line - the datasource controls look cool in simple demos but in the real world, they're more work and headache.

    -Mark

Comments have been disabled for this content.