Data Binding a ComboBox to Child Collection

Tuesday, November 13, 2007

“One of the most frequent questions I get regarding data binding is how to hook up a combo box with a data bound list of selections from a related table or collection, and have selections in that combo box set a corresponding value in the related collection. For example, in the following form, the main collection is the Products table from Northwind. The textboxes are displaying a couple of the columns from that table, and the combo box is displaying the supplier name from the related Suppliers table. Products contains a foreign key column with a SupplierID that makes the link.

The code required to hook all this up in .NET 2.0 is very simple, although maybe slightly difficult to discover on your own. The solution below binds the controls to two BindingSource components, which is the new way of doing data binding in .NET 2.0. The BindingSource component provides a layer of indirection between your bound controls and their data sources that makes synchronizing bound controls easier, allows you to switch out the data source without needing to re-initialize bindings, and exposes a number of useful events to monitor what is going on in bound data sources in your code.

Basically all you need to do is get your data and set that as the data source on the two binding sources. Then set up a normal ComboBox data binding on the ComboBox with the DataSource, DisplayMember, and ValueMember properties.”

For a full set of instructions and a complete example, please see the full article on Brian Noye’s blog.



 
Indelv.com is for sale!
 
ERP systemen
Alle ERP-systemen op een rij, compleet met ERP-nieuws en ERP-software informatie.
www.ERPcentraal.nl
ERP systemen
Alle ERP-systemen op een rij.
www.erpmatrix.nl


Quick Links
Our Friends
Cool Places
Visit also
About Us