Multi-layer database development 12: Using data controls

xiaoxiao2021-03-06  99

Chapter 12 Using Data Controls In the database application, data controls are often used. Different data controls are visible to the data set components such as TTABLE, TQUERY, TSTOREDPROC, TCLIENTDATASET. That is, if the properties of these components are modified, it can be reflected immediately on the form. If the enabled attributes of these components are set to True and the Active property of the data set is also set to TRUE, data can be seen during the design period.

12.1 What data controls in Delphi 4

In the IDE of Delphi 4, the data controls are located on the "Data Controls" page of the component palette. These data controls are briefly introduced below:

.TDBGRID displays data with rows and columns.

.Tdbnavigator is used to navigate the record of the entire database, such as turning forward a record, turn back a record, turn to the first record, turn to the last record, etc.

.TdbText Displays the content of the field as the label.

.Tdbedit Displays the contents of a field in the form of an edit box.

.Tdbmemo Displays the contents of the note field in the form of a multi-line text editor.

.Tdbimage is used to display the image field in the database.

.Tdblistbox displays data in the form of a list box.

.TdbcomboBox displays data in the form of a combo box.

.Tdbcheckbox displays data in the form of a check box.

.TdbradioGroup displays data in the form of a single-select box.

.Tdblookuplistbox displays the data of the LookUp table of this table in the form of a list box.

.TdblookuppcomboBox displays the data of the LookUp table in the form of a combo box.

.Tdbrichedit Displays the contents of the note field in RTF format.

.TDBCTRLGRID is similar to TDBGRID, but each unit can set attributes separately.

.Tdbchart Displays the data of the database in the form of a chart, usage is similar to TCHART.

The above data control has the characteristics of data perception in the design period. If the DataSource property is set correctly, you can see the data immediately without compiling and running the program.

12.2 Basic Law of Data Control

This section describes the basic usage of data controls, including how to specify a data source, how to edit and update data, how to ban and allow data to refresh.

12.2.1 Specify a data source

Data controls must be connected to the data set through the TDataSource component. The role of the TDataSource component actually is the bridge between the data control and the data set.

First, put a data set member on the form or data module, set its DatabaseName property to specify the database to be accessed, set its TableName property to specify the table to access.

Next, put a TDataSource component onto the form or data module, set its DataSet property to specify a data set.

Then, put a data control on the form, set its DataSource property to specify the TDataSource component, and the DataSet property of this TDataSource component has specified a data set.

Finally, set the DataField property of the data control to specify the fields to display. However, for TDBGRID, TDBCTRLGRID, and TDBNAVigator components, it is not necessary to set the DataField property, because these controls are working with the entire data set.

12.2.2 Editing and updating data

In addition to the TDBNAVigator components, other data controls are used to display and edit data. Here you need to introduce how to edit data.

To allow users to edit data, the data set must enter the Dsedit state. If the TDataSource's AutoEdit property is set to false, the user cannot edit the data directly, unless the program calls the Edit function.

To make the user to modify the data in the data control, you must set the readOrthly property of the data control to False. If the ReadOnly property is set to true, the data displayed in the data control is read-only. In general, the enabled attribute of the TDataSource component is set to True. If this property is set to FALSE, the data control cannot display data, and the data cannot be modified.

If the ReadOnly property of the data set component is set to true, the data set is read-only, and the modification of the user in the data control cannot be written to the data set.

In addition to the TDBGRID component, when the user modifies the value of a field, it is necessary to remove the input focus, and the new data is written to the data set. Before removing the input focus, the user can press the ESC button to cancel the modification.

In the grid established in the TDBGRID component, when the user modifies the value of a field, you need to move the input focus to another record, and the new data is written to the data set.

12.2.3 Prohibition and allow data to refresh

When the program is traversing the entire data set or searches for a specific record, the data control should be temporarily disabled, so that the speed of traversal or search can be quickly blinking.

DisableControls calling the dataset can temporarily disable the connection data of the data control to connect to this data set. The DisableControls function is usually called before the loop operation. After the cycle is over, the program should immediately call the EnableControls function of the data set component to re-refresh the data.

