Студопедия
Случайная страница | ТОМ-1 | ТОМ-2 | ТОМ-3
АвтомобилиАстрономияБиологияГеографияДом и садДругие языкиДругоеИнформатика
ИсторияКультураЛитератураЛогикаМатематикаМедицинаМеталлургияМеханика
ОбразованиеОхрана трудаПедагогикаПолитикаПравоПсихологияРелигияРиторика
СоциологияСпортСтроительствоТехнологияТуризмФизикаФилософияФинансы
ХимияЧерчениеЭкологияЭкономикаЭлектроника

Displaying multiple views of a table

Читайте также:
  1. A) Complete the table with personal and professional abilities. Use the list below. Give the reasons.
  2. A. Prepare a talk, giving your own views on any one of these topics which you feel strongly about. Find some facts to support your idea.
  3. An The reacting Unstable The Products of
  4. Annex 7 TRANSLITERATION TABLE FOR GREEK
  5. ARW2 Summary table of literature sources
  6. B) Complete the following table of statistics. Fill in the missing percentage using figures from the box.
  7. B) Do the multiple choice test given below in figures and letters.

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, youcan 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 datasource 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 formusing 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 eventhandler 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;

end;

This code sets the dataset to nil whenever the second form is closed. While not necessary, this ensures that the hidden detail view is not responding to events.

 


Дата добавления: 2015-10-26; просмотров: 153 | Нарушение авторских прав


Читайте в этой же книге: ДИСКУССИЯ С БУДДИСТАМИ | ДИСКУССИЯ С УЧИТЕЛЯМИ | Using the FieldByName method | Data type mappings |
<== предыдущая страница | следующая страница ==>
Creating a master-detail form| Удельный вес расходов на данное благо в доходе потребителя.

mybiblioteka.su - 2015-2024 год. (0.007 сек.)