It had been a while since I had done some WinForm development, with all of the rage in web development for the last few years, so I was pleasantly surprised to see that Microsoft finally got DataBinding right. My early experiences with the technology were very bad leading me down a path that caused a re-write on a project, so I was a little apprehensive when I looked at the technology again. But I was pleasantly surprised to see that it work really nicely. I was most impressed with the ability to bind UI controls (textboxes, dropdowns and the like) to objects. I have always believed that working with objects lends itself to a cleaner implementation but have been disappointed with the choices available to connect my business objects with the user interface. Using the GotFocus and LostFocus events to tie your business objects to your user interface just seemed like way to much work and if you tried another route of waiting till the user hit say the save button that sometimes lead to way to many error to deal with at one time. With DataBinding, it’s easy to tie your business objects to UI controls. All it takes is a simple syntax like this:
TextBox1.DataBindings.Add(“Text”, MyBusinessObject, “CustomerName”)
What you see here is a textbox having it’s text property bound to the CustomerName property of the MyBusinessObject. There is a great deal to talk about here as to how this works not only in .NET 1.0/1.1 but in.NET 2.0 as well which I will detail in some up coming blogs. I will also cover the total tie in by connecting your business objects to UI controls which are then all tied to the an ErrorProvider control to provide a complete user experience based on objects and events. Until my next blog I would recommend following up on DataBinding by visiting the links below.
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbcon/html/vboriwindowsformsdataarchitecture.asp – this link on MSDN provides a good starting point to get introduced to data binding. The topics in these links mainly cover binding to ADO.NET but the underlining things they talk about like the CurrencyManager and the BindingContext (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbcon/html/vbconconsumersofdataonwindowsforms.asp) are there regardless of what you are binding to.
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnadvnet/html/vbnet02252003.asp – this link on MSDN begins to cover binding objects to UI controls and builds upon what you learn in the earlier link.
http://www.code-magazine.com/Article.aspx?quickid=0409051 – for those who are really adventurous, this article in CoDe magazine written by Steve Lasker, a Program Manager on the VB.NET team, talks about databinding in Whidbey and is a very interesting read.