In order to ensure that it is always possible to recover refresh, it is recommended to use the Try ... Finally structure. In this way, even if an abnormality occurs in the cycle, it is also guaranteed to call EnableControls.

The following code demonstrates how to call the DisableControls and EnableControls functions:

CustTable.disableControls;

Try

CustTable.first;

While Not CustTable.eof DO

Begin

...

CustTable.Next;

END;

Finally

CustTable.enableControls;

END;

12.2.4 Manually refreshing data

Refresh's refresh can read the latest data in the data set and refresh the data control, which is especially useful in multi-user environments, as other users have changed data in the data set.

Sometimes, calling Refresh may lead to unexpected results. For example, if another user has already deleted a record, the record will disappear from the data control after calling Refresh.

12.3 Displaying the data control of a single field

Some data controls use a database of one or a few fields as work content, such as TDBText and TDBedit, and some data controls work with the entire dataset, such as TDBGRID and TDBNAVIGATOR.

The data controls that display a single field are often evolved from a standard Windows control, for example, the TDBEDit component can be considered to be TEDIT's data perceived version.

Figure 12.1 Displaying the data as a label with a TDBText component

12.3.1 Display data as a label

The TDBText component is a read-only data control that is very similar to the TLABEL component and the TSTATICText component. The TDBText component can display the data as a label to label other controls. For example, the name of the fish can be displayed in a picture of the fish in a picture of a TdbText component, as shown in Figure 12.1.

The TDBText component needs to specify a field. When the user uses a navigator or other means to browse the record, the data displayed by the TDBText component will automatically change because the TDBText component always displays the current recorded data. For example, in Figure 12.1, when the user clicks on the arrow at both ends of the roller of the grid, the fish's picture will automatically change, and the name of the fish also changes accordingly, all of which does not need to be programmed. The AutoSize property of the TDBText component is generally set to TRUE because the content of the field may be different. If the AutoSize property is set to False, some longer content may be truncated.

12.3.2 Display and editing data

The TDBText component can only display data and cannot edit data. To display data and edit data, you should use the TDBEDit component. TDBEDIT can be considered TEDIT's Data Performance (Data-Aware).

For example, there is a TDataSource component called CustomersSource, and its DataSet property points to a TTable component called CustomersTable. Place a TDBEDit component on the form, and its DataSource property is set to CustomersSource and set its DataField property to Custno. This TDBEDIT component is now able to display the value of the CustNo field. Users can type a new value in the edit box.

12.3.3 Display and editing multi-line text

The TDBMEMO component is a data perceived version of the TMEMO component, which shows the contents of the DBASE and Paradox database.

Unlike TDBEDIT, TDBMemo can display text in multi-line form while allowing users to type multi-line text.

By default, TDBMEMO allows users to modify the text it displayed. If you don't want the user to modify the text, just set the ReadOnly property to true. To allow the user to insert a tab in text, the WantTabs property should be set to true, otherwise, when the user presses the Tab key, the input focus will be removed instead of inserting a tab.

To limit the number of characters that the user can enter, you can set the MAXLEngth property. If this property is set to 0, there is no limit.

In addition, the scrollbars property can set whether or not to add the scroll bar, the WordWrap property can set whether to allow automatic windback, the Alignment property can set the alignment of the text.

At runtime, you can call the CutToclipboard and CopyToClipboard function to cut the selected text and copy it into the clipboard, call the PasteFromclipboard to paste the text in the clipboard. If the AutodisPlay property is set to True, the TDBMEMO component is automatically refreshed when the content specified by the DataField property changes. If the AutodisPlay property is set to false, only the field name is displayed on the TDBMEMO component, and the user must double-click this component or program to call LoadMemo to refresh the data.

12.3.4 Display text in RTF format

The TDBRICHEDIT component can be considered a data perceived version of the TricHEDIT component for displaying the formatted text in the Blob field in the RTF format. Its usage is similar to the TDBMEMO component, and multi-line text can be displayed.

