I have this situation where I need to prepopulate a form(drop down menus)
with data from multiple tables in a database. How do I get multiple tables
within a dataset so that I can acheive this? Any example code would be very
helpful.
Hi! If you have a DataSet with multiple tables this is done easily.
1. Create a strongly typed DataSet (from your DB), just drag the tables you
need onto the form.
2. Right-click the sqlconnection (or whatever connection) you have and
choose "Generate DataSet".
3. Now add all the tables you want to the new dataset.
4. Choose to have an instance of this new strongly typed dataset added to
your form.
5. Use the DataSource property on your combobox along with ValueMember and
DisplayMember.
Example:
If you have a table named Orders, and one named Customers, you could list
these in two combos.
combo1.DataSource = YourDataSet.Orders;
combo1.ValueMember = YourDataSet.Orders.OrderIDColumn;
combo1.DisplayMember = YourDataSet.Orders.OrderIDColumn;
combo2.DataSource = YourDataSet.Customers;
combo2.ValueMember = YourDataSet.Customers.CustomerIDColumn;
combo2.DisplayMember = YourDataSet.Customers.FullNameColumn;