Please excuse this minor tirade I'm about to belch out...
I've never been a fan of automatic data binding. I still remember the full page ads in Visual Basic Programmers Journal showing sample screenshots of an Access employee databased hooked in to a VB3 for with very little coding. It looked sweet and demo'd well, but in practice, the data binding was never good enough for production work. As I recall, VB4/5/6 didn't show any big improvements in data binding.
.NET 1.x showed noticeable improvements in data binding. To be honest, I never used data binding in 1.x, but heard it was much more robust -- yet still lacked in some areas and was, at times, cumbersome to use.
Now that .NET 2.0 is here, I've got a chance to look at data binding again. I've read a lot of nice things about the improvements in data binding with 2.0 and was cautiously optimistic that it may be something I could actually put to good use. I whipped up a quick sample app and was impressed at how easy it was to bind control properties to datasource properties. But before doing this in my production app, I had one more aspect to get right.
I want to control exactly when the controls are loaded with property values from the datasource and I wanted to control exactly when the datasource was updated from the controls. The reason? I've got a configuration form that I want to display some values to the user. They can edit and change stuff using the UI, but I don't want the datasource updated unless they press the "OK" button. If they press "Cancel", I just close the form. If the press "OK", I was going to tell all the Binding objects to Write their values to the datasource.
From reading the documentation, I thought I could get this kind of control by setting the DataSourceUpdateMode to Never (never update the datasource unless I explicitly call WriteValue on the Binding object) and setting ControlUpdateMode to Never (never update the control unless I explicitly call ReadValue on the Binding object). So I set out to implement this (what I thought!) very easy sample:
b = new Binding("Text", _config, "Desc", false, DataSourceUpdateMode.Never); b.ControlUpdateMode = ControlUpdateMode.Never; textBox1.DataBindings.Add(b);
My datasource (_config) has a "Desc" property I'm binding to the "Text" property of textBox1. As you can see both DataSourceUpdateMode and ControlUpdateMode are set to Never. The code shown above is inside my Form ctor, right after InitializeComponent. Yet what happens when my ctor exits?
THE PROPERTY SETTER ON 'Desc' IS CALLED AND IT'S SET TO AN EMPTY STRING!!
Sorry for shouting. But The call is coming from "external code" (according to the Debug Location dropdown) -- in other words, not my code. As a matter of fact, those three lines are about the only code added to the Form -- there is no other code in my sample app that sets the Desc property. Why is the setter being called? I've set DataSourceUpdateMode to Never. I can't see any reason why the "Desc" property should be touched.
Argh... Bit in the a$$ again by data binding.