Note: Although the TDBRICHEDIT component can display the text of the RTF format and provide a strong editing function, it does not provide the user interface, the application must design the corresponding user interface to play the powerful function of the TDBRICHEDIT component. .

By default, the TDBRICHEDIT component allows the user to type new text. If you don't want the user to modify the text, you can set the ReadOnly property to true.

To allow the user to insert a tab in text, the WantTabs property should be set to true, otherwise, when the user presses the Tab key, the input focus will be removed instead of inserting a tab. To limit the number of characters that the user can enter, you can set the MAXLEngth property. If this property is set to 0, there is no limit.

If the AutodisPlay property is set to True, the TDBRICHEDIT component will be automatically refreshed when the content of the field specified by the DataField property changes. If the autodisplay property is set to false, only the field name is displayed on the TDBRICHEDIT component, and the user must double-click this component or program to call the loadMemo function to refresh the data.

12.3.5 Display and edit the image

The TDBIMAGE component can be considered a data perceived version of the TIMAGE component, which can display the contents of the blob field. After the TDBIMAGE component retrieves the image from the data set, the TDBIMAGE component has established a copy in the DIB format.

You can call the CutToClipboard or the CopyToclipboard function to cut the image or copied to the clipboard, call the PasteFromClipboard to paste the image from the clipboard.

If the Stretch property is set to True, the image will be automatically scaled to accommodate the size of the TDBIMAGE component, which may cause image deformation. If the AutodisPlay property is set to True, the TDBIMAGE component will be automatically refreshed when the content of the field specified by the DataField property changes. If this property is set to false, only the field name is displayed on the TDBImage component, and the user must double-click this component to refresh the data, of course, can also call LoadPicture to refresh the data.

12.4 Display and edit data with list boxes and combo boxes

There are 4 special data controls to display and edit data with list boxes and combo boxes, which can be considered as a standard list box and a data perceived version of the combo box. The following is a brief introduction to these 4 data controls:

.Tdblistbox Displays a set of data with a list box to select a value from it.

.TdbcomboBoX Displays a set of data with a combo box to allow the user to select a value.

.Tdblookuplistbox Displays a set of data in another dataset with a list box, allowing the user to select a value.

.TDBlookUpComboBoX Displays a set of data in another dataset with a combo box to allow the user to select a value.

12.4.1 TDBListBox

The TDBListbox component can display a set of data in a list box, and the user can select a data. When a user browses the record, the program will automatically search for items that match the value of the field in the list box. If this is selected, as shown in Figure 12.2.

In the list box, select the match in turn, when the user selects an item in the list box, the program automatically changes the value of the current record in the list box to the list box. Of course, to make modifications that must also call the POST function.

To set the item displayed in the list box in the Design period, you can open a string list editor in the ITEMS properties, then enter some strings.

If the IntegrarHeight property is set to true, the height of the list box will always be an integer multiple of the height.

12.4.2 TDBCOMBOBOX

The TDBCOMBOBOX component is actually a data perceived version of the TcomboBox component, which can display a set of data in the form of a combo box, allowing the user to select a value from the list or enter a value directly.

The Items property is used to set a set of data to display in the list. At the design period, you can open a string list editor in the Items property, then enter some strings.

The DropDownCount property is used to set the number of items that can be displayed when the user is dropped. The number of items that can be displayed will be displayed. The default is 8. If the number of items exceeds 8 when the user dropped combination box, the scroll bar is added. If the actual number of items has not been specified in the DropDownCount property, the height of the pull-down combo box is automatically reduced. In the case where the Style property is set to CSOWNERDRAWFIXED, the itemHeight property is used to set the height of the item.

12.4.3 Displaying the data in another data set

The TDBlookUplistbox components and TDBlookUpComboBox components displays data in another data set in the form of a list box and combo box, respectively.

