Thoughts from Dan Miser RSS 2.0
 Wednesday, July 05, 2006
Time to confess: I'm a fan of data binding. I've loved it ever since Delphi 3 made it workable by introducing TClientDataset. It's powerful, but makes application code fairly simple. Yes, it can be - and often, is - abused when used incorrectly, but overall, it's a good concept.

I recently worked on getting data binding in a .NET 2.0 application to work with a lookup combobox. The concept is pretty straight-forward (and very analogous to Delphi's TDBLookupCombobox). Databinding in .NET is pretty impressive, and a step forward from where Delphi currently is. For example, you can bind straight to business objects as opposed to being required to consume database resources/components to complete data binding. One big snag was in getting the lookup relationship to respect null values properly. For example, assume you have 2 very simple classes, Order and Supplier, that are bound via BindingSource components. Furthermore, assume you have your data binding set up properly to have a ComboBox set with DataSource=orderBindingSource, DisplayMember=Name, ValueMember=SupplierID, and nameComboBox.DataBindings.Add(new Binding("SelectedValue", this.orderBindingSource, "SupplierID", true)); (see this reference or this reference for more details). Lastly, if you populate your objects as in the example below, you will notice that the original display of the combobox is populated with the first item in the supplier list. If you scroll to the next record and then back to the first record, the combobox will display properly.


    private void btnPopulate_Click(object sender, EventArgs e)
    {
      orderBindingSource.Add(new Order(1, "Test1", null));
      orderBindingSource.Add(new Order(2, "Test2", 1));
      supplierBindingSource.Add(new Supplier(1, "Supplier X"));
      supplierBindingSource.Add(new Supplier(2, "Supplier Y"));
    }

There are a couple of ways to fix this behavior. First, you could just reverse the order and populate the supplierBindingSource first. Alternatively, you can leave the population order alone and just call ResetBindings() after you're done populating all of the data.


    private void btnPopulate_Click(object sender, EventArgs e)
    {
      orderBindingSource.Add(new Order(1, "Test1", null));
      orderBindingSource.Add(new Order(2, "Test2", 1));
      supplierBindingSource.Add(new Supplier(1, "Supplier X"));
      supplierBindingSource.Add(new Supplier(2, "Supplier Y"));
      orderBindingSource.ResetBindings(false);
    }

One other quick note. When you want to set the Order.SupplierID field to null, you cannot write code like this:


SupplierNameCombo.SelectedValue = null;

Instead, you can either set the SelectedIndex to -1, or (IMO) more elegantly, write code like this:


SupplierNameCombo.SelectedValue = DBNull.Value;
Wednesday, July 05, 2006 12:47:00 PM (Central Standard Time, UTC-06:00)  #    Comments [0] -
Delphi
Tracked by:
http://9nr-information.info/61837405/index.html [Pingback]
http://9nb-information.info/13279541/index.html [Pingback]
http://9nl-information.info/14565867/index.html [Pingback]
http://9na-information.info/10686093/coyle-and-casady-high-school.html [Pingback]
http://9na-information.info/69366150/book-bronze-bow.html [Pingback]
http://9np-information.info/39583582/reykjavik-car-rental-airport.html [Pingback]
http://9na-information.info/44236089/index.html [Pingback]
http://9ns-information.info/29933837/index.html [Pingback]
http://9nm-information.info/17368215/cornerstone-estate-agent-huddersfield.html [Pingback]
http://9nd-information.info/06547899/index.html [Pingback]
http://9nm-information.info/95021500/business-and-management-courses-at-english-... [Pingback]
http://9ns-information.info/97675074/index.html [Pingback]
http://9nf-information.info/72736104/index.html [Pingback]
http://9nk-information.info/94482447/prophey-marc-house.html [Pingback]
http://9ns-information.info/58875472/index.html [Pingback]
http://9oq-information.info/31691039/index.html [Pingback]
http://9og-information.info/84423723/used-truck-mount-carpet-cleaning-equipment.... [Pingback]
http://9oe-information.info/07462975/nyc-hip-hop-dance-school.html [Pingback]
http://9qg-information.info/35884873/vendita-box-salsomaggiore-terme.html [Pingback]
http://9ok-information.info/26069534/index.html [Pingback]
http://9qi-information.info/56266293/index.html [Pingback]
http://9rt-information.info/33436294/home-and-away-tv-show-australia.html [Pingback]
http://9rc-information.info/84074610/munster-car-show.html [Pingback]
http://9re-information.info/09992945/index.html [Pingback]
http://9rg-information.info/33993325/index.html [Pingback]
http://9rp-information.info/43911256/nba-west-all-star.html [Pingback]
http://9ro-information.info/73426825/index.html [Pingback]
http://9sd-information.info/91487232/index.html [Pingback]
http://9sq-information.info/68013981/index.html [Pingback]
http://9uafa-le-informazioni.info/32091752/index.html [Pingback]
http://9uaff-le-informazioni.info/08770219/index.html [Pingback]
http://9uafa-le-informazioni.info/02777283/udo-erasmus.html [Pingback]
http://9uaeq-le-informazioni.info/22068033/index.html [Pingback]
http://9uafd-le-informazioni.info/66784239/index.html [Pingback]
http://9uaei-le-informazioni.info/37044385/www-kawasaki.html [Pingback]
http://9uaed-le-informazioni.info/89508929/udine-ingegneria.html [Pingback]
http://9uafg-le-informazioni.info/59245222/index.html [Pingback]
http://9uafe-le-informazioni.info/24097846/index.html [Pingback]
http://9uaee-le-informazioni.info/34191567/index.html [Pingback]
http://9uafg-le-informazioni.info/66156886/index.html [Pingback]
http://9uahi-le-informazioni.info/75361671/index.html [Pingback]
http://9uags-le-informazioni.info/80976560/index.html [Pingback]
http://9uagh-le-informazioni.info/86601097/index.html [Pingback]
http://9uahd-le-informazioni.info/77119239/index.html [Pingback]
http://9uagb-le-informazioni.info/76404184/phuket-informazione-generali.html [Pingback]
http://9uahr-le-informazioni.info/61688995/index.html [Pingback]
http://9uahj-le-informazioni.info/81758600/index.html [Pingback]
http://9uagi-le-informazioni.info/43210576/garotas-do-programa-sao-paulo-br.html [Pingback]
Comments are closed.
Navigation
Archive
<July 2008>
SunMonTueWedThuFriSat
293012345
6789101112
13141516171819
20212223242526
272829303112
3456789
About the author/Disclaimer

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

© Copyright 2008
Dan Miser
Sign In
Statistics
Total Posts: 306
This Year: 21
This Month: 0
This Week: 0
Comments: 604
All Content © 2008, Dan Miser
DasBlog theme 'Business' created by Christoph De Baene (delarou)