動態AdvStringGrid完美樣本 (AdvStringGrid提示/Cells)

來源:互聯網
上載者:User

此表單,只需要簡單準備如下,即可運行:
    1,添加一個TAdvStringGrid,並命名為strGrid1。
    2,設定:TAdvStringGrid-->option-->goEditing=true
             TAdvStringGrid-->enableGraphics=true
    3,修改Form名稱為form1,或替換以下代碼中的form1為當前表單的名字。
    4,將以下代碼覆蓋原來的代碼。
    5,關聯以下過程(只需要在表單和strGrid1控制項屬性-事件頁中雙擊相關項目即可完成關聯。)
        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';   //聲明常量,儲存單元名稱

    //聲明常量,用於儲存Cells的列數
    cColQty1=8; 

    //聲明常量數組,儲存Cells的各種屬性,各種屬性含義如下:
    {0-顯示
    1-編輯
    2-必輸
    3-類型
    4-對齊
    5-顏色
    6-寬度
    7-標題
    8-是否從資料庫中讀取
    9-資料表名稱
    10-欄位名
    11-欄位長度
    12-文本中讀取
    13-文本中位置    }
  cColProp1: array[0..cColQty1-1] of array[0..13] of string=(   //列屬性
   //顯示編輯必輸類型對齊 顏色     寬度    標題
    // 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
);
    //聲明常量,定義列號,便於修改與引用
    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}

//表單建立時執行代碼
procedure TForm1.FormCreate(Sender: TObject);
Var
  i,j:integer;
begin
    //設定行數最大值,用於設定CheckedBox
    strGrid1.RowCount:=1000;

    //設定cColProp1[3,J]='C'的儲存格為CheckedBox格式
    for i:=1 to strGrid1.rowcount-1 do begin

        //通過以下屬性數組設定未通過,當兩列checkbox時,只能設定一列。
        {for j:=0 to cColQty1-1 do begin
            if cColProp1[3,J]='C' then
                strGrid1.AddCheckBox(j,i,false,false);
        end;}
        //改為以下方式直接定義。
        strGrid1.AddCheckBox(4,i,false,false);
        strGrid1.AddCheckBox(5,i,false,false);
    end;
end;

//表單顯示時執行代碼
procedure TForm1.FormShow(Sender: TObject);
begin
    form1.caption:= cUnit_ID + ' ' + form1.caption;
    prClear(0);
end;

//表單清空代碼
procedure TForm1.prClear(pMode:byte);
begin
    case pMode of
    0:begin
        prStgInitialize1;
    end;
    1:begin
        prStg1Clear;
    end;
    end;

    //其它清空內容

end;

//表單重新整理代碼
procedure Tform1.prRefresh(pMode: byte);
begin
    //表單重新整理內容

end;

//AdvStringGrid初始化過程
procedure TForm1.prStgInitialize1;
Var
  I:Integer;
begin

    //設定零件表初始行數和列數
    strGrid1.RowCount:=2;
    strGrid1.ColCount:=cColQty1;
    strGrid1.FixedRows:=1;
    strGrid1.FixedCols:=1;

    //設定列寬度和欄位標題
    for I:=0 to cColQty1-1 do begin
        strGrid1.Cells[I,0]:=cColProp1[I,7];                 //標題
        if cColProp1[I,0]='Y' then
            strGrid1.ColWidths[I]:=strToInt(cColProp1[I,6])  //列寬
        else
            strGrid1.ColWidths[I]:=0;                        //列寬
    end;

end;

//AdvStringGrid清空過程
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;

//設定單元表各列是否可以編輯
procedure TForm1.strGrid1CanEditCell(Sender: TObject; ARow,
  ACol: Integer; var CanEdit: Boolean);
Var
  I:integer;
begin

    //直接定義
    {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}

    //用屬性數組定義
    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;

    //以下代碼首先根據列cC1Data_Flag的值設定一行是否可以編輯,然後再根據屬性數組設定。
    {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;

//設定單元表各列對齊
procedure TForm1.strGrid1GetAlignment(Sender: TObject; ARow, ACol: Integer; var AAlignment: TAlignment);
Var
  I:integer;
begin
    //直接定義
    {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;  }
    //用屬性數組定義
    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;

//設定單元表各列顏色
procedure TForm1.strGrid1GetCellColor(Sender: TObject; ARow,
  ACol: Integer; AState: TGridDrawState; ABrush: TBrush; AFont: TFont);
Var
  I:integer;
begin

    //直接定義
    {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;}

    //用屬性數組定義
    if ARow=0 then
            abrush.Color:=clBtnFace        // 首行為灰色
    else begin
        for I:=0 to cColQty1 do begin      // 非首行按屬性數組設定顏色
            if ACol=I then abrush.Color:=StringToColor(cColProp1[I,5]);
        end;
    end;

end;

//設定單元表各列控制項類型
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 : 同意');
                 strGrid1.AddComboString('N : 拒絕');
             end;
         end;
    end;
   
end;

end.

    (以上過程在Delphi6中測試通過。)

    這樣,如果修改Cells相關的屬性,只需要修改數組cColProp1相關的值就可以實現了,很方便的。
    關於修改cColProp1,你可以直接在上面的代碼視窗中修改,但如果列相當多的話,這樣改起來也是相當麻煩的,而且容易改錯,也是不方便的!
    對此,我做了一個excel模板:動態Cells設定工具表,如附件。

    通過這個模板,可以自動產生靜態數組cColProp1和靜態列號,修改起來也很直觀,很方便。
    修改後,將產生的靜態數組cColProp1和靜態列號複製到代碼裡即可。

 

 動態Cells設定工具表: DynamicCells_Setting.xls  

http://www.taoyoyo.net/ttt/post/118.html

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.