16th chapter-Application and programming of data browsing parts (i) (2)

Source: Internet
Author: User
Tags modify

16.1 Basic characteristics of data browsing parts

Most of the data-browsing widgets evolved from standard components, which have similar characteristics, such as Tdbgrid and Tstringgrid parts, Tdbedit and tedit parts, Tdblistbox and Tlistbox, etc. Only the data browsing part is a field information that is specifically used to display and edit records in the database. The way they are used and the attributes are very different. In the program design phase, when the data browsing part is connected to a physical database through the Tdatasource part and the ttable part, you can view the data information in the database, that is, you can see the effect after the application runs in the program design phase. This feature gives us the convenience of modifying the application.

The data browsing part is connected to the specific field part of the ttable part through the Tdatasource part, thus the data control part has some common properties for connecting the Tdatasource parts and Tfield parts.

DataSource property: Describes the data source part Tdatasource The data browsing part connection, the data browsing part obtains the data from the Tdatasource part.

DataField property: Describes the actual field name in the database table for the data browsing part. The value of this property is actually the name of the field part in the Tdatasource connected dataset part Ttable or Tquery part.

Therefore, to create an application to display and edit records in a database table, you typically place a dataset part (ttable part or tquery part) and at least one data source part Tdatasource part and multiple data browsing parts in the application form. The general steps for its creation are as follows:

1. Place the said part in the form and connect the dataset part, the data source part.

2. Set the DataSource property value for each data browsing part the name of the Tdatasource part that exists in the window.

3. Set the DataField property of each data browsing part to the name of the field part that exists in the dataset part Ttable or Tquery part.

Note here that the Tdbgrid part and the Tdbnavigator part are automatically accessing all the available field parts in the dataset part, so they do not have datafield properties, and you can skip step 3rd for these parts.

Enabled property: When a data-browsing part is connected to a dataset part, its Enabled property determines whether the data browsing part can accept messages from the mouse, keyboard, and timer events. When the Enabled property value is False, the data browsing part becomes invalid and cannot accept outside information. When the Enabled property of the Tdatasource part that is connected to the data browsing part is false or if the active property of the dataset part Ttable or Tquery part connected to the data source part Tdatasource part is false, The data browsing widget will also become invalid.

ReadOnly Property: Most data browsing widgets can be used to edit the fields that correspond to them, so there is a readonly property that controls whether the value of the modified field can be edited in data browsing. By default, the value of this property is false, which means that the user can edit the value of the Modified field.

Of course, users need to be constrained by other factors when they want to edit the record fields in a database table through the data browsing part. In addition to the ReadOnly property set to False for the data browsing part itself, set the Canmodify property true of its corresponding field part and dataset part, and set the Autoedit property of the Tdatasource part to true. If the database table is a database table in the SQL database server, the user must have permission to read and write to the database, and so on. Of course, in the actual program design process is not so cumbersome, because many properties of the default value is to allow users to modify the records in the table.

In addition to the Tdbgrid part, if the value of a field is modified through a data-browsing part, the modified value in the data-browsing part is automatically written back to the disk database when the cursor (or focus) leaves the data-browsing part. If the ESC key is pressed before the focus leaves the data browsing part, Delphi automatically discards its modification of the field values. When you modify records in a table in the Tdbgrid part, the user's modifications to the current record are written back to the database table on the disk when the focus leaves the current record, or when the record pointer moves to another record, and Delphi automatically discards the changes to the current record when the focus does not leave the current record.

16.2 using the Tdbtext part to display the data in the table

The Tdbtext part is a read-only data browsing widget, similar to the Tlabel part. Only the Tdbtext part is used to display the value of the specified field recorded in the database table. Because the Tdbtext part displays the value of the specified field for the current record in the table, its display is dynamic, and the content displayed changes as the record pointer moves. Using the Tdbtext part to display the company field information in the Customer.db table can be achieved with the form shown in Figure 16.2.

The property settings for each part are shown in table 16.2

Table 16.2 property settings for each part of the table

━━━━━━━━━━━━━━━━━━━

Property property Value

───────────────────

Table1.databasename DEMOS

Table1.tablename customer.db

Datasource1.dataset Table1

Dbtext.datasource DataSource1

Dbtext.dbfield Company

━━━━━━━━━━━━━━━━━━━

16.3 using the Tdbedit part to display and edit data in a table

The Tdbedit part is a data browsing widget that is designed to display the values of each field of the current record in the editing database table, and in the application we often use a tdbedit part to correspond to a field in the table by setting the DataSource of the Tdbedit part, The DataField property can specify the corresponding field in the table for the Tdbedit part. If the user wishes to modify the field values in the database table through the Tdbedit part edit, set the ReadOnly property of the Tdbedit part to false. Set the Autoedit property of the Tdatasource part of the Tdbedit-connected data source part to true and ensure that the dataset part ttable or Tquery part that is connected to the Tdatasource part is in edit state. That is, set their Canmodify property to true.

For example, in the form shown in Figure 16.3, multiple tdbedit parts are used to display and edit the fields of the current record in the Customer.db table. The properties of each part in the form are shown in table 16.3.

Figure 16.3 Displaying and editing data in a table with the Tdbedit part

Table 16.3 Properties of each part in a form

━━━━━━━━━━━━━━━━━━━━

Property property Value

────────────────────

Table1.databasename DEMOS

Table1.tablename customer.db

Datasource.dataset Table1

Datasource.autoedit True

Dbnavigator.datasource DataSource1

━━━━━━━━━━━━━━━━━━━━

Other parts of the form are Tlabel parts and tdbedit parts, and the Tlabel part is used to display the names of the fields in the table, tdbedit the fields in the corresponding table of the part. The program is run as shown in Figure 16.4. The user can modify the field values in any one of the tdbedit parts.

Display and modify data in a table with the Tdbedit part

A tdbnavigator part is also used in the form to move the record pointer in a table, to modify, insert, delete records, and to use and manipulate the section 16.5 tdbnaigator parts.

16.4 display and edit the data in the table with the Tdbgrid part

Tdbgrid parts, like Tdbedit parts, are designed to display and edit data in a database table, but the Tdbgrid part is more powerful, and it can display all the field information for all records in a database table as a grid.

Displaying record information in a database table with Tdbgrid

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.