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

Source: Internet
Author: User
Tags header tostring
ado| Data Display Data
This application uses combo boxes, list boxes, and rich text boxes to select and display data.

Add controls to select and display data

In Solution Explorer (Solution Explorer), right-click Form1 (. cs or. vb, depending on the language of your application), and then select View Designer from the shortcut menu (View Designer).
In the left half of the form, add a ListBox control and set its Name property to Lborders.
In the right half of the form, add a RichTextBox control and set its Name property to Rtbdetails.
At the top of the list box, add a ComboBox control and set its Name property to Cbcustomers.
Save the project.


Figure 1: The proposed form control layout

Now you can start adding functionality to your application.

Set up a combo box that displays the company name

Select the combo box (cbcustomers) and set the following properties: property settings
DataSource DsNorthwind1
DisplayMember Customers.companyname
ValueMember Customers.CustomerID

Populating tables with Data
To populate a table with data, you must add code to your application.

Populating data in the Customer table and order table in a 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
' Closes the constraint in the dataset.
Dsnorthwind1.enforceconstraints = False
' Populate the table with data.
daOrders.Fill (DSNORTHWIND1)
Dacustomers.fill (DSNORTHWIND1)

' Open the constraint again.
Dsnorthwind1.enforceconstraints = True
End Sub

C#
private void Form1_Load (object sender, System.EventArgs e)
{
Closes the constraint in the dataset.
Dsnorthwind1.enforceconstraints = false;

Populate the table with data.
daOrders.Fill (DSNORTHWIND1);
Dacustomers.fill (DSNORTHWIND1);

Open the constraint again.
Dsnorthwind1.enforceconstraints = true;
}



Save the project.
Press the F5 key to run the application. The combo box now contains a list of company names.
Closes the form. Virtual Host
Browse related records in two tables
Here's a quick overview of how to access data between two tables that make up a one-to-many relationship in a dataset. After you select a data row, you can return its related records by calling the GetChildRows or GetParentRow method and passing the appropriate data relationship to the data row.

Note: The GetChildRows method returns data as an array of DataRow objects, and the GetParentRow method returns only a single row of data. Virtual Host
To demonstrate this functionality, you need to add code to the application to return all orders (child rows) for the selected customer in the combo box. Changing the selected customer in the combo box raises the Combobox.selectedindexchanged event, and the list box populates the order ID for each order for that selected customer.

You can invoke the GetChildRows method based on the customer selected in the combo box. All related records in the order table are assigned to an array of data rows named Draorders.

Note: The next section adds the ability to display a list of related orders in a list box. To confirm that the array does contain related records, the length of the array (that is, the total number of orders for the selected customer) is displayed as the title of the form.
Create an event handler to obtain an order for the selected customer

In Solution Explorer (Solution Explorer), right-click Form1 and select View Designer from the shortcut menu.
Double-click a 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
' Declares a string to hold the customer ID of the selected customer.
Dim Selectedcustomerid as String
Selectedcustomerid = cbCustomers.SelectedValue.ToString ()
' Declares a data row that holds records for the selected customer.
Dim Drselectedcustomer as DataRow
Drselectedcustomer = _
DsNorthwind1.Customers.FindByCustomerID _
(Selectedcustomerid)
' Declares a data row array to hold the related records.
Dim draorders as DataRow ()
Draorders = Drselectedcustomer.getchildrows ("CustomersOrders")
' Show the length of the array in the form header (number of orders)
' and the customer ID.
Me.Text = draOrders.Length.ToString () & "Order Owner" & _
Selectedcustomerid
End Sub

C#
private void cbcustomers_selectedindexchanged
(object sender, System.EventArgs e)
{
Declares a string that holds the customer ID of the selected customer.
String Selectedcustomerid;
Selectedcustomerid = CbCustomers.SelectedValue.ToString ();

Declares a data row that holds records for the selected customer.
DataRow Drselectedcustomer;
Drselectedcustomer =
DsNorthwind1.Customers.FindByCustomerID (Selectedcustomerid);

Declares an array of data rows to hold related records. Virtual Host
Datarow[] draorders;
Draorders = Drselectedcustomer.getchildrows ("CustomersOrders");

Display the length of an array in the form header (number of orders)
and the 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 for the selected customer and their customer ID are displayed.


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.