Suppose there is a table called ORDERSTABLE, which contains a CustNO field that uses the customer's number, but other information from the customer is not included in addition to the number of the customer in the ORDERSTABLE table. And another table is supposed to be CustomersTable, in addition to the CustNo field, there are information such as corporate names, addresses such as customers.

The TDBLookUplistbox component can achieve such functions. When the user browsses the record in ORDERSTABLE, the program first looks for a record that matches the CUSTNO field in CustomersTable. If you can't find it, you find a string that matches the company field from the list; if you find Select this as shown in Figure 12.3.

Figure 12. Usage of TDBlookUplistbox components

When the user selects an item in the list, the program finds the COMPANY field that matches the string in CustomersTable. If found, replace the CustNo field in the OrderStable table with the value of the CustNo field.

12.5 Treat Boolean fields with checkbox

The TDBCHECKBOX component can be considered a data perceived version of the Tcheckbox component for processing the Boolean type field. For example, you can use a checkbox to indicate whether the customer has paid.

The TDBCHECKBOX component is actually compared to the value of the field with a preset string, which is specified by the valueChecked and the valueunchecked property. If the value of the field matches the string specified by the valueChecked property, select the check box. If the value of the field matches the string specified by the valueunchecked property, the check box is not selected. Note: The ValueChecked property and the string specified by the Valueunchecked property cannot be the same.

In general, the valueChecked property is set to "True", "YES" string, but it can also be any other string, even a set of strings, and the semicolon is separated from each other, for example:

Dbcheckbox1.valueChecked: = 'true; yes; on';

In the above case, when the value of the field matches one of the strings, select the check box. To explain, the string specified by the ValueChecked property is sensitive.

In general, the valueunchecked property is set to "false", "NO" string, but it can also be any other string, even a set of strings, separated from each other.

If the value of the field does not match the string string specified by the valueChecked property, it is not matched to the string string specified by the valueunchecked property, and the check box is as gray.

12.6 Value of fields with single-selection grouping box

The TDBRADIOGROUP component can be considered a data perceived version of the TRADIOGROUP component that limits the value of the user from a set of data.

Like the TRADIOGROUP component, first set the items property to specify which items to display in the radio group box. The Items property is a typical TStrings object, each of which corresponds to a button in the radio group box. When the user browsses the record, if the value of the field matches the label of a button in the radio group box, select this button, as shown in Figure 12.4.

Figure 12.4 Select the radio box that matches the value of the CustNo field

Conversely, when the user selects a button in the radio group box, the program assigns the field specified by this button to the DataField property.

If you do not want the button's tag to match the value of the field, you can specify another string, which is used to use the value of the value. The VALUES attribute is also a TStrings object that specifies a set of strings. When the user selects a button in the radio group box, the program assigns a string in the values ​​property to the field specified by the DataField property instead of the button label.

12.7 Using TDBGRIDTDBGRID components

Display and edit data in the data set in the form of a grid. Its appearance depends largely on the following three factors:

One is a permanent column object.

Second, a permanent field object.

. Third, the ObjectView property of the data set component will affect the display mode of the ADT and array fields.

For TDBGRID components, the most important properties are columns, which is a TDBGRIDCOLUMNS object for managing a set of TColumn objects. At the design period, you can open an editor to establish a permanent column object and then set the properties of the column object in the object viewer.

12.7.1 Dynamic column object

If the State property of TDBGRIDColumns is set to CSDefault, the column is dynamically generated, the attributes of the column depends on the properties of the field. When the properties of the field changes, the properties of the column are followed.

The benefit of letting the column dynamically generated is to dynamically select other datasets during the runtime, without worrying about whether the raster is suitable for displaying a new dataset. For example, you can first display a paradox table with the same TDBGRID component, then display the results of the query another database.

In the design period, the properties of the dynamic column object cannot be directly modified, which can only modify the properties of the field object, and indirectly modify the properties of the dynamic column object.

The life of the dynamic column object also depends on the life of the field object. If the data set does not establish a permanent field object, then all dynamic column objects will also disappear when the data set is turned off.

