Properties of 15.5.1 Tdatasource parts
The Tdatasource part has the following attributes in addition to the name and tag attributes that are owned by other parts:
DataSet property: The name of the dataset from which the Tdatasource part obtains data, which can be the name of the ttable part, the name of the tquery part, or even the data set in the other window to be the value of the property. As in the following program, we specify that the Table1 in the form Form2 is the DataSet property value of the DataSource1 in the form Form1:
Tform1.formcreate (Sender:tobject);
Begin
Datasource1.dataset: = Form2.table1;
End
Enable property: The Enable property temporarily cuts the connection between the Tdatasource part and the dataset part to which it is connected. This is a Boolean variable. When its value is false, the connection between the Tdatasource part and the dataset part is cut off, and all data browsing parts that are connected to the Tdatasource part become blank and do not display any data information. When the enabled value becomes true, the Tdatasource part and the dataset part are restored, and the data browsing part that is connected to the Tdatasource part resumes displaying the data. To implement these features, however, you will not normally use the Tdatasource component's enabled properties, but instead invoke the Disablecontrols method and Enablecontrols method of the dataset part. Because these two methods are invoked, it is easy to control all the Tdatasource parts that are connected to the dataset and the data browsing parts that are connected to the Tdatasource part.
Autoedit property: This is a Boolean variable that describes whether the dataset that is connected to the Tdatasource part is placed in edit state. When the Autoedit value is true, the dataset part connected to the Tdatasource is automatically set to edit when the application runs, and the records in the dataset part change as the user enters a new value in the data-browsing part connected to the Tdatasource part. If the value of Autoedit is false, the user wants to modify the records in the dataset through a data-browsing part or program, you must call the Edit method of the dataset part to be able to do so after it is placed in edit state.
Events for 15.5.2 Tdatasource parts
The Tdatasource part has three events:
OnDataChange Events
Onstatechange
Onupdatadata
OnDataChange Event: The event is triggered when the position of the record pointer in the Tdatasource-connected dataset is changed, that is, when the program calls the dataset part's next, Previous, Insert, The event is triggered when a append method causes the position of the record pointer to change. This event is typically used to maintain synchronization between multiple parts in the application.
Onupdatadata Event: The event is triggered when the current record in the dataset part is about to be modified. For example, after a program calls the POST method but triggers the event before the modified data record is actually written back to the database file on the disk, the event is used when the non-data browsing part is used in the application to keep it synchronized with the dataset.
Onstatechange Event: The event is triggered when the state of the dataset part that is connected to the Tdatasource part is changed. Because the State property of the dataset part indicates the current status of the dataset part, when the state of the dataset changes, it is useful to use the event for processing, in a specific application, the state of the dataset component is often changed frequently, in order to track the state changes of the dataset part, The current state of the dataset part can be displayed on a label using the program code in the following example:
Tform1.datasource1onstatechange (Sender:tobject);
Var
s:string;
Begin
Case Table1,state of
Dsinactive:s: = ' Inactive ';
Dsbrowse:s: = ' Browse ';
Dsedit:s: = ' Edit ';
Dsinsert:s: = ' setkey ';
Dssetkey:s: = ' setkey ';
End
Label1.Caption: = S;
End
Similarly, we can control whether the buttons and menu items are valid by detecting the state of the dataset part. For example, there is a insertbtn button in an application form that controls the insertion of records into the database table corresponding to the dataset part Table1, and a cancelbtn button that controls whether the user modifies the current record or inserts a new record. The following program code controls the function of these two buttons based on the state of the Table1 (whether the form is dimmed or not).
Form1.datasource1onstatechange (Sender:tobject);
Begin
insertbtn.enabled: = (table1.state = dsbrowse);
cancelbtn.enabled: = Table1.state in [Dsinsert,dsedit,dssetkey]
End
In the code above, when Table1 is in Browse state (browse state), the user is not able to insert new records into the database table, at which point the insertbtn button will become dimmed or invalid. When Table1 is not in browse state, the insertbtn button is valid and the user is able to insert new records into the table. Similarly, the CANCELBTN button works only if the Table1 is in a specific state (insert state) or edit state (edit state) or lookup state (Setkey state), which means that the user can cancel the currently inserted record, modify the current record, and find the results.
15.6 Field part and field editor use
A field part is sometimes called a Field object that corresponds to a column in a database table, a field, a Field object that is not visible, and in Delphi there are two ways to create a field part:
① the field parts corresponding to each column in the database table are dynamically created as the dataset part is activated as the application runs.
② in the design process, programmers use the field editor (Fields Editor) to create permanent field parts, which will not change even if the structure of the database tables corresponding to the field objects has changed.
Since the field part corresponds to each field in the database table, and the fields in the database table have multiple data types, the field part has several types, and the type of the field part corresponds to the data type of the field in the database table, as shown in table 15.5.
Table 15.5 Types of field parts
━━━━━━━━━━━━━━━━━━━━━━━━━━━━
The type of the field part corresponds to the data type
────────────────────────────
Tstringfield a field of string type
Tsmallintfield a field of short integer type-32768-32767
Tintegerfield field of integer type
Twordfield a positive integer type field 0-65535
Tbooleanfield-Boolean field
Tfloatfield a field of a floating-point number type
Tcurrencyfield currency field
Tdatafield Date Type
Ttimefield Time Type
Tbcdfield fixed floating-point number with decimal digits
Tdatatimefield Date-Time field
━━━━━━━━━━━━━━━━━━━━━━━━━━━
We only describe the use of some common types of field parts in this book, and the use of other types of field parts can be seen in the online Help file.