Browse multiple related tables in ADO.NET data set (3)

xiaoxiao2021-03-06  39

In ADO.NET dataset browse multiple related tables (3) Author: Microsoft www.ASPCool.com Time: 2003-1-23 17:37:34 Views: 7915

Display data This application uses a combo box, a list box, and an RTF text box to select and display data. Add selection and display data, in Solution Explorer, right-click Form1 (.cs or .vb, depending on the language of the application), then select View Designer from the shortcut menu (View Designer ). Add a ListBox control to the left half of the form and set its Name property to LBorders. Add a RichTextBox control in the form of the form and set its Name property to RTBDetails. Add a ComboBox control over the list box and set its Name property to CBCustomers. Save the project. Figure 1: Suggested Form Control Layout Now, you can start adding a function to your application. Set the combo box to display the company name Select the Combination box (CBCustomers) and set the following properties: Property Settings DataSource DSNorthWind1 Displaymember Customers.COMERID Use Data Fill Table To use data padding table, you must add code to your application. Fill in the customer table and the order table in the dsnorthwind1) Double-click the 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 E AS System.EventArgs) Handles mybase.load' Close the constraint in the data set. DSNorthWind1.enforceConstraints = false 'filled with data. Daorders.Fill (dsnorthwind1) Dacustomers.fill (dsnorthwind1) 're-on constraint. DSNorthWind1.enforceConstRAINTS = True End Sub // C # private void form1_load (Object sender, system.eventargs e) {// Turns the constraint in the data set. DSNorthWind1.enforceConstraints = false; // Fill in the table with data. Daorders.Fill (DSNorthWind1); Dacustomers.Fill (dsnorthwind1); // Re-open constraint. DSNorthWind1.enforceConstraints = true;} Save the project. Press the F5 key to run the app. Now there is a list of company names in the combo box. Close the form. Browse related records in two tables This briefly describes how to access data between two tables that constitute a pair of multi-relations in the data set. After selecting a data line, it can be returned by calling the getChildRows or getParenTrow method and passed the appropriate data relationship to the data line. Note: The getChildROWS method returns data in the form of a DataRow object array, and the getParenTrow method only returns a single data line.

To demonstrate this feature, you need to add some code to the application to return all orders (sub-rows) that selected customers in the combo box. Changing the selected customer in the combo box raises the ComboBox.selectedIndexchanged event, the order ID of each order of the selected customer will populate the selected customer. You can call the getChildRows method according to the customer selected in the combo box. All related records in the order table will be assigned to the data line array named Draorders. Note: The next section will add a function of displaying the relevant order list in the list box. To confirm that the array does include related records, the length of the array (ie the total number of orders of the selected customer) will be displayed as the form. Creating an event handler for obtaining a selected customer's order In the Solution Explorer, right-click Form1 and select View Designer from the shortcut menu. Double-click the combination 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' statement for a customer ID of the selected character string stored customer. DIM SELECTEDCUSTOMERID AS STRING SELECTEDCUSTOMERID = CBCUSTOMERS.SELECTEDVALUE.TOSTOSTRING () declares a data line for saving records of the selected customer. DIM DRSELECTEDCUSTOMER AS DATAROW DRSELECTEDCUSTOMER = _ dsnorthwind1.customers.FindbyCustomerid _ (SELECTEDCUSTOMERID) 'Declare a data line array for saving related records. Draorders As DataRow () Draorders = DRSELECTEDCUSTOMER.GETCHILDROWS ("CustomersRDERS") 'Displays the length (order number)' and customer ID of the array in the form header. Me.Text = draOrders.Length.ToString () & "Orders owner" & _ SelectedCustomerID End Sub // C # private void cbCustomers_SelectedIndexChanged (object sender, System.EventArgs e) {// declare a customer to save the selected customers The string of the ID. String selectedCustomerid; selectedCustomerid = cbcustomers.selectedValue.toString (); // Declare a data line to save the selected customer's record. Datarow DRSELECTEDETOMER; DRSELECTEDCUSTOMER = dsnorthwind1.customers.FindByCustomerid (SELECTEDCUSTOMERID); // Declare a data line array for saving related records.

转载请注明原文地址:https://www.9cbs.com/read-72613.html

New Post(0)