The principle is to declare constants, including the number of columns, the number of rows, the properties of each column, and then use these constants to control cells in other procedures of the program. Very convenient, easy to modify and transplant!
The following is the overall code for the form, with instructions in the middle. This code is not only a perfect example of dynamic Advstringgrid, but also a common procedure for general forms, comparing form initialization and refresh process.
This form can be run by simply preparing the following:
1, add a tadvstringgrid and name it strGrid1.
2, set: Tadvstringgrid-->option-->goediting=true
Tadvstringgrid-->enablegraphics=true
3, modify the form name called Form1, or replace the Form1 in the following code with the name of the current form.
4, overwrite the original code with the following code.
5, the following procedure is associated (you only need to double-click the related item in the form and StrGrid1 control Properties-Event page to complete the association.) )
Formcreate
Formshow
Strgrid1caneditcell
Strgrid1getalignment
Strgrid1getcellcolor
Strgrid1geteditortype
Copy Code code as follows:
Unit ENMA0101;
Interface
Uses
Windows, Messages, sysutils, variants, Classes, Graphics, Controls, Forms,
Dialogs, Grids, Advgrid;
Const
Cunit_id= ' ENMA0101 '; declaring constants, saving cell names
Declaring constants for saving the number of cells columns
ccolqty1=8;
Declares a constant array that holds various attributes of cells, with various attribute meanings as follows:
{0-Display
1-Edit
2-Must lose
3-type
4-Align
5-Color
6-width
7-Title
8-read from the database
9-Data table name
10-Field Name
11-Field length
12-Read in text
13-Position in text}
Ccolprop1:array[0..ccolqty1-1] of array[0..13] of string= (//Column Properties
Show edit required type align color width title
0 1 2 3 4 5 6 7 8 9 10-11
(' Y ', ' n ', ' n ', ' E ', ' R ', ' clbtnface ', ' + ', ' NO ', ' n ', ', ', ', ' 0 ', ' n ', ' 0 '),//0
(' Y ', ' n ', ' n ', ' E ', ' L ', ' clinfobk ', ' I ', ' part No ', ' Y ', ' pojbsub ', ' part_no ', ', ' Y ', ' 2 ',//1
(' Y ', ' y ', ' N ', ' E ', ' R ', ' Clwindow ', ', ', ' Qty ', ' Y ', ' pojbsub ', ' order_quantity ', ' 9 ', ' Y ', ' 3 ',//2
(' Y ', ' n ', ' n ', ' E ', ' C ', ' clmoneygreen ', ' ', ' u/m ', ' Y ', ' pojbsub ', ' unit_of_measure ', ' 2 ', ' Y ', ' 4 '),//3
(' Y ', ' y ', ' N ', ' C ', ' C ', ' Clwindow ', ', ', ' Dept ', ' Y ', ' pojbsub ', ' delivery_to_dept ', ' 3 ', ' Y ', ' 5 ',//4
(' Y ', ' y ', ' N ', ' C ', ' C ', ' Clwindow ', ' m ', ' Grp ', ' Y ', ' pojbsub ', ' group_a ', ' 3 ', ' Y ', ' 7 '),//5
(' Y ', ' n ', ' n ', ' E ', ' L ', ' clskyblue ', ' 160 ', ' part Name ', ' Y ', ' pojbsub ', ' part_name_c ', ', ', ' y ', ' 8 ',//6
(' Y ', ' y ', ' N ', ' M ', ' L ', ' Clwindow ', ' m ', ' DF ', ' y ', ' pojbsub ', ' vendor_no ', ' 5 ', ' Y ', ' 6 ')//7
);
Declare constants, 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 code when form is created
Procedure Tform1.formcreate (Sender:tobject);
Var
I,j:integer;
Begin
Sets the maximum number of rows to set the Checkedbox
strgrid1.rowcount:=1000;
Set ccolprop1[3,j]= ' C ' cell to Checkedbox format
For i:=1 to Strgrid1.rowcount-1 do begin
The following property array settings do not pass, and only one column can be set when two columns of a checkbox.
{for j:=0 to Ccolqty1-1 do begin
If ccolprop1[3,j]= ' C ' then
Strgrid1.addcheckbox (J,i,false,false);
End;}
be defined directly in the following manner.
Strgrid1.addcheckbox (4,i,false,false);
Strgrid1.addcheckbox (5,i,false,false);
End
End
Execute code when form is displayed
Procedure Tform1.formshow (Sender:tobject);
Begin
form1.caption:= cunit_id + ' + form1.caption;
Prclear (0);
End
Form emptying 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
Set the initial number of rows and columns for the part table
strgrid1.rowcount:=2;
Strgrid1.colcount:=ccolqty1;
Strgrid1.fixedrows:=1;
Strgrid1.fixedcols:=1;
Set column width and column headings
For i:=0 to 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; Width
End
End
Advstringgrid vacuuming 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 of a cell table 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}
Define with a property 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 column Cc1data_flag and then set according to the property 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 for a 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 }
Define with a property array
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
Set the color of each column in a 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;}
Define with a property array
If Arow=0 Then
Abrush. Color:=clbtnface//First behavior Gray
ELSE begin
For i:=0 to CColQty1 do begin//not first row set color by property array
If Acol=i then Abrush. Color:=stringtocolor (ccolprop1[i,5]);
End
End
End
To set the column control types for a 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: Refusal ');
End
End
End
End
End.
(The above process is tested through DELPHI6.) )
In this way, if you modify cells related properties, you only need to modify the array CColProp1 related values can be achieved, very convenient.
About modifying CColProp1, you can directly in the above code window to modify, but if the column is quite a lot of words, this change is also quite troublesome, and easy to change the wrong, is not convenient!
For this, I made an Excel template: Dynamic cells settings tool sheet, such as attachment.
Through this template, you can automatically generate static array cColProp1 and static column number, modified is also very intuitive, very convenient.
Once modified, the generated static array cColProp1 and static column numbers are copied into the code.
Dynamic Cells setting Tool table Http://xiazai.jb51.net/200906/other/DynamicCells_Setting.xls