Note: If you set the State property of TDBGRIDCOLUMNS to CSDefault at the runtime, all columns are removed, and then reconstruct the column object according to the field object in the data set.

12.7.2 Permanent column object

To be able to customize the grid in the design period, you should use a permanent column object. After establishing a permanent column object, if the State property of TDBGRIDCOLUMNS is set to CSCUSTomized, the properties of each column can be set independently. For example, by default, the title of the column shows the tag of the field, the displaylabel property, by modifying the TColumnTitle's CAPTION attribute, and the TField's DisplayLabel property will not be affected.

The State property of TDBGRIDCOLUMNS is set to CSCustomized Structure for those datasets is fixed. If you need to switch different datasets in the running period, you cannot set the State property to CSCUSTOMized.

To create a permanent column object, first select the TDBGRID component on the form, then click the omit button on the edge of the Column attribute in the object viewer to open the editor shown in Figure 12.5.

Figure 12.5 Creating a permanent column object

At the beginning, this editor is blank, because by default, the column objects in the grid are dynamically generated, and there is no permanent column object. To create a permanent column object based on each field in the data set, click the right mouse button, select the "Add All" command in the pop-up menu.

To create a separate permanent list, you can click the (Add New button on the toolbar. Select this active column object, then set the fieldName property in the object viewer to specify a field, set the title of the Caption property specified.

To delete a column object, you can click the (Delete SELECTED button on the toolbar. If the permanent column is deleted, the grid can display all the fields in the data set, because the permanent column deletes, Delphi 4 automatically sets the TDBGRIDCOLUMNS's State property to CSDefault and dynamically generates all columns. To adjust the order in which the column is displayed in the grid, you can use the mouse to move the column object forward or post.

For a permanent column object, its default value of its attribute is still tapped in the field unless you have modified the properties of the permanent column object. For example, in the default, the title of the column is the DisplayLabel attribute of the field. If you modify the DisplayLabel property of the field, the title of the column will change. However, once you modify the CAPTION property of the column object, the title of the column is no longer associated with the DISPLAYLABEL attribute of the field, which is independent of each other.

As mentioned earlier, when you create a permanent column object, you need to set the fieldName property to specify a field. However, you can also make the fieldName property empty. At this time, the Field property of the TColumn object will return null, and the column is blank in the grid. Blank columns are often used for users to display some custom content, such as images or charts.

The fieldName properties of several column objects can be set to the same field. It can be seen that TDBGRID's fieldcount attribute may be less than the number of columns of the grid.

12.7.3 Let users edit data

For a permanent column object, you can provide several ways to edit data. First, display the data of another dataset in a combination box, and the other is to create a static list, and the third is to open a dialog with the omitted button.

To display the data of another dataset in a combination box, you must first add a lookup field in the data set, then create a permanent column object, set its fieldname property to specify this lookup field, and set its buttonstyle attribute to CBSAUTO. After the above operation, the effect of the grid is shown in Figure 12.6.

Figure 12.6 Displays data of another dataset in the form of a combo box

To create a static list, do not have to add a lookup field in advance, just create a permanent column object, set the buttonstyle property to CBSAUTO, click the omitial number button on the edge of the Picklist property in the object viewer to open a string list editor The device and then type some strings in turn.

To use a dialog to let the user select data, you must set the ButtonStyle property of the column object to CBSELLIPSIS. When the program runs, the column will appear a omitted button, click this omitted button to trigger the OneditButtonClick event, and you can open a dialog in the handle of this event to allow the user to select data. For image fields, an image can be displayed when the user clicks the ingredient button.

12.7.4 Setting the properties of the column object in the design period

The properties of the column object determine the display mode of the data in the grid. In the design period, you can set its following properties:

.Alignment is used to set the data alignment, the default value is the Alignment of Tfield. .ButtonStyle is set to CBSAUTO means plus a combo box. Set to CBSELLIPSIS means plus a omitted button, when the user clicks this button will trigger OneDitButtonClick. Set to CBSnone indicates that neither combo box does not have a omitted button.

.Color is used to set up background colors, the default value is the Color of TDBGRID.

.DropdownRows drop-down list, the default is 7.

.Expanded If the field corresponding to the column object is the ADT or array field, this property is used to control whether expand. l FieldName is used to specify a field and make it empty.

.Readonly is set to True means that the column is read-only and cannot be edited.

.Width is used to set the width of the column, and the default value is Tfield's DisplayWidth.

.Font is used to set the font of the characters in the column, the default value is the font of TDBGRID.

.Picklist is used to create a static list.

.Title This is a TColumnTitle object for setting the column headline.

Among them, TColumnTitle has the following properties:

.Alignment is used to set the title of the title, set to TalEftjustiFy indicate that the left alignment is set to Tacenter, set to TARightJustify indicate the right alignment.

.Caption is used to specify the text of the title, the default value is the DISPLAYLABEL of TField.

.Color is used to set the background color of the title, the default value is the FixedColor of TDBGRID.

.Font is used to set the font of the title, the default value is TitleFont of TDBGRID.

12.7.5 Display the value of the ADT and array fields

The TDBGRID component of Delphi 4 fully supports Oracle 8 object fields.

If the ObjectView property of the data set component is set to True, the object field can be expanded or folded. When the object field is expanded, the original column is divided into several columns, each column shows the value of a subfield, and the columns in each subfield have their own title bar, which can clearly see the included in the field. relationship. When the object field is folded, only one string is displayed, which lists the values ​​of each subfield, separated from each other.

If the ObjectView property of the data set component is set to False, each subfield is the same as the object field, each is alone.

Here is a few properties related to the object field. First, the tcolumn's Expandable property is set to TRUE, indicating that the column is expandable. The second is the TColumn's Expanded property that is used to test whether the column is currently expanded. The third is TDBGRID's maxTitlerows attribute, which is used to set up a few rows of titles in the grid. The fourth is the ObjectView of TDataSet, which has been introduced before this property. The fifth is TColumn's ParentColumn property that returns a column object that displays the parent field.

Figure 12.7 Demonstrate the case where the ObjectView property is set to FALSE:

Figure 12.7 What the ObjectView property is set to false

Figure 12.8 Demonstrate the ObjectView property set to TRUE but the object field is folded:

Figure 12.8 ObjectView property is set to TRUE but the object field is folded

Figure 12.9 Demonstrate the ObjectView property set to TRUE but the object field is deployed:

Figure 12.9 ObjectView attribute is set to true but the object field is expanded

12.7.6 Setting the raster option

You can set the raster option in the design period, which is to use the Options property of the TDBGRID component. The Options property is a collection that can contain the following elements: .dgeediting If you contain this element (below), allow the user to modify the data in the grid;

The .dgalwaysshoweditor grid is automatically edited, otherwise you need to press F2 to enter the editing state;

.dgtitles display the title of the column;

The leftmost end of the .dgindicator will display a 4 symbol;

The width of the .dgcolumnResize column can be reset;

The .dgcollines column is separated from the columns;

The .DGROWLINES line is separated from the lines;

.dgtabs can move input focus between columns and columns with TAB keys and SHIFT TAB keys;

.dgrowselect users can choose a single line, otherwise only one unit can be selected;

.dgalwaysshowselection even if the input focus is removed, the selected unit remains selected;

.dgconfirmdelete Press Ctrl Delete to delete a row, a confirmation box will make the user confirm;

.dgcancelonexit When the raster exits, any pending modification will be canceled.

.dgmultiselect users can select multi-line in the grid, which is equivalent to a list box for a check.

12.7.7 Action response user action in operation

The TDBGRID component has several events for responding to the user's actions. Since the grid is composed of rows and columns,, in the event handle, the first is to distinguish which column is currently in. These events are listed below:

.Oncellclick will trigger this event when the user clicks a unit.

.OnColenter will trigger this event when the input focus enters a column.

.Onnexit will trigger this event when the input focus leaves a column.

.Oncolumnmoved will trigger this event when the user moves a column to another.

This event will be triggered when the user doubles in the grid.

.OnDragdrop will trigger this event when the user releases the dragable object in the grid.

.OnDragover will trigger this event when the user dragged an object through the grid.

This event will be triggered when a unit needs to redraw when a unit needs to be redrawn.

This event will be triggered when the State property is set to CSDefault at the State property.

.OnEditButtonClick will trigger this event when the user clicks the ingredient button.

.OnEnddrag will trigger this event when the user releases the mouse.

This event will trigger this event when the grid gets the focus.

.OnExit will trigger this event when the grid loses the input focus.

.OnkeyDown will trigger this event when the user presses a button.

.Onkeypress will trigger this event when the user presses a visible character button.

.OnkeyUP will trigger this event when the user releases a key.

.Onstartdrag will trigger this event when the user starts to drag and drop.

.Ontitleclick Triggered this event when the user clicks on a list of a column.

12.7.8 A special database grid

The TDBGRID component is characterized by only one record, but can set the properties of each column. The TDBCTRLGRID component is characterized by each line (TPANEL object), you can put some controls on the pane. For example, a TDBEDit component can be placed on the pane, allowing it to display a value of a field, but also add a TLABEL component as a label next to the edit box. Another feature of TDBCTRLGRID is that there are multiple panes on a line, that is, a line can display multiple records, the height and width of the pane can be adjusted.

First, put a TDBCTRLGRID component on the form, set its DataSource property to specify a TDataSource component. At the beginning, the TDBCTRLGRID component has three rows and a column.

Next, put some data controls on the Design Unit of the TDBCTRID component, set the DataField property of these data controls to specify the fields to display. "Design Unit" is usually the one of the upper or leftmost units, and the difference from other units in appearance is that it has no oblique line shading.

Note: Only the data control is placed on the "Design Unit" of the TDBCTRID component. After the program is compiled, the data control on the Design Unit is automatically copied to other units, and each unit displays different records.

Figure 12.10 Demo the usage of the TDBCTRLGRID component:

Figure 12.10 Usage of TDBCTRLGRID components

The properties of the TDBCTRLGRID component are listed below:

.Allowdelete is set to True means that the record is allowed to delete.

. Allowed into TRUE to indicate that the record is allowed.

.COLCOUNT is used to set the number of panes in each row, and the default is 1.

.Orientation is set to GOVERTICAL to indicate that the record is displayed from top to bottom, set to gohorizontal to display the record from left to right.

.Panelheight is used to set the height of the pane, the default is 72.

.Panelwidth is used to set the width of the pane, the default is 200.

.Rowcount is used to set the number of rows, the default is 3.

.ShowFocus If set to True, display a dashed border on the currently recorded pane.

.SelectedColor is used to set the background color of the current recorded pane.

12.8 guidelines

The navigator is implemented with TDBNAVigator components that allow users to easily browse and manipulate records. The navigator can consist up to 10 buttons, including:

.First first record;

Previous record before .prior;

A record after .Next;

.Last last record;

.INSERT inserts a blank record;

.Delete deletes the current record;

.Edit allows editing of the current record;

.Post makes the unresolved editing operation;

.CANCEL cancels the unresser editing operation;

.Refresh uses the latest data refresh record in the data set.

You can selectively display some buttons on the navigator as needed, which is to use the VisibleButtons property.

At the design period, you can select the button to display on the navigator in the object viewer. At runtime, you can select the button to display on the navigator by programming. For example, suppose to use the same navigator to navigate to two data sets, one allows the user to edit data, and the other is not allowed to edit data. When switching between two data sets, the button on the navigator should be dynamically changed because INSERT, DELETE, EDIT, POST, CANCEL, and Refresh and other buttons. The following code hides these buttons:

Procedure TFORM1.CUSTOMERCOMPANYENTER (Sender: TOBJECT);

Begin

If sender = Customercompany Then

Begin

DBNAVIGATORALL.DataSource: = CustomerCompany.DataSource;

DBNAVIGATORALL.VisibleButtons: = [NBFirst, NBPrior, NBNext, NBLAST];

End

Else

Begin

DBNAVIGATORALL.DATASOURCE: = Ordernum.dataSource;

DBNAVIGATORALL.VisibleButtons: = DBNAVIGATORALL.VisibleButtons [Nbinsert, nbdelete, nbedit, nbpost, nbcancel, nbrefresh];

END;

END;

By the way, when the same navigator needs to navigate a few data sets, you must dynamically set the DataSource property. Suppose there are two TDataSource components on the form, called CustomersSource and ORDERSSOOSSOURCE, and their DataSet attributes specify a CustomersTable table and an ORDERSTABLE table, in addition, there are two TDBedit components on the form, called CustomersEDit and ORDERSEDIT, their DataSource properties, respectively, their DataSource properties Point CustomersSource and ORDERSSOOSSOURCE. The following program writes a common event handle for the two TDBEDit components to handle the OneNTER event. When the user edits on the CustomersEdit, the navigator will navigate to the CustomersTable, when the user edits on the ORDERSET, the navigation will be OrderStable. navigation. Procedure TFORM1. Customereditant (Sender: TOBJECT);

Begin

If sender = Customeredit Then

DBNAVIGATORL.DATASOURCE: = Customeredit.DataSource

Else

DBNAVIGATOR1.DATASOURCE: = Orderedit.DataSource;

END;

12.9 Durable Source

The TDataSource component is a non-visible component that acts as a bridge between data sets and data controls. Each data control must specify a data source (TDataSource component), accordingly, the Dataset property of the TDataSource component must specify a data set. The following is a brief introduction to the properties and events of the TDataSource component.

The DataSet property is used to specify a data set. At the design period, you can select a dataset for the DataSet property in the object viewer. At runtime, you can dynamically select the data set through the code. The program is now as follows:

WITH CUSTSOURCE DO

Begin

IF dataset = 'Customers' Then

DataSet: = 'Orders'

Else

DataSet: = 'Customers';

END;

You can also specify a data set member on another form, for example:

Procedure TFORM2. Formcreate (Sender: TOBJECT);

Begin

DataSource1.dataset: = form1.table1;

END;

In general, the name of the TDataSource component is irrelevant. However, the name of the TDataSource component should reflect the data set it is connected, for example, suppose the data set of the TDataSource component is called Customers, and the name of the TDataSource component is best called CustomersSource.

The enabled property is used to control whether the TDataSource component is connected to the data set, set to true to represent the connection, set to false to temporarily disconnect. If the enabled property is set to false, the data control connected to this data source will become blank.

If the AutoEdit property is set to True, when the user is typed in the data control, the data set automatically enters the DSEDIT state. If the AutoEdit property is set to false, the program must call the Edit function to enter the Dsedit status.

The OONDATACHANGE event will be triggered when the current record of the data set changes, which may be because the program calls next, previous, insert, etc.

The current recorded data will trigger onupdatedata events when it is updated, which may be due to calling POST. In handling the handle of this event, the data can be verified. The ONSTATECHANGE event will be triggered when the State property changes. For example, you can dynamically display the current state with a tag. The program is now as follows:

Proceduretform1.datasource1.StateChange (Sender: TOBJECT);

VAR

String;

Begin

Case CustTable.State of

DSINACTIVE: S: = 'inactive';

DSBROWSE: S: = 'browse';

Dsedit: s: = 'edit';

DSINSERT: S: = 'INSERT';

DSSetKey: s: = 'setKey';

END;

CustTableStateLabel.caption: = S;

END;

You can also dynamically allow or prohibit buttons and menu items using an onStateChange event.

Procedure form1.datasource1.stateChange (Sender: TOBJECT);

Begin

CustTableEditbtn.enabled: = (CustTable.State = DSBROWSE);

CustTableCancelbtn.enabled: = CustTable.State In [DSInsert, Dsedit, DSSetKey];

...

END;

转载请注明原文地址:https://www.9cbs.com/read-95933.html

New Post(0)