線上訂房服務-台灣趴趴狗聯合訂房中心
發文 回覆 瀏覽次數:7404
推到 Plurk!
推到 Facebook!

VCL How To - DataControl 元件篇

 
bruce0211
版主


發表:157
回覆:668
積分:279
註冊:2002-06-13

發送簡訊給我
#1 引用回覆 回覆 發表時間:2002-08-14 23:36:09 IP:163.32.xxx.xxx 未訂閱
元件名稱:TDBGrid 文件版本:for Delphi 文件作者:Danny Tzu (deven_tzu.tw@yahoo.com.tw) 2002/08/14     ●[元件簡介] 資料瀏覽器 TDBGrid Delphi 有提供一系列的 Grid 的元件, 但屬本元件最為常用也最難使用, 因為先天設計上有一些問題, 所以幾乎都要改寫才能完成工作, 但更改並不容易, 採用如 InforPower...的元件是蠻多人的選擇.    ●[重要屬性] DataSource: 資料的來源, 可於 Design Time 用下拉式設定, 亦可於程式中動態設定, 如: DBGrid1.DataSource := DataModule1.DataSource; 但 Columns 中原來設定的 Fields 並不會自動消失 . ReadOnly:唯讀, 可以設定此 DBGrid 是否為可編輯資料狀態. Options:操作屬性, 內有可以設定之單獨選項, 有些會彼此影響(Delphi 會自動更動). dgEditing: 是否可以編輯資料 (與 ReadOnly 類似) dgAlwaysShowEditor: 是否欄位被選擇時自動進入編輯狀態. dgTitles: 是否顯示標題列. dgIndicator: 是否顯示左邊的資料指標圖示. dgColumnResize: 是否用滑鼠動態改變欄位寬度. dgColLines:  是否顯示欄位行分格線. dgRowLines: 是否顯示欄位列分格線. dgTabs: 是否可以用 TAB 鍵移動欄位. dgRowSelect: 是否選擇列顯示整行選擇, False是單一欄位顯示選擇. dgAlwaysShowSelection: 是否一直顯示左邊的資料指標, False是 DBGrid 取得焦點時才顯示. dgConfirmDelete: 是否刪除時顯示詢問視窗讓 User 確認. dgCancelOnExit: 是否 DBGrid 失去焦點時, 作 DataSet.Cancel 動作. dgMultiSelect: 是否可以用 [Ctrl] + [Mouse Left] 或 [Ctrl]+[Shift]+方向鍵 或 [Shift]+方向鍵作多重選擇.    □要用程式控制 Options 程式如下這樣:    DBGrid1.Options := [dgEditing, dgRowSelect];   // 指定的選項為 True    □設定某些選項為 True: DBGrid1.Options := DBGrid1.Options + [dgMultiSelect, dgConfirmDelete];     □設定某些選項為 False: DBGrid1.Options := DBGrid1.Options - [dgMultiSelect, dgConfirmDelete]; FieldCount:Columns 中設定有幾個 Field Fields:Columns 中的 Field, 用法如下(將所有 Field 設為 40 欄寬):
for i := 0 to DBGrid1.FieldCount -1 do
    DBGrid1.Fields[i].Width := 40;
SelectedField: 目前選擇的欄位 TField. SelectedIndex: 目前選擇的欄位順序索引值, 就是 Fields 範例的 i 值. □Columns: 是容納 Field 的物件, 其重要特性(屬性)如下: Buttonstyle: 欄位內的編輯按鍵顯示方式: cbsAuto: 自動依 PickList 是否有內容決定是否顯示. cbsEllipsis: 強迫顯示. cbsNone: 不顯示. DropDownRows: 下拉式選項列數, Buttonstyle 設為 cbsAuto, 且 Field 有 Lookup 欄位或 PickList 有內容才有效. Expands: 是否展開額外的資料欄位. FieldName: 指定來源欄位的 FieldName. ImeMode: 輸入法狀態. ImeName: 輸入法名稱. PickList: 下拉式選項的內容. ReadOnly: 本欄位是否可編輯資料狀態. Title: 欄位抬頭. Alignment: 對齊方式:taLeftJustify(向左靠),taRightJustify(向右靠),taCenter: 中間對齊 Caption: 抬頭說明 Color: 顏色 Font: 字型 Visible: 本欄位是否可顯示. Width: 欄位寬度. ●[重要事件] OnColEnter: 當欄位取得焦點時觸發.要知道是那個欄位取得焦點可以用 Column.FieldName 判斷. OnColExit: 當欄位失去焦點時觸發. OnDrawDataCell: Delphi 已經宣佈不再使用, 您就當作歷史遺跡吧! OnDrawColumnCell: 畫欄位資料時觸發. OnEditButtonClick: 編輯按鍵被按下後觸發. OnCellClick: 當欄位被選取時觸發. OnTitleClcik: 當欄位標題被按下時觸發. ●[範例說明] □某特定值為真時, 動態改變顏色
procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect; 
DataCol: Integer; Column: TColumn; 
State: TGridDrawState); 
begin       If Column.FieldName = 'NAME' then  // 當畫的欄位是 NAME 時
     If Column.Field.Dataset.FieldbyName('OK').AsBoolean then 
        dbgrid1.canvas.brush.color := clBlack         //focused 
     else             dbgrid1.canvas.brush.color := clPaleGreen; //not focused        // 呼叫內定的 DrawColumnCell
   dbgrid1.DefaultDrawColumnCell(rect,DataCol,Column,State) 
