How databinding really works v3

Databinding is the name of the mechanism which should provide you a way to create forms which can display and edit the underlying data (fetched from a database and stored in some objects or DataSet) without writing one line of code. All you would do is to set some properties in Visual Studio. In an ideal world it works like this but in WinForms 1.0 there so many design and implementation flaws so you HAVE to read this document if you want to create a nontrivial program. Not checked but in WinForms 1.1 MS invented some new bugs and left some from this document so do not except that it is bugfree now. I will update this document as soon as I will have time to check v1.1.

In databinding there is an idea to keep all your controls in a form in sync and there is an another idea to permit creating different data islands. So MS invented BindingContext. It should provide a feature so that you have some controls in sync in one data island and another group of controls in sync in another data island. It creates more trouble than hand coding this but now it is your reality.

If you did not undertand the previous paragraph then here is a picture:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbcon/html/vbconconsumersofdataonwindowsforms.asp
It is not correct since in a form you can have more than one BindingContext and a datasource (array, DataTable) can have more than one CurrencyManager.

There are two types of databinding
Simple binding (when you bind a one value to one property of a control)
Complex binding (when you bind a list of values to a control like DataGrid or ComboBox)

I wanted to write some more details I found while trying to create a nontrivial db application but now I ceased to use databinding so those articles will probably never go into this site.
BindToObject's descrition will be missing forever.
In complex binding there are two things worth of words:
- how to create bindable objects (IBindingList, etc) can be found here
- how does DataMember work (it is burried deep inside BindingContext) can be found here

I would like to write about master/detail relations, expressions and views in depth when I will have some time. It includes relations, indexes, filters. Why you cannot extend filter expressions. How DataSet.Update works. What is AcceptChanges or EndEdit. (These are not in databinding but in ADO.NET.)

However if you ask me I can tell you that I do not use databinding at all. The only exception is when I want to display a readonly list in a DataGrid or ListControl. Any other case I copy data with a generic routine which works by the help of Control.Tag and SqlParameter.SourceColumn. Avoiding databinding is not a big deal since it seems that MS guys have never created a real world database application and do not know what a real world database developer (RWDD) needs.

How should it work?
1. You should create the XSD file which describes the database structure (now you can at least do this with that ugly xml editor).
2. You should create VIEWS which describe that in a form how do you want to look at those data.
It would be similar to ColumnStyles except that it is not DataGrid dependent and works. The view should have the position and not an automagically created CurrencyManager. There is a sample JoinView in MSDN that does something similar.
3. You should create the form and EXPLICITLY define all the supporting objects. The form designer could help automagically create objects but NOT in runtime.
4. DataTables should be read when a VIEW references them. All the form could use data from the same DataTable trough different views.
Now you can design relations in the xsd editor but you cannot decouple calculated fields and lookups from the normalized database design (what goes to the xsd file). It is not a big problem in the MS sample applications but it is a pain in a real world app where you have to look at the same data in different ways.
5. With an enhanced serialization architecture there would be a chance to create bugfree bindable controls.
6. The controls should support what a RWDD expects (for example ReadOnly what is not equal with Enabled == false).
7. The data wizard should work by customizable templates. For example I hate always modifying the created SQL stored procedures. Why not create a cutomized???

The only problem is that I do not believe that MS will modify VS.NET so there is no chance only to help www.go-mono.com maybe they will implement an USABLE databinding architecture. (Hmmmm. Now I know that mono (GTK#) does not have any databinding capabilities so nevermind.)

Period

Appendix

Since I have started to answer questions in the windowsforms.databinding newsgroups I got some messages about my harsh opinion about databinding. Of cource they are right because I really do not know how to be polite in English. However here is a GEM I wrote.


NoiseEHC


Any feedback/corrections are welcome!