Browse multiple related tables in the ADO. Net dataset (3)

Source: Internet
Author: User
Browse multiple related tables in the ADO. Net dataset (3)
Author: MicrosoftWww.aspcool.comTime: 17:37:34 views:7915

Show data
This application uses a combo box, a list box, and an RTF text box to select and display data.

Add controls for selecting and displaying data

In Solution Explorer, right-click form1 (. CS or. VB, depending on the language of the Application), and then select View designer from the shortcut menu ).
In the left half of the form, add a ListBox control and set its name attribute to lborders.
In the right half of the form, add a RichTextBox Control and set its name attribute to rtbdetails.
Add a ComboBox control at the top of the list box and set its name attribute to cbcustomers.
Save the project.


Figure 1: Suggested layout of Form Controls

Now, you can add features to the application.

Set the display company name combo box

Select the combo box (cbcustomers) and set the following attributes: Property settings
Datasource dsnorthwind1
Displaymember MERs. companyName
Valuemember MERs. customerid

Fill a table with data
To fill the table with data, you must add code for the application.

Fill in data in the customer table and order table in the dataset (dsnorthwind1)

Double-click a blank area on the form to create an event handler for the form1_load event.
Add the following code:

'Visual basic
Private sub form1_load (byval sender as system. Object ,_
Byval e as system. eventargs) handles mybase. Load
'Close the constraints in the dataset.
Dsnorthwind1.enforceconstraints = false
'Fill the table with data.
Daorders. Fill (dsnorthwind1)
Dacustomers. Fill (dsnorthwind1)

'Re-enable the constraint.
Dsnorthwind1.enforceconstraints = true
End sub

// C #
Private void form1_load (Object sender, system. eventargs E)
{
// Close the constraints in the dataset.
Dsnorthwind1.enforceconstraints = false;

// Fill the table with data.
Daorders. Fill (dsnorthwind1 );
Dacustomers. Fill (dsnorthwind1 );

// Re-enable the constraint.
Dsnorthwind1.enforceconstraints = true;
}



Save the project.
Press F5 to run the application. The combo box contains a list of company names.
Close the form.
View related records in two tables
This section briefly introduces how to access data between two tables that constitute a one-to-multiple relationship in a dataset. After selecting a data row, you can call the getchildrows or getparentrow method and pass an appropriate data relationship to the data row to return relevant records.

Note: The getchildrows method returns data in the form of an array of datarow objects, while the getparentrow method returns only one single data row.
To demonstrate this function, you need to add code to the application to return all orders (sub-rows) of the customer selected in the combo box ). Changing the selected customer in the combo box will cause the ComboBox. selectedindexchanged event. The list box will fill in the order ID of each order of the selected customer.

You can call the getchildrows method based on the customer selected in the combo box. All related records in the order table will be allocated to the data row array named draorders.

Note: The next section will add the function of displaying the relevant order list in the list box. To confirm that the array contains related records, the length of the array (that is, the total number of orders of the selected customer) is displayed as the title of the form.
Create an event handler to obtain the order of the selected customer

In Solution Explorer, right-click form1 and select View designer from the shortcut menu ).
Double-click the combo box to create an event handler for the selectedindexchanged event.
Add the following code:

'Visual basic
Private sub cbcustomers_selectedindexchanged _
(Byval sender as system. Object, byval e as system. eventargs )_
Handles cbcustomers. selectedindexchanged
'Declare a string to save the customer ID of the selected customer.
Dim selectedcustomerid as string
Selectedcustomerid = cbcustomers. selectedvalue. tostring ()
'Declare a data row to save the records of the selected customer.
Dim drselectedcustomer as datarow
Drselectedcustomer = _
Dsnorthwind1.customers. findbycustomerid _
(Selectedcustomerid)
'Declare an array of data rows to save related records.
Dim draorders as datarow ()
Draorders = drselectedcustomer. getchildrows ("customersorders ")
'Display the length of the array in the form header (number of orders)
'And customer ID.
Me. Text = draorders. length. tostring () & "Order owner "&_
Selectedcustomerid
End sub

// C #
Private void cbcustomers_selectedindexchanged
(Object sender, system. eventargs E)
{
// Declare a string to save the customer ID of the selected customer.
String selectedcustomerid;
Selectedcustomerid = cbcustomers. selectedvalue. tostring ();

// Declare a data row to save the records of the selected customers.
Datarow drselectedcustomer;
Drselectedcustomer =
Dsnorthwind1.customers. findbycustomerid (selectedcustomerid );

// Declare an array of data rows used to save related records.
Datarow [] draorders;
Draorders = drselectedcustomer. getchildrows ("customersorders ");

// Display the length of the array in the Form title (number of orders)
// And customer ID.
This. Text = draorders. length. tostring () +
"Order owner" + selectedcustomerid;
}



Save the project.
Run the application.
Select another customer and check the Form title. The total number of orders of the selected customers and their customer IDs are displayed.

Close the form.

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.