end;
□將選擇的列改變顏色
procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; 
const Rect: TRect; DataCol: Integer; Column: TColumn; 
State: TGridDrawState); 
begin 
   with TDBGrid(Sender) do 
      if DataLink.ActiveRecord = Row - 1 then 
        Canvas.Brush.Color := clRed 
      else 
        Canvas.Brush.Color := clWhite; 
   DefaultDrawColumnCell(Rect, DataCol, Column, State); 
end; 
□顯示 Memo 欄位於 DBGrid 中
procedure TForm1.DBGrid1DrawDataCell(Sender: TObject; const 
Rect: TRect; Field: TField; State: 
TGridDrawState); 
var 
  P: array [0..50] of char; {array size is number of characters needed} 
  bs: TBlobStream; {from the memo field} 
  hStr: String; 
begin 
  if Field is TMemoField then 
  begin 
     with (Sender as TDBGrid).Canvas do 
     begin {Table1Notes is the TMemoField} 
        bs := TBlobStream.Create(Table1Notes, bmRead); 
        FillChar(P,SizeOf(P),#0); {terminate the null string} 
        bs.Read(P, 50); {read 50 chars from memo into blobStream} 
        bs.Free; 
        hStr := StrPas(P); 
        while Pos(#13, hStr) > 0 do {remove carriage returns and} 
           hStr[Pos(#13, hStr)] := ' '; 
        while Pos(#10, hStr) > 0 do {line feeds} 
           S[Pos(#10, hStr)] := ' '; 
        FillRect(Rect); {clear the cell} 
        TextOut(Rect.Left, Rect.Top, hStr); {fill cell with memo data} 
     end; 
  end; 
end; 
□指到某欄位
DbGrid1.SelectedIndex:=ComboBox1.ItemIndex;
5. 指到某欄位名稱處
function SetDBGridFocusByFieldname(ADBGrid: TDBGrid; AFieldName: String): Integer;    var i: Integer;    begin       for i := 0 to ADBGrid.Columns.Count-1 do
   begin
      if ADBGrid.Columns[i].FieldName = AFieldName then          begin             ADBGrid.SelectedIndex:= i;             Result := i;             Break;          end;  
   end;
end;
●[使用經驗]
Delphi 的 DBGrid 無法提供目前的所在位置, 以下元件可以達到    unit ahDBGrid;    interface    uses
   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
   Grids, DBGrids;    type
   TahDBGrid = class(TDBGrid)
   private
      { Private declarations }
      function GetPointData: TPoint;
   protected
      { Protected declarations }
   public
      { Public declarations }
      constructor Create(AOwner:TComponent);override;
      destructor Destroy;override;
   published
      { Published declarations }
      property GetPoint: TPoint read GetPointData;
   end;    procedure Register;    implementation    procedure Register;
begin
   RegisterComponents('Samples', [TahDBGrid]);
end;    { TahDBGrid }    constructor TahDBGrid.Create(AOwner: TComponent);
begin
   inherited Create(AOwner);
end;    destructor TahDBGrid.Destroy;
begin
   inherited Destroy;
end;    function TahDBGrid.GetPointData: TPoint;
begin
   Result.x := Col;
   Result.y := Row;
end;    end.
發表人 - bruce0211 於 2002/08/20 09:12:40
系統時間:2024-03-29 7:48:53
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!