I am currently working on a project for a client using Whidbey and WinForms and ran across an interesting issue with databinding. On this project we are binding our business objects to WinForm controls using the techniques outlined in my previous blogs:
http://blogs.interknowlogy.com/adamcalderon/archive/2005/05/15/151.aspx, http://blogs.interknowlogy.com/adamcalderon/archive/2005/05/25/158.aspx, http://blogs.interknowlogy.com/adamcalderon/archive/2005/06/14/210.aspx.
So in this design we bind directly to the properties of the business objects and the .NET framework handles the bi-directional binding with help from the events we raise out of our business objects. The issue I ran into was that when I selected an entry from a ComboBox and left the control to move to the next one on the form the value I just selected disappeared. Since I am working with Beta software I thought for sure I had found a bug. What I began to realize after a lot of digging is that I found a behavior that is directly related to the strongly typed foundation of .NET. It seems that the data source I used to fill the ComboBox entries, a strongly typed collection, contained a value that was not compatible with the property I was binding to. In this case my strongly typed collection was using a string value for the DisplayMember and another string value for the ValueMember. This in itself was ok but when combined with a property whose value was type integer, which is what the ValueMember would be bound to, caused a type mismatch. The thing that amazed me the most is that .NET did not raise an error. I just didn’t accept the value. I took this even further by using a TextBox and binding it to an integer and entering in characters. When I left the TextBox to move to another control the value disappeared as well. What I began to realize is that the values being assigned to the properties must be of the same type or the assignment will silently fail. To solve my CombBox problem I changed the strongly typed collection to be based on a string value for the DisplayMember and an integer value for the ValueMember and all worked well. I hope this is hopeful to you and helps you better understand how databinding works.