Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 111 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
111
Dung lượng
675,71 KB
Nội dung
20-18 Developer’ s Guide Viewing and editing data with TDBGrid cells. For example, you can use a blank column to display aggregated values on the last record of a group of records that the aggregate summarizes. Another possibility is to display a bitmap or bar chart that graphically depicts some aspect of the record’s data. Two or more persistent columns can be associated with the same field in a dataset. For example, you might display a part number field at the left and right extremes of a wide grid to make it easier to find the part number without having to scroll the grid. Note Because persistent columns do not have to be associated with a field in a dataset, and because multiple columns can reference the same field, a customized grid’s FieldCount property can be less than or equal to the grid’s column count. Also note that if the currently selected column in a customized grid is not associated with a field, the grid’s SelectedField property is NULL and the SelectedIndex property is –1. Persistent columns can be configured to display grid cells as a combo box drop-down list of lookup values from another dataset or from a static pick list, or as an ellipsis button (…) in a cell that can be clicked upon to launch special data viewers or dialogs related to the current cell. Creating persistent columns To customize the appearance of grid at design time, you invoke the Columns editor to create a set of persistent column objects for the grid. At runtime, the State property for a grid with persistent column objects is automatically set to csCustomized. To create persistent columns for a grid control, 1 Select the grid component in the form. 2 Invoke the Columns editor by double clicking on the grid’s Columns property in the Object Inspector. The Columns list box displays the persistent columns that have been defined for the selected grid. When you first bring up the Columns editor, this list is empty because the grid is in its default state, containing only dynamic columns. You can create persistent columns for all fields in a dataset at once, or you can create persistent columns on an individual basis. To create persistent columns for all fields: 1 Right-click the grid to invoke the context menu and choose Add All Fields. Note that if the grid is not already associated with a data source, Add All Fields is disabled. Associate the grid with a data source that has an active dataset before choosing Add All Fields. 2 If the grid already contains persistent columns, a dialog box asks if you want to delete the existing columns, or append to the column set. If you choose Yes, any existing persistent column information is removed, and all fields in the current dataset are inserted by field name according to their order in the dataset. If you choose No, any existing persistent column information is retained, and new column information, based on any additional fields in the dataset, are appended to the dataset. 3 Click Close to apply the persistent columns to the grid and close the dialog box. Using data controls 20-19 Viewing and editing data with TDBGrid To create persistent columns individually: 1 Choose the Add button in the Columns editor. The new column will be selected in the list box. The new column is given a sequential number and default name (for example, 0 - TColumn). 2 To associate a field with this new column, set the FieldName property in the Object Inspector. 3 To set the title for the new column, expand the Title property in the Object Inspector and set its Caption property. 4 Close the Columns editor to apply the persistent columns to the grid and close the dialog box. At runtime, you can switch to persistent columns by assigning csCustomized to the Columns.State property. Any existing columns in the grid are destroyed and new persistent columns are built for each field in the grid’s dataset. You can then add a persistent column at runtime by calling the Add method for the column list: DBGrid1.Columns.Add; Deleting persistent columns Deleting a persistent column from a grid is useful for eliminating fields that you do not want to display. To remove a persistent column from a grid, 1 Double-click the grid to display the Columns editor. 2 Select the field to remove in the Columns list box. 3 Click Delete (you can also use the context menu or Del key, to remove a column). Note If you delete all the columns from a grid, the Columns.State property reverts to its csDefault state and automatically build dynamic columns for each field in the dataset. You can delete a persistent column at runtime by simply freeing the column object: DBGrid1.Columns[5].Free; Arranging the order of persistent columns The order in which columns appear in the Columns editor is the same as the order the columns appear in the grid. You can change the column order by dragging and dropping columns within the Columns list box. To change the order of a column, 1 Select the column in the Columns list box. 2 Drag it to a new location in the list box. 20-20 Developer’ s Guide Viewing and editing data with TDBGrid You can also change the column order at runtime by clicking on the column title and dragging the column to a new position. Note Reordering persistent fields in the Fields editor also reorders columns in a default grid, but not a custom grid. Important You cannot reorder columns in grids containing both dynamic columns and dynamic fields at design time, since there is nothing persistent to record the altered field or column order. At runtime, a user can use the mouse to drag a column to a new location in the grid if its DragMode property is set to dmManual. Reordering the columns of a grid with a State property of csDefault state also reorders field components in the dataset underlying the grid. The order of fields in the physical table is not affected. To prevent a user from rearranging columns at runtime, set the grid’s DragMode property to dmAutomatic. At runtime, the grid’s OnColumnMoved event fires after a column has been moved. Setting column properties at design time Column properties determine how data is displayed in the cells of that column. Most column properties obtain their default values from properties associated with another component (called the default source) such as a grid or an associated field component. To set a column’s properties, select the column in The Columns editor and set its properties in the Object Inspector. The following table summarizes key column properties you can set. Table 20.2 Column properties Property Purpose Alignment Left justifies, right justifies, or centers the field data in the column. Default source: TField.Alignment. ButtonStyle cbsAuto: (default) Displays a drop-down list if the associated field is a lookup field, or if the column’s PickList property contains data. cbsEllipsis: Displays an ellipsis ( ) button to the right of the cell. Clicking on the button fires the grid’s OnEditButtonClick event. cbsNone: The column uses only the normal edit control to edit data in the column. Color Specifies the background color of the cells of the column. Default source: TDBGrid.Color. (For text foreground color, see the Font property.) DropDownRows The number of lines of text displayed by the drop-down list. Default: 7. Expanded Specifies whether the column is expanded. Only applies to columns representing ADT or array fields. FieldName Specifies the field name associated with this column. This can be blank. ReadOnly True: The data in the column cannot be edited by the user. False: (default) The data in the column can be edited. Using data controls 20-21 Viewing and editing data with TDBGrid The following table summarizes the options you can specify for the Title property. Defining a lookup list column You can create a column that displays a drop-down list of values, similar to a lookup combo box control. To specify that the column acts like a combo box, set the column’s ButtonStyle property to cbsAuto. Once you populate the list with values, the grid automatically displays a combo box-like drop-down button when a cell of that column is in edit mode. There are two ways to populate that list with the values for users to select: • You can fetch the values from a lookup table. To make a column display a drop- down list of values drawn from a separate lookup table, you must define a lookup field in the dataset. For information about creating lookup fields, see “Defining a lookup field” on page 25-9. Once the lookup field is defined, set the column’s FieldName to the lookup field name. The drop-down list is automatically populated with lookup values defined by the lookup field. • You can specify a list of values explicitly at design time. To enter the list values at design time, double-click the PickList property for the column in the Object Inspector. This brings up the String List editor, where you can enter the values that populate the pick list for the column. By default, the drop-down list displays 7 values. You can change the length of this list by setting the DropDownRows property. Note To restore a column with an explicit pick list to its normal behavior, delete all the text from the pick list using the String List editor. Width Specifies the width of the column in screen pixels. Default source: TField.DisplayWidth. Font Specifies the font type, size, and color used to draw text in the column. Default source: TDBGrid.Font. PickList Contains a list of values to display in a drop-down list in the column. Title Sets properties for the title of the selected column. Table 20.3 Expanded TColumn Title properties Property Purpose Alignment Left justifies (default), right justifies, or centers the caption text in the column title. Caption Specifies the text to display in the column title. Default source: TField.DisplayLabel. Color Specifies the background color used to draw the column title cell. Default source: TDBGrid.FixedColor. Font Specifies the font type, size, and color used to draw text in the column title. Default source: TDBGrid.TitleFont. Table 20.2 Column properties (continued) Property Purpose 20-22 Developer’ s Guide Viewing and editing data with TDBGrid Putting a button in a column A column can display an ellipsis button (…) to the right of the normal cell editor. Ctrl+Enter or a mouse click fires the grid’s OnEditButtonClick event. You can use the ellipsis button to bring up forms containing more detailed views of the data in the column. For example, in a table that displays summaries of invoices, you could set up an ellipsis button in the invoice total column to bring up a form that displays the items in that invoice, or the tax calculation method, and so on. For graphic fields, you could use the ellipsis button to bring up a form that displays an image. To create an ellipsis button in a column: 1 Select the column in the Columns list box. 2 Set ButtonStyle to cbsEllipsis. 3 Write an OnEditButtonClick event handler. Restoring default values to a column At runtime you can test a column’s AssignedValues property to determine whether a column property has been explicitly assigned. Values that are not explicitly defined are dynamically based on the associated field or the grid’s defaults. You can undo property changes made to one or more columns. In the Columns editor, select the column or columns to restore, and then select Restore Defaults from the context menu. Restore defaults discards assigned property settings and restores a column’s properties to those derived from its underlying field component At runtime, you can reset all default properties for a single column by calling the column’s RestoreDefaults method. You can also reset default properties for all columns in a grid by calling the column list’s RestoreDefaults method: DBGrid1.Columns.RestoreDefaults; Displaying ADT and array fields Sometimes the fields of the grid’s dataset do not represent simple values such as text, graphics, numerical values, and so on. Some database servers allow fields that are a composite of simpler data types, such as ADT fields or array fields. There are two ways a grid can display composite fields: • It can “flatten out” the field so that each of the simpler types that make up the field appears as a separate field in the dataset. When a composite field is flattened out, its constituents appear as separate fields that reflect their common source only in that each field name is preceded by the name of the common parent field in the underlying database table. To display composite fields as if they were flattened out, set the dataset’s ObjectView property to False. The dataset stores composite fields as a set of separate fields, and the grid reflects this by assigning each constituent part a separate column. Using data controls 20-23 Viewing and editing data with TDBGrid • It can display composite fields in a single column, reflecting the fact that they are a single field. When displaying composite fields in a single column, the column can be expanded and collapsed by clicking on the arrow in the title bar of the field, or by setting the Expanded property of the column: • When a column is expanded, each child field appears in its own sub-column with a title bar that appears below the title bar of the parent field. That is, the title bar for the grid increases in height, with the first row giving the name of the composite field, and the second row subdividing that for the individual parts. Fields that are not composites appear with title bars that are extra high. This expansion continues for constituents that are in turn composite fields (for example, a detail table nested in a detail table), with the title bar growing in height accordingly. • When the field is collapsed, only one column appears with an uneditable comma delimited string containing the child fields. To display a composite field in an expanding and collapsing column, set the dataset’s ObjectView property to True. The dataset stores the composite field as a single field component that contains a set of nested sub-fields. The grid reflects this in a column that can expand or collapse Figure 20.2 shows a grid with an ADT field and an array field. The dataset’s ObjectView property is set to False so that each child field has a column. Figure 20.2 TDBGrid control with ObjectView set to False Figure 20.3 and 20.4 show the grid with an ADT field and an array field. Figure 20.3 shows the fields collapsed. In this state they cannot be edited. Figure 20.4 shows the fields expanded. The fields are expanded and collapsed by clicking on the arrow in the fields title bar. Figure 20.3 TDBGrid control with Expanded set to False ADT child fields Array child fields 20-24 Developer’ s Guide Viewing and editing data with TDBGrid Figure 20.4 TDBGrid control with Expanded set to True The following table lists the properties that affect the way ADT and array fields appear in a TDBGrid: Note In addition to ADT and array fields, some datasets include fields that refer to another dataset (dataset fields) or a record in another dataset (reference) fields. Data-aware grids display such fields as “(DataSet)” or “(Reference)”, respectively. At runtime an ellipsis button appears to the right. Clicking on the ellipsis brings up a new form with a grid displaying the contents of the field. For dataset fields, this grid displays the dataset that is the field’s value. For reference fields, this grid contains a single row that displays the record from another dataset. Setting grid options You can use the grid Options property at design time to control basic grid behavior and appearance at runtime. When a grid component is first placed on a form at design time, the Options property in the Object Inspector is displayed with a + (plus) sign to indicate that the Options property can be expanded to display a series of Boolean properties that you can set individually. To view and set these properties, click on the + sign. The list of options in the Object Inspector below the Options property. The + sign changes to a – (minus) sign, that collapses the list back when you click it. Table 20.4 Properties that affect the way composite fields appear Property Object Purpose Expandable TColumn Indicates whether the column can be expanded to show child fields in separate, editable columns. (read-only) Expanded TColumn Specifies whether the column is expanded. MaxTitleRows TDBGrid Specifies the maximum number of title rows that can appear in the grid ObjectView TDataSet Specifies whether fields are displayed flattened out, or in object mode, where each object field can be expanded and collapsed. ParentColumn TColumn Refers to the TColumn object that owns the child field’s column. ADT child field columns Array child field columns Using data controls 20-25 Viewing and editing data with TDBGrid The following table lists the Options properties that can be set, and describes how they affect the grid at runtime. Table 20.5 Expanded TDBGrid Options properties Option Purpose dgEditing True: (Default). Enables editing, inserting, and deleting records in the grid. False: Disables editing, inserting, and deleting records in the grid. dgAlwaysShowEditor True: When a field is selected, it is in Edit state. False: (Default). A field is not automatically in Edit state when selected. dgTitles True: (Default). Displays field names across the top of the grid. False: Field name display is turned off. dgIndicator True: (Default). The indicator column is displayed at the left of the grid, and the current record indicator (an arrow at the left of the grid) is activated to show the current record. On insert, the arrow becomes an asterisk. On edit, the arrow becomes an I-beam. False: The indicator column is turned off. dgColumnResize True: (Default). Columns can be resized by dragging the column rulers in the title area. Resizing changes the corresponding width of the underlying TField component. False: Columns cannot be resized in the grid. dgColLines True: (Default). Displays vertical dividing lines between columns. False: Does not display dividing lines between columns. dgRowLines True: (Default). Displays horizontal dividing lines between records. False: Does not display dividing lines between records. dgTabs True: (Default). Enables tabbing between fields in records. False: Tabbing exits the grid control. dgRowSelect True: The selection bar spans the entire width of the grid. False: (Default). Selecting a field in a record selects only that field. dgAlwaysShowSelection True: (Default). The selection bar in the grid is always visible, even if another control has focus. False: The selection bar in the grid is only visible when the grid has focus. dgConfirmDelete True: (Default). Prompt for confirmation to delete records ( Ctrl+Del ). False: Delete records without confirmation. dgCancelOnExit True: (Default). Cancels a pending insert when focus leaves the grid. This option prevents inadvertent posting of partial or blank records. False: Permits pending inserts. dgMultiSelect True: Allows user to select noncontiguous rows in the grid using Ctrl+Shift or Shift+ arrow keys. False: (Default). Does not allow user to multi-select rows. 20-26 Developer’ s Guide Viewing and editing data with TDBGrid Editing in the grid At runtime, you can use a grid to modify existing data and enter new records, if the following default conditions are met: •The CanModify property of the Dataset is True. •The ReadOnly property of grid is False. When a user edits a record in the grid, changes to each field are posted to an internal record buffer, but are not posted until the user moves to a different record in the grid. Even if focus is changed to another control on a form, the grid does not post changes until another the cursor for the dataset is moved to another record. When a record is posted, the dataset checks all associated data-aware components for a change in status. If there is a problem updating any fields that contain modified data, the grid raises an exception, and does not modify the record. Note If your application caches updates, posting record changes only adds them to an internal cache. They are not posted back to the underlying database table until your application applies the updates. You can cancel all edits for a record by pressing Esc in any field before moving to another record. Controlling grid drawing Your first level of control over how a grid control draws itself is setting column properties. The grid automatically uses the font, color, and alignment properties of a column to draw the cells of that column. The text of data fields is drawn using the DisplayFormat or EditFormat properties of the field component associated with the column. You can augment the default grid display logic with code in a grid’s OnDrawColumnCell event. If the grid’s DefaultDrawing property is True, all the normal drawing is performed before your OnDrawColumnCell event handler is called. Your code can then draw on top of the default display. This is primarily useful when you have defined a blank persistent column and want to draw special graphics in that column’s cells. If you want to replace the drawing logic of the grid entirely, set DefaultDrawing to False and place your drawing code in the grid’s OnDrawColumnCell event. If you want to replace the drawing logic only in certain columns or for certain field data types, you can call the DefaultDrawColumnCell inside your OnDrawColumnCell event handler to have the grid use its normal drawing code for selected columns. This reduces the amount of work you have to do if you only want to change the way Boolean field types are drawn, for example. Using data controls 20-27 Viewing and editing data with TDBGrid Responding to user actions at runtime You can modify grid behavior by writing event handlers to respond to specific actions within the grid at runtime. Because a grid typically displays many fields and records at once, you may have very specific needs to respond to changes to individual columns. For example, you might want to activate and deactivate a button elsewhere on the form every time a user enters and exits a specific column. The following table lists the grid events available in the Object Inspector. There are many uses for these events. For example, you might write a handler for the OnDblClick event that pops up a list from which a user can choose a value to enter in a column. Such a handler would use the SelectedField property to determine to current row and column. Table 20.6 Grid control events Event Purpose OnCellClick Occurs when a user clicks on a cell in the grid. OnColEnter Occurs when a user moves into a column on the grid. OnColExit Occurs when a user leaves a column on the grid. OnColumnMoved Occurs when the user moves a column to a new location. OnDblClick Occurs when a user double clicks in the grid. OnDragDrop Occurs when a user drags and drops in the grid. OnDragOver Occurs when a user drags over the grid. OnDrawColumnCell Occurs when application needs to draw individual cells. OnDrawDataCell (obsolete) Occurs when application needs to draw individual cells if State is csDefault. OnEditButtonClick Occurs when the user clicks on an ellipsis button in a column. OnEndDrag Occurs when a user stops dragging on the grid. OnEnter Occurs when the grid gets focus. OnExit Occurs when the grid loses focus. OnKeyDown Occurs when a user presses any key or key combination on the keyboard when in the grid. OnKeyPress Occurs when a user presses a single alphanumeric key on the keyboard when in the grid. OnKeyUp Occurs when a user releases a key when in the grid. OnStartDrag Occurs when a user starts dragging on the grid. OnTitleClick Occurs when a user clicks the title for a column. [...]... graphic form Each decision graph shows the value of a single summary, such as Sum, Count, or Avg, charted for one or more dimensions For more information on crosstabs, see page 22-3 For illustrations of decision graphs at design time, see Figure 22.1 on page 22-2 and Figure 22.4 on page 22- 15 Creating decision graphs To create a form with one or more decision graphs, 1 Follow steps 1–3 listed under “Guidelines... listed under “Guidelines for using decision support components” on page 22-4 2 Add one or more decision grid components (TDecisionGrid) and bind them to the decision source, TDecisionSource, with the Object Inspector by setting their DecisionSource property to the appropriate decision source component 3 Continue with steps 5 7 listed under “Guidelines for using decision support components.” For a description... environment These books are distributed as PDF files on the Delphi Companion Tools CD Most of the information in the PDF files is also available in the online Help To display online Help for a Rave Reports component on a form, select the component and press F1 To display online Help for the Rave Visual Designer, use the Help menu 21-6 Developer’s Guide Chapter 22 Using decision support components Chapter22... has many values For more information, see “Decision support components and memory control” on page 22-20 • To determine the starting value for ranges, or the drill-down value for a “Set” dimension, first choose the appropriate Grouping value in the Grouping dropdown, and then enter the starting range value or permanent drill-down value in the Initial Value drop-down list 22-8 Developer’s Guide Using decision... components are: TRavePostNetBarCode TRaveI2of5Bar Code TRaveCode39BarCode TRaveCode128BarCode TRaveUPCBarCode TRaveEANBarCode Getting more information Delphi includes the following Nevrona Designs documentation for Rave Reports Table 21.1 Rave Reports documentation Title Description Rave Visual Designer Manual for Reference and Learning Provides detailed information about using the Rave Visual Designer... any standard format for that data type, and Subtotals 22-12 Developer’s Guide Creating and using decision graphs indicates whether to display subtotals for that dimension With summary fields, these same properties are used to changed the appearance of the data that appears in the summary area of the grid When you’re through setting dimension properties, either click a component in the form or choose... which your Delphi application is located and save the Rave project file as MyRave.rav k Minimize the Rave Visual Designer window and return to Delphi 5 From the Rave page of the Component palette, add the Rave project component, TRvProject, to the form 6 In the Object Inspector, set the ProjectFile property to the report project file (MyRave.rav) that you created in step j 21-2 Developer’s Guide The... display the report For a more information on using the Rave Visual Designer, use the Help menu or see the Rave Reports documentation listed in “Getting more information” on page 21-6 The Rave Visual Designer To launch the Rave Visual Designer, do one of the following: • Choose Tools|Rave Designer • Double-click a TRvProject component on a form • Right-click a TRvProject component on a form, and choose... listed in “Getting more information” on page 21-6 Creating reports with Rave Reports 21-3 Component overview Component overview This section provides an overview of the Rave Reports components For detailed component information, see the documentation listed in “Getting more information” on page 21-6 VCL/CLX components The VCL/CLX components are non-visual components that you add to a form in your VCL or... from left to right PanelHeight Sets the height for an individual panel Default = 72 PanelWidth Sets the width for an individual panel Default = 200 RowCount Sets the number of panels to display Default = 3 ShowFocus True (default): Displays a focus rectangle around the current record’s panel at runtime False: Does not display a focus rectangle For more information about database control grid properties . not want to use on a form. For example, when working with a unidirectional dataset, only the First, Next, and Refresh buttons are meaningful. On a form that is intended for browsing rather. to generate reports within a Delphi application. Additional documentation for Rave Reports is included in the Delphi directory, as described in “Getting more information” on page 21-6. Note: Rave. on a form, and choose Rave Visual Designer. For a detailed information on using the Rave Visual Designer, use the Help menu or see the Rave Reports documentation listed in “Getting more information”