After you create and save a DataWindow object, you can use it in a window. You have already created the d_custlist and the d_customer DataWindow objects. Now you associate each of these DataWindow objects with a DataWindow control in the w_customers window.
In this lesson you:
How long does it take?
About 15 minutes.
Where you are
> Attach the DataWindow object to the master DataWindow control
Attach the DataWindow object to the detail DataWindow control
Attach DataWindow objects to the Product window
Now you attach the DataWindow object to a DataWindow control in the w_customers window.
-
Expand the pbtutor.pbl branch in the System Tree.
-
Right-click w_customers and select Edit from the pop-up menu
or
Double-click w_customers in the System Tree.
The Window painter displays the w_customers window.
-
Right-click the top DataWindow control (dw_master) in the Layout view.
If the Properties view is not displayed, select Properties from the pop-up menu.
Click the ellipsis button next to the DataObject box in the Properties view.
The Select Object dialog box displays.
-
Select d_custlist in the DataWindows list box and click OK.
PowerBuilder associates the d_custlist DataWindow object with the dw_master DataWindow control.
The Layout view now shows the d_custlist DataWindow headings inside the dw_master control, but you do not see any data yet. The DataWindow does not execute its SELECT statement until you run the application.
Adding DataWindow objects to the window using drag and drop
In this tutorial, you use a custom DataWindow control that has built-in error handling. If you want to use the standard DataWindow control, you do not need to add the control to the window and then attach a DataWindow object to it as you did in this lesson. You can simply select the DataWindow object you want from the System Tree and drag it onto the window in the Layout view. PowerBuilder creates the DataWindow control for you.
Where you are
Attach the DataWindow object to the master DataWindow control
> Attach the DataWindow object to the detail DataWindow control
Attach DataWindow objects to the Product window
Now you attach a DataWindow object to the detail DataWindow control. The Window painter should still be open for the w_customers window.
-
Select the bottom DataWindow control (dw_detail) in the Layout view.
Click the ellipsis button next to the DataObject box in the Properties view.
Select d_customer in the Select Object dialog box and click OK.
PowerBuilder associates the d_customer DataWindow object with the dw_detail DataWindow control. The Window painter workspace now shows the d_customer DataWindow object inside the dw_detail control.
-
In the Layout view, make the dw_detail control larger so that you can see all of the columns you added to the DataWindow object.
If you need to, you can also enlarge the window to make more room. If you make the dw_detail control wider, you may also want to make the dw_master control the same width.
Where you are
Attach the DataWindow object to the master DataWindow control
Attach the DataWindow object to the detail DataWindow control
Attach DataWindow objects to the Product window
Now you run the application again to test the insert, update, and delete capabilities of the second DataWindow.
-
Click the Run button () in the PowerBar.
PowerBuilder prompts you to save your changes.
-
Click Yes.
The application begins running, and the login window displays.
-
Type dba in the User ID box.
Type sql in the Password box and click OK.
The database connection is established, and the MDI frame for the application displays.
-
Select File>Report>Maintain Customers from the menu bar.
The Customer window displays.
The top DataWindow control (dw_master) shows all of the rows retrieved from the Customer table. The hand pointer shows which row is selected.
The bottom DataWindow control (dw_detail) shows further information about the selected customer.
-
In the running application, click the Insert button in the toolbar
or
Select Edit>Insert from the Frame window menu bar.
This clears (resets) the dw_detail DataWindow, allowing you to add information for a new row that you will insert into the data source. The cursor is in the Customer ID box in the dw_detail control.
-
Add a new customer row by entering information in the boxes in the detail DataWindow.
Typing information for a new customer
The Customer ID number must be unique. To avoid duplicate numbers, use a four-digit number for your new database entry, or scroll down the list of three-digit customer numbers in the master DataWindow and select an ID number that does not appear in the list.
Enter values for the remaining fields.
The phone number and zip code use edit masks to display the information you type. You must enter numbers only for these data fields. To specify the state in which the customer resides, you must click the arrow next to the state column and select an entry from the drop-down list box.
-
Click the Update button in the toolbar
or
Select Edit>Update from the menu bar.
This sends the new customer data to the database and displays a confirmation message, as coded in the script for the ue_update event.
The new customer does not yet display in the master DataWindow. (You could add code to include this feature). However, if you open another instance of the w_customers sheet, the new customer data is visible in both the master and detail DataWindow controls.
-
Click OK in the message box.
Click a customer in the master DataWindow.
That customer data displays in the lower DataWindow.
-
Change the customer address in the detail DataWindow.
-
Click the Update button in the toolbar
or
Select Edit>Update from the menu bar.
This sends the revised customer data to the database and displays another confirmation message.
-
Click OK in the message box.
Select another customer in the master DataWindow.
That customer data displays in the detail DataWindow.
-
Click the Delete button in the toolbar
or
Select Edit>Delete from the menu bar.
The customer is deleted from the DataWindow immediately but is not deleted from the database unless you select the Update option on the Edit menu. In this particular situation, the Update operation may fail, because rows in other tables in the Demo Database may refer to the row that you are trying to delete.
You should be able to delete any row that you have added to the database.
-
Select File>Exit from the menu bar.
The application terminates and you return to the Window painter.
-
Close the Window painter.
Where you are
Attach the DataWindow object to the master DataWindow control
Attach the DataWindow object to the detail DataWindow control
> Attach DataWindow objects to the Product window
Now you add two DataWindow objects to the w_products window. These DataWindow objects are provided for you in the tutor_pb.pbl library.
-
Right-click w_products in the System Tree and select Edit from the pop-up menu
or
Double-click w_products in the System Tree.
The Window painter displays the w_products window.
-
If the Control List view is not open, select View>Control List from the View menu.
Select the dw_master DataWindow control in the Control List view.
Click the ellipsis button next to the DataObject box in the Properties view.
PowerBuilder displays the Select Object dialog box.
-
Select tutor_pb.pbl in the Application Libraries box.
Select d_prodlist in the DataWindows box and click OK.
The Data Object box in the Properties view of the Window painter now displays d_prodlist.
PowerBuilder associates the d_prodlist DataWindow object with the dw_master DataWindow control in the w_products window. You see the headings for the DataWindow object in the Layout view. You might need to resize the control and/or the window.
-
Click the dw_detail DataWindow control in the Control List view.
Click the ellipsis button next to the DataObject box in the Properties view.
The Select Object dialog box displays.
-
Select tutor_pb.pbl in the Application Libraries box.
Select d_product in the DataWindows list box and click OK.
PowerBuilder associates the d_product DataWindow object with the dw_detail DataWindow control. The Layout view now shows the d_product DataWindow object inside the dw_detail control. The d_product DataWindow object has seven columns labeled Product ID, Product Name, Product Description, Size, Color, Quantity, and Unit Price.
If necessary, in the Layout view, make the dw_detail control larger so that you can see all of the columns in the DataWindow object. You can also enlarge the window to make more room.
Where you are
Attach the DataWindow object to the master DataWindow control
Attach the DataWindow object to the detail DataWindow control
Attach DataWindow objects to the Product window
Now you run the application again to test the Product window.
At this point the Product window should have all of the capabilities of the Customer window. Like the Customer window, the Product window functions as a master/detail window, providing support for retrieval, insert, update, and delete operations against the database.
-
Click the Run button () in the PowerBar.
PowerBuilder prompts you to save your changes.
-
Click Yes.
The application begins running, and the login window displays.
-
Type dba in the User ID box.
Type sql in the Password box and click OK.
The database connection is established, and the MDI frame for the application displays.
-
Select File>Report>Maintain Products from the menu bar.
The Product window displays. The top DataWindow control shows all of the rows retrieved from the Product table.
The bottom DataWindow control shows information about the product selection in the top DataWindow control.
-
Select Edit>Insert from the menu bar.
This clears the dw_detail DataWindow and allows you to add a new row to the DataWindow. The cursor is in the Product ID box in the dw_detail control.
-
Add a new product row by entering information in the boxes in the lower DataWindow.
Use the Tab key to move from box to box.
-
Select Edit>Update from the menu bar.
This sends the new product data to the database and displays a confirmation message, as coded in the script for the ue_update event.
The new product does not display yet in the top DataWindow, but if you open another product sheet, the new information displays. If you want, you can add code to the Clicked event of the update button to automatically refresh the data in the master DataWindow control.
-
Click OK in the message box.
Click a product in the master DataWindow.
That product data displays in the detail DataWindow.
-
Change the product's unit price.
Select Edit>Update from the menu bar.
This sends the revised product data to the database and displays another confirmation message.
-
Click OK in the message box.
Select another product in the master DataWindow.
That product's data displays in the detail DataWindow.
-
Select Edit>Delete from the menu bar.
The product is deleted from the DataWindow immediately but is not deleted from the database until you select the Update option on the Edit menu.
-
Select File>Exit from the menu bar.
The application closes and you return to the Window painter.
-
Close the Window painter.