Tuesday, May 19, 2009

TableAdapters Part 2

Now for some more advanced training. In Part 2 we will build a link between 2 tables, one populating based on what was selecting in the first. If you want to follow along you will need the form built from Part 1.

In your Data Sources screen, choose to select a new source. You can use the same connection string and this time add the "Orders" table. Notice that .NET created a new dataset that contains the Orders table.



This took some work to learn, but is really cool, if anyone knows of a cleaner way, let me know, it seems there should be a more straightforward route. Right-click "NorthwindDataSet" and select "Edit DataSet with Designer". Do the same thing with the new dataset. Now you should have 2 .xsd files open, one with each table in it. Click on the Orders table and drag it to the tab of the other dataset, it should open that tab, and continue dragging until you get the icon for a safe drop and drop it. A relationship was created automatically and your screen should look something like this:









If the relationship was not made, or if you want another, feel free to do so, it is pretty self-explanatory and very familiar to Access users.



You should now see that the Orders table has been added to the original dataset, but what is even better is that if you expand Customers, you will see another Orders table:

What the heck is that? That my friends is a heck of a lot of work that you don't have to do! By adding the table to the dataset and including a relationship, .NET builds an Orders table that is a subset if you will of the current table. Long story short, by selecting a Customer from the top, you will see their related Orders on the bottom. If you were to use the Orders table beneath the customer table, and not the Orders table "inside" (hit the + on the Customer table and scroll down) then you would just see to tables, there would be no connection.

That is cool enough by itself, but expanding on it is even better. Instead of another datagrid, you could use a chart or a dial or a progress bar that could be bound to this second table so a change in one would change the controls bound to the other.

Next Stop: DataBinding


No comments: