• No results found

Displaying multiple views of a table

Applications often need to display two different views of the same dataset. For instance, if you have two forms, and each displays different columns of the same row, you need to find a way to keep these forms synchronized to ensure accurate and up-to-date views of the data in each form.

There are two main concepts important to working with multiple views of a table: • The TDataSource component provides access for multiple forms sharing a single

dataset.

• When you want more than one different view of one table, put a TDataSource on each form that contains data aware components. You only need put a single TTable or TQuery component on the first form.

The simplest way to do this is to place a TDataSource on each form, and connect the first data source to the dataset you want to access. At run time you can then set the DataSet property of the second TDataSource to the TTable on the first form, for example:

Form2.DataSource2.Dataset := Form1.Table1;

Once the second TDataSource is assigned to a valid dataset, both forms will remain synchronized.

The TWOFORMS.DPR example in \DELPHI\DEMOS\DB\TWOFORMS

demonstrates how to work with multiple forms and a single dataset. The program opens the COUNTRY table and shows the Name, Capital, and Continent fields on one form, and the Area and Population fields on a second form. A button on the first form opens the second form. Both forms have TDBNavigator components, so you can navigate through the table. The forms look like this:

Figure 3.9 Two forms

✔ To create the program manually,

1 Place a TTable, a TDataSource, a TButton, three TDBEdit, and three TLabel components on a form.

2 Give the Button the name “Detail.”

3 Set the Table1’s DatabaseName property to the DBDemos alias, and open the COUNTRY Table.

4 Connect DataSource1 to Table1, then connect each of the DBEdit component’s Datasource properties to DataSource1. By performing these steps in this order, you can drop down a list in the DataField property of the data aware controls.

5 Create a second form, and place a data source, a TBitBtn, two TDBLabels, a TDBNavigator and two TDBEdit controls on the form. Don’t yet connect the data source on this form to anything else.

6 Connect the DBEdit components to the DataSource, then enter ‘Area’ for the DataField property of DBEdit1, and ‘Population’ for DBEdit2.

7 Set the Kind property of the bitbutton to ‘OK’.

8 Name this second form DetailView, then save the whole unit under the name DETAILS.PAS.

Note When you are creating the second form, you might find it helpful to temporarily create a TTable component and link the data source to it. This enables you to design the form using live data, and gives you access to a list of field names for each edit control. Once you have the form set up properly, you can delete the TTable component and at run time reconnect the data source to a table on a separate form.

✔ Once you have built the second form, go back to the first form and add DETAILS to the

uses clause in the form unit’s implementation part, and create the following event handler for the OnClick event of the Detail button:

procedure TForm1.DetailClick(Sender: TObject);

begin

DetailView.DataSource1.DataSet := Table1; DetailView.Visible := True;

end;

This code first assigns the data source on the second form to the table in the first form. Once this connection is made, then the data source on the second form is “live.” That is, it will act exactly like the data source on the first form. Then it makes the form visible. ✔ Now, connect the navigators on each form to the appropriate data source.

When you run the program, open the second form by clicking on the Detail button. Notice that whether you use the navigator in either form, the edit controls on the other form remain synchronized with the current record.

✔ Close the application and add two more lines of code to the second form’s unit. This code is called in response to a click on the OK button:

procedure TDetailView.BitBtn1Click(Sender: TObject);

begin

DataSource1.DataSet := nil;

Close;

C h a p t e r 3 , U s i n g d a t a a c c e s s c o m p o n e n t s a n d t o o l s 97 This code sets the dataset tonil whenever the second form is closed. While not necessary, this ensures that the hidden detail view is not responding to events.

C h a p t e r 4 , U s i n g D a t a C o n t r o l s 99

C h a p t e r

4

Chapter 4

Using Data Controls

To display and edit data from a database, use the components on the Data Controls page of the Component palette. Data controls include components such as TDBGrid for displaying and editing all specified records and fields in a table, and TDBNavigator for navigating among records, deleting records, and posting records when they change. Figure 4.1 Data Controls Component palette

The following table summarizes the data controls in order from left to right as they appear on the Component palette:

Table 4.1 Data controls

Data control Description

TDBGrid Displays information from a data source in a spreadsheet-like grid. Columns in the grid can be specified at design time using the Fields Editor or at run time (dynamically bound).

TDBNavigator Provides buttons for navigating through data obtained from a data source. At design time, you can choose to include one or more buttons to navigate through records, update records, post records, and refresh the data from the data source. TDBText Displays data from a specific column in the current data record.

TDBEdit Uses an edit box to display and edit data from a specific column in the current data record.

TDBMemo Displays memo-type database data. Memo fields can contain multiple lines of text or can contain BLOB (binary large object) data.

TDBImage Displays graphic images and BLOB data from a specific column in the current data record.

TDBListBox Displays a list of items from which a user can update a specific column in the current data record.

TDBComboBox Combines a TDBEdit control with an attached list. The application user can update a specific column in the current data record by typing a value or by choosing a value from the drop-down list.

Most data controls are data-aware versions of standard components. Some, such as TDBGrid and TDBNavigator, are data-aware, but differ from standard components in significant, useful ways. A data-aware control derives display data from a database source outside the application, and can also optionally post (or return) data changes to a data source. Data controls are data-aware at design time, meaning that when you connect a component to an active data source while building an application, you can

immediately see live data in the controls. You can use the Fields Editor to scroll through records at design time to verify that your application is making the right database connections without requiring you to compile and run the application, but you cannot modify data at design time.

This chapter describes basic features common to all Data Control components, then describes how and when to use individual components.

Related documents