Browsing multiple related tables in a Ado.net dataset (4)

Source: Internet
Author: User
Tags array
ado| data display related records
Now that you have the relevant records for the selected customer (stored in the data row array), you can display them to interact with the user. The application accesses the data in the array of data rows returned by the GetChildRows method one by one and adds the order ID of each related record as a separate item to the list box.

Note: Although this example accesses data in an array of related rows, the list box may already be using the property window and is bound to the related record through the DataSource property, the DataMember property, and the ValueMember property.
Display related records in a list box

In Solution Explorer (Solution Explorer), right-click Form1 and select View Code from the shortcut menu.
In the SelectedIndexChanged event handler for the combo box that you created in the previous step, add the following code to the code already in your handler:

' Visual Basic
' Clears the list of orders when the selected customer is changed.
LbOrders.Items.Clear ()
Rtbdetails.text = ""
' Add the order IDs for each related order to the list box.
Dim Drorder as DataRow
For each drorder in Draorders
LBORDERS.ITEMS.ADD (Drorder ("OrderID"))
Next

C#
Clears the list of orders when the selected customer is changed.
LbOrders.Items.Clear ();
Rtbdetails.text = "";

Add the order IDs for each related order to the list box.
foreach (DataRow drorder in draorders)
{
LBORDERS.ITEMS.ADD (drorder["OrderID"]);
}




Save the project.
Run the application.
List box displays the list of orders. Select another customer in the combo box and the order list will be updated.

Closes the form.
Browse related records in three or more tables
Browsing three or more tables is as simple as working with two tables. To learn how to work with more than two tables, add the Order details and product tables to the DsNorthwind dataset. When you select an order in the list box, the details of the order are displayed in the Rich text box.

To satisfy the constraint rules, you need to delete existing data relationships and recreate them in the future.

Temporarily delete the DataRelation in the DsNorthwind data set

In Solution Explorer (Solution Explorer), double-click Dsnorthwind.xsd to open it in the XML Designer (XML Designer).
Select an existing customersorders relationship and delete it.
Save the project.
Now you need to add the other two tables to the existing dataset and create a new DataRelation object to connect all the tables together.

Add order schedules and product tables to the DsNorthwind data set

In Solution Explorer (Solution Explorer), right-click Form1 and select View Designer from the shortcut menu.
Drag the Order Details table from the Server Explorer (Server Explorer) onto the form. A new data adapter is displayed in the component tray.
Select the new data adapter and set its Name property to daOrderDetails.
Drag the Products table from the Server Explorer (Server Explorer) onto the form. A new data adapter is displayed in the component tray.
Select the new data adapter and set its Name property to Daproducts.
The new table is only added to the form, so you must regenerate the dataset each time you add additional tables.

Rebuilding the DsNorthwind DataSet

From the Data menu, select the Generate DataSet (the build dataset).
Tip: Move the cursor over the form to use the Data menu.
Click Existing (existing), and then select DsNorthwind DataSet.
Select all four tables (Customer table, order form, order schedule, and product table).
Clear the Add this dataset to the designer (add the dataset to the designer) check box, and then click OK. A DataSet with additional tables is generated.
Note: If a dialog box appears stating "The file has been modified outside of the source editor." Do your want to reload it? " (The file has been modified outside of the source editor.) Do you want to reload it? , click Yes.
Keep in mind that generating a dataset does not automatically create relationships between the tables in the dataset.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.