Perfect example of dynamic AdvStringGrid in oracle (AdvStringGrid usage tips/Cell

Source: Internet
Author: User
This method perfectly creates and controls dynamic TAdvStringGrid with constants and constant arrays.

This method perfectly creates and controls dynamic TAdvStringGrid with constants and constant arrays.

The principle is to declare constants, including the number of columns, number of rows, and attributes of each column, and then use these constants to control Cells in other procedures of the program. Very convenient for modification and transplantation!
The following is the overall code of the form, which is described in the middle. This Code not only provides a perfect example of dynamic AdvStringGrid, but also common forms processes, such as initialization and refresh processes.
This form can be run simply by preparing the following:
1. Add a TAdvStringGrid and name it strGrid1.
2. Set: TAdvStringGrid --> option --> goEditing = true.
TAdvStringGrid --> enableGraphics = true
3. Modify Form name to form1, or replace form1 in the following code with the name of the current Form.
4. overwrite the original code.
5. Associate the following process (you only need to double-click related items on the form and strGrid1 control properties-event page to complete the association .)
FormCreate
FormShow
StrGrid1CanEditCell
StrGrid1GetAlignment
StrGrid1GetCellColor
StrGrid1GetEditorType
The Code is as follows:
Unit ENMA0101;
Interface
Uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Grids, AdvGrid;
Const
CUnit_ID = 'enma0101 '; // declare a constant and save the unit name.
// Declare a constant to save the number of columns of Cells
CColQty1 = 8;
// Declare a constant array and save the attributes of Cells. The meanings of these attributes are as follows:
{0-Display
1-Edit
2-Mandatory
3-type
4-Alignment
5-color
6-Width
7-title
8-whether to read data from the database
9-data table name
10-field name
11-Field Length
12-read from text
13-text position}
CColProp1: array [0 .. cColQty1-1] of array [0 .. 13] of string = (// column attribute
// Display and edit the mandatory Type of alignment color width title
// 0 1 2 3 4 5 6 7 8 9 10 11
('Y', 'n', 'n', 'E', 'E', 'R', 'clbtnface', '25', 'No. ', 'n', '','', '0', 'n', '0'), // 0
('Y', 'n', 'n', 'E', 'E', 'l', 'clinfobk ', '123456', 'Part no', 'y ', 'pojbsub', 'Part _ no', '30', 'y', '2'), // 1
('Y', 'y', 'n', 'E', 'R', 'clwindow', '60', 'qty, 'y ', 'pojbsub', 'order _ QUANTITY ', '9', 'y', '3'), // 2
('Y', 'n', 'n', 'E', 'E', 'C', 'cleygreen', '40', 'U/m', 'y ', 'pojbsub', 'unit _ OF_MEASURE ', '2', 'y', '4'), // 3
('Y', 'y', 'n', 'C', 'C', 'clwindow', '60', 'dept', 'y ', 'pojbsub', 'delivery _ TO_DEPT ', '3', 'y', '5'), // 4
('Y', 'y', 'n', 'C', 'C', 'clwindow', '50', 'grp ', 'y ', 'pojbsub', 'Group _ a', '3', 'y', '7'), // 5
('Y', 'n', 'n', 'E' E ', 'l', 'clskyblue', '123', 'Part name', 'y ', 'pojbsub', 'Part _ NAME_C ', '70', 'y', '8'), // 6
('Y', 'y', 'n', 'M', 'l', 'clwindow', '50', 'df', 'y ', 'pojbsub', 'vendor _ no', '5', 'y', '6') // 7
);
// Declare constants and define column numbers for easy modification and reference
CC1NO = 0;
CC1PART_NO = 1;
CC1ORDER_QUANTITY = 2;
CC1UNIT_OF_MEASURE = 3;
CC1DELIVERY_TO_DEPT = 4;
CC1GROUP_A = 5;
CC1PART_NAME_C = 6;
CC1Data_Flag = 7;
Type
TForm1 = class (TForm)
StrGrid1: TAdvStringGrid;
Procedure FormCreate (Sender: TObject );
Procedure FormShow (Sender: TObject );
Procedure strGrid1CanEditCell (Sender: TObject; ARow,
ACol: Integer; var CanEdit: Boolean );
Procedure strGrid1GetAlignment (Sender: TObject; ARow,
ACol: Integer; var AAlignment: TAlignment );
Procedure strGrid1GetCellColor (Sender: TObject; ARow,
ACol: Integer; AState: TGridDrawState; ABrush: TBrush; AFont: TFont );
Procedure strGrid1GetEditorType (Sender: TObject; ACol,
ARow: Integer; var AEditor: TEditorType );
Private
{Private declarations}
Procedure prClear (pMode: byte );
Procedure prRefresh (pMode: byte );
Procedure prStgInitialize1;
Procedure prStg1Clear;
Public
{Public declarations}
End;
Var
Form1: TForm1;
Implementation
{$ R *. dfm}
// Execute the code when creating the form
Procedure TForm1.FormCreate (Sender: TObject );
Var
I, j: integer;
Begin
// Sets the maximum number of rows for CheckedBox
StrGrid1.RowCount: = 1000;
// Set cColProp1 [3, J] = 'C' to the CheckedBox format
For I: = 1 to strGrid1.rowcount-1 do begin
// The following attribute array settings fail. Only one column can be set for two columns of checkbox.
{For j: = 0 to a cColQty1-1 do begin
If cColProp1 [3, J] = 'C' then
StrGrid1.AddCheckBox (j, I, false, false );
End ;}
// Use the following method for direct definition.
StrGrid1.AddCheckBox (4, I, false, false );
StrGrid1.AddCheckBox (5, I, false, false );
End;
End;
// Execute code when the form is displayed
Procedure TForm1.FormShow (Sender: TObject );
Begin
Form1.caption: = cUnit_ID + ''+ form1.caption;
PrClear (0 );
End;
// Clear the form code
Procedure TForm1.prClear (pMode: byte );
Begin
Case pMode
0: begin
PrStgInitialize1;
End;
1: begin
PrStg1Clear;
End;
End;
// Other cleared content
End;
// Form refresh code
Procedure Tform1.prRefresh (pMode: byte );
Begin
// Refresh the content of the form
End;
// AdvStringGrid initialization process
Procedure TForm1.prStgInitialize1;
Var
I: Integer;
Begin
// Set the initial number of rows and columns in the part table
StrGrid1.RowCount: = 2;
StrGrid1.ColCount: = cColQty1;
StrGrid1.FixedRows: = 1;
StrGrid1.FixedCols: = 1;
// Set the column width and Column Title
For I: = 0 to a cColQty1-1 do begin
StrGrid1.Cells [I, 0]: = cColProp1 [I, 7]; // Title
If cColProp1 [I, 0] = 'y' then
StrGrid1.ColWidths [I]: = strToInt (cColProp1 [I, 6]) // column width
Else
StrGrid1.ColWidths [I]: = 0; // column width
End;
End;
// AdvStringGrid clearing process
Procedure TForm1.prStg1Clear;
Var
I: integer;
J: integer;
Begin
For I: = 1 to strGrid1.RowCount-1 do begin
For J: = 0 to cColQty1-1 do begin
StrGrid1.Cells [J, I]: = '';
StrGrid1.SetCheckBoxState (J, I, false );
End;
End;
StrGrid1.RowCount: = 2;
End;
// Set whether the columns in the unit table can be edited.
Procedure TForm1.strGrid1CanEditCell (Sender: TObject; ARow,
ACol: Integer; var CanEdit: Boolean );
Var
I: integer;
Begin
// Directly define
{If stgPList. Cells [cNCols1 [3]-2, ARow] = '1' then
CanEdit: = false
Else begin
If ACol = cNCols1 [2]-2 then
CanEdit: = true
Else
CanEdit: = false;
End ;}
{If aRow = 0 then
CanEdit: = false
Else if}
// Define with attribute array
For I: = 0 to cColQty1 do begin
If ACol = I then begin
If cColProp1 [I, 1] = 'y' then CanEdit: = true;
If cColProp1 [I, 1] = 'n' then CanEdit: = False;
End;
End;
// The following code first sets whether a row can be edited based on the value of the cC1Data_Flag column, and then sets it based on the attribute array.
{If (strGrid1.cells [cC1Data_Flag, ARow] = '') or (strGrid1.cells [cC1Data_Flag, ARow] = 'C') then begin
CanEdit: = false;
Exit;
End else begin
For I: = 0 to cColQty1 do begin
If ACol = I then begin
If strGrid1.cells [cC1Data_Flag, aRow] = 'C' then CanEdit: = false
Else begin
If cColProp1 [I, 1] = 'y' then CanEdit: = true;
If cColProp1 [I, 1] = 'n' then CanEdit: = False;
End;
End;
End;
End ;}
End;
// Set the column alignment of the Unit Table
Procedure TForm1.strGrid1GetAlignment (Sender: TObject; ARow, ACol: Integer; var AAlignment: TAlignment );
Var
I: integer;
Begin
// Directly define
{If ARow = 0 then AAlignment: = tacenter
Else begin
Case ACol
0: AAlignment: = taRightJustify;
1: AAlignment: = taCenter;
2: AAlignment: = taCenter;
3: AAlignment: = taRightJustify;
4: AAlignment: = taCenter;
6: AAlignment: = taCenter;
8: AAlignment: = taCenter;
9: AAlignment: = taCenter;
Else AAlignment: = taLeftJustify;
End;
End ;}
// Define with attribute array
If ARow = 0 then AAlignment: = taCenter
Else begin
For I: = 0 to a cColQty1-1 do begin
If ACol = I then begin
// Case strToInt (cColProp1 [I, 4])
If cColProp1 [I, 4] = 'C' then AAlignment: = taCenter;
If cColProp1 [I, 4] = 'l' then AAlignment: = taLeftJustify;
If cColProp1 [I, 4] = 'R' then AAlignment: = taRightJustify;
End;
End;
End;
End;
// Set the color of each column in the Unit Table
Procedure TForm1.strGrid1GetCellColor (Sender: TObject; ARow,
ACol: Integer; AState: TGridDrawState; ABrush: TBrush; AFont: TFont );
Var
I: integer;
Begin
// Directly define
{If ARow> 0 then begin
Case ACol
1: ABrush. Color: = RGB (227,249,248 );
2: ABrush. Color: = RGB (250,232,193 );
3: ABrush. Color: = RGB (227,249,248 );
4: ABrush. Color: = RGB (250,232,193 );
12: ABrush. Color: = RGB (227,249,248 );
14: ABrush. Color: = RGB (250,232,193 );
24: ABrush. Color: = RGB (227,249,248 );
48: ABrush. Color: = RGB (250,232,193 );
51: ABrush. Color: = RGB (227,249,248 );
End;
End ;}
// Define with attribute array
If ARow = 0 then
Abrush. Color: = clBtnFace // first behavior gray
Else begin
For I: = 0 to cColQty1 do begin // set the color of a non-First row by attribute array
If ACol = I then abrush. Color: = StringToColor (cColProp1 [I, 5]);
End;
End;
End;
// Set the control types of each column in the Unit Table
Procedure TForm1.strGrid1GetEditorType (Sender: TObject; ACol,
ARow: Integer; var AEditor: TEditorType );
Var
I: integer;
Begin
For I: = 0 to cColQty1 do begin
If ACol = I then begin
If cColProp1 [I, 3] = 'M' then begin
AEditor: = edComBoEdit;
StrGrid1.ClearComboString;
StrGrid1.AddComboString ('y: Agree ');
StrGrid1.AddComboString ('n': denied ');
End;
End;
End;
End;
End.

(The above process passes the test in delphi6 .)

In this way, if you modify the properties related to Cells, you only need to modify the values related to the array cColProp1, which is convenient.
You can modify cColProp1 directly in the code window above. However, if there are quite a few columns, it is quite troublesome to modify it, and it is easy to correct and inconvenient!
In this regard, I made an excel template: Dynamic Cells set tool table, such as attachment.

This template automatically generates static arrays cColProp1 and static column numbers, which are intuitive and convenient to modify.
After modification, copy the generated static array cColProp1 and static column number to the code.
Dynamic Cells setting tool table

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.