oracle Dynamic AdvStringGrid Perfect example of AdvStringGrid Use tips and Cells

  • 2020-06-23 02:09:28
  • OfStack

The idea is to declare constants, including the number of columns, the number of rows, and the properties of each column, and then use these constants for the rest of the program to control Cells. Very convenient, easy to modify and transplant!
The following is the overall code for the form, with instructions in the middle. This code not only has a perfect example of dynamic AdvStringGrid, but also a common procedure for a general form, comparing the form initialization and refresh procedures.
This form can be run by simply preparing the following:
1. Add 1 TAdvStringGrid and name it strGrid1.
2. Setting: TAdvStringGrid-- > option-- > goEditing=true
TAdvStringGrid-- > enableGraphics=true
3, change the Form name to form1, or replace form1 in the following code with the name of the current form.
4. Overwrite the original code with the following code.
5, associate the following procedure (just double-click the relevant item in the form and strGrid1 control propert-Event page to complete the association).
FormCreate
FormShow
strGrid1CanEditCell
strGrid1GetAlignment
strGrid1GetCellColor
strGrid1GetEditorType

unit ENMA0101; 
interface 
uses 
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, 
Dialogs, Grids, AdvGrid; 
const 
cUnit_ID='ENMA0101'; // Declare constants and save the cell name  
// Declare constants for saving Cells The number of columns  
cColQty1=8; 
// Declare an array of constants, save Cells The meanings of the attributes are as follows:  
{0- According to  
1- The editor  
2- Will lose  
3- type  
4- alignment  
5- color  
6- The width of the  
7- The title  
8- Whether to read from the database  
9- Table name  
10- The field name  
11- The length of the field  
12- Read in text  
13- Position in text  } 
cColProp1: array[0..cColQty1-1] of array[0..13] of string=( // List of attributes  
// Display type alignment required for editing   color   The width of the   The title  
// 0 1 2 3 4 5 6 7 8 9 10 11 
('Y','N','N','E','R','clBtnFace','25','NO.','N','','','0','N','0'), // 0 
('Y','N','N','E','L','clInfoBk','150','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','C','clMoneyGreen','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','L','clSkyBlue','160','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} 
// Code is executed when the form is created  
procedure TForm1.FormCreate(Sender: TObject); 
Var 
i,j:integer; 
begin 
// Sets the maximum number of rows for setting CheckedBox 
strGrid1.RowCount:=1000; 
// Set up the cColProp1[3,J]='C' Is the cell of CheckedBox format  
for i:=1 to strGrid1.rowcount-1 do begin 
// Set failed through the following array of properties when two columns checkbox , can only be set 1 The column.  
{for j:=0 to cColQty1-1 do begin 
if cColProp1[3,J]='C' then 
strGrid1.AddCheckBox(j,i,false,false); 
end;} 
// Change to the following way to directly define.  
strGrid1.AddCheckBox(4,i,false,false); 
strGrid1.AddCheckBox(5,i,false,false); 
end; 
end; 
// Executes code when the form is displayed  
procedure TForm1.FormShow(Sender: TObject); 
begin 
form1.caption:= cUnit_ID + ' ' + form1.caption; 
prClear(0); 
end; 
// Form clearing code  
procedure TForm1.prClear(pMode:byte); 
begin 
case pMode of 
0:begin 
prStgInitialize1; 
end; 
1:begin 
prStg1Clear; 
end; 
end; 
// Other empty content  
end; 
// Form refresh code  
procedure Tform1.prRefresh(pMode: byte); 
begin 
// Form refresh content  
end; 
//AdvStringGrid Initialization process  
procedure TForm1.prStgInitialize1; 
Var 
I:Integer; 
begin 
// Sets 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 headers  
for I:=0 to cColQty1-1 do begin 
strGrid1.Cells[I,0]:=cColProp1[I,7]; // The 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 Clear the 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; 
// Sets whether the cell table columns can be edited  
procedure TForm1.strGrid1CanEditCell(Sender: TObject; ARow, 
ACol: Integer; var CanEdit: Boolean); 
Var 
I:integer; 
begin 
// Direct definition  
{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} 
// Defined with an array of properties  
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 starts with a column cC1Data_Flag The value of the set 1 Whether the row can be edited, and then set according to the properties 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; 
// Sets the alignment of the columns of the cell table  
procedure TForm1.strGrid1GetAlignment(Sender: TObject; ARow, ACol: Integer; var AAlignment: TAlignment); 
Var 
I:integer; 
begin 
// Direct definition  
{if ARow=0 then AAlignment:=tacenter 
else begin 
case ACol of 
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; } 
// Defined with an array of properties  
if ARow=0 then AAlignment:=taCenter 
else begin 
for I:=0 to 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; 
// Sets the color of each column in the cell table  
procedure TForm1.strGrid1GetCellColor(Sender: TObject; ARow, 
ACol: Integer; AState: TGridDrawState; ABrush: TBrush; AFont: TFont); 
Var 
I:integer; 
begin 
// Direct definition  
{if ARow>0 then begin 
Case ACol of 
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;} 
// Defined with an array of properties  
if ARow=0 then 
abrush.Color:=clBtnFace //  Head gray  
else begin 
for I:=0 to cColQty1 do begin //  The non-first line sets the color by an array of properties  
if ACol=I then abrush.Color:=StringToColor(cColProp1[I,5]); 
end; 
end; 
end; 
// Sets the control type for each column of the cell 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 :  Refused to '); 
end; 
end; 
end; 
end; 
end. 

(The above procedure was tested in Delphi6.)

In this way, if you change the Cells-related properties, you only need to change the values associated with the array cColProp1, which is convenient.
Regarding modifying cColProp1, you can modify it directly in the code window above, but if there are a lot of columns, it is quite troublesome, easy to correct and inconvenient!
For this, I made an excel template: dynamic Cells configuration tool table, as attached.

With this template, the static array cColProp1 and the static column number can be automatically generated, which is also intuitive and convenient to modify.
After modification, the generated static array cColProp1 and static column number can be copied into the code.
Dynamic Cells setting tool table http: / / xiazai ofstack. com / 200906 / other DynamicCells_Setting xls

Related articles: