国产一级a片免费看高清,亚洲熟女中文字幕在线视频,黄三级高清在线播放,免费黄色视频在线看

打開APP
userphoto
未登錄

開通VIP,暢享免費電子書等14項超值服

開通VIP
轉(zhuǎn)載DBGrid和DBGridEH

轉(zhuǎn)載DBGridDBGridEH

二、應(yīng)用實例
    Enlib3.0組件包安裝成功后
A、定制標題行
 1、制作復(fù)雜標題行
    標題行可設(shè)為2行以上高度,并可以為多列創(chuàng)建一個共同的父標題行。為實現(xiàn)這個效果,需在各個列標題屬性中以分隔父標題和子標題,如辦公用品包括代碼和名稱兩部分,具體屬性設(shè)置如下:
 
usemultititile=true;
titlelines=2
DBGridEh.Columns[0].Title.Caption := '辦公用品|代碼';
DBGridEh.Columns[1].Title.Caption := '辦公用品|名稱';
 
 2、標題行顯示圖片 
    實現(xiàn)圖2中的購買人標題行顯示效果。首先添加一個imagelist組件img1并在其中添加一組bmp,ico格式的圖片。然后將DBGridEhTitleImages設(shè)置為img1.最后在需要顯示圖片的列標題的imageindex中設(shè)置需要顯示的img1中圖片的序號。按F9執(zhí)行一下程序,是不是很酷!
 
 3、自動顯示標題行的升降排序標志符(降序升序)并做相應(yīng)排序
    DBGridEh組件可以在標題行單元格中顯示小三角形升、降排序標志符圖片,在運行時可點擊標題行,圖片自動切換并做相應(yīng)排序。具體屬性設(shè)置如下:
 
OptionsEhdghAutoSortMarking 
Column.Title.TitleButtontrue 
 
SortMarkedColumns 為當(dāng)前排序列可在運行時使用.
然后在該列的ontitleclick事件中添加代碼:
procedure TForm_Query.DBGridEh1TitleBtnClick(Sender: TObject; ACol: Integer; Column: TColumnEh);
var
  sortstring:string; //排序列
begin
  //進行排序
  with Column do
  begin
    if FieldName = '' then
      Exit;
    case Title.SortMarker of
      smNoneEh:
      begin
        Title.SortMarker := smDownEh;
        sortstring := Column.FieldName + ' ASC';
      end;
      smDownEh: sortstring := Column.FieldName + ' ASC';
      smUpEh: sortstring := Column.FieldName + ' DESC';
    end;
  //進行排序
    try
      dataset.Sort := sortstring //dataset為實際數(shù)據(jù)集變量名
    except
    end;
  end;
end;
 
切記lookup型字段不可做上述設(shè)置,否則系統(tǒng)會提示錯誤。
 
  
 
B、定制表格底部(footer)區(qū)域的匯總統(tǒng)計行
    DBGridEh 組件可以在表格底部顯示匯總行,如記錄數(shù)合計、列字段累加和等信息。在FooterRowCount中設(shè)置底部顯示的行數(shù);然后在Footers 編輯器中添加一個或多個顯示列,顯示列可以是字段值累加和、記錄數(shù)合計、字段值或靜態(tài)文件等集合類型,可以在設(shè)計時在ValueType屬性中設(shè)置,也可在運行時通過設(shè)置Footers[i].ValueType指定其類型。其含義見下表:
 
    切記設(shè)置DBGridEh.SumList.Active  True,才會進行匯總統(tǒng)計運算。需注意的是,如顯示類型為不是當(dāng)前列的累加和,則需在fieldname屬性中指定匯總列,其它類型則無此要求。 
 
 
 
C、定制表格數(shù)據(jù)單元外觀
 1、根據(jù)不同字段值顯示相應(yīng)的小圖片
    如根據(jù)庫存材料的不同狀態(tài)在數(shù)據(jù)單元格中顯示相應(yīng)圖片,具體設(shè)置如下:
    添加一個imagelist組件img1并在其中添加一組bmp,ico格式的圖片。然后將需要顯示圖片的列的imagelist屬性設(shè)置為img1;在keylist屬性中添加實際數(shù)據(jù)存儲值,一行為一個值,切記一定要與imagelist中圖片順序一一對應(yīng),否則會張冠李戴,面目全非。還可在picklist中添加提示信息,也要求是一行為一個值,并設(shè)tooltiptrue,那么,運行時當(dāng)鼠標移動到該數(shù)據(jù)單元格時在顯示圖片的同時還顯示提示信息,怎么樣,功能夠強大吧!可使用空格鍵或鼠標切換下一張圖片,圖片切換的同時也改變了實際存儲數(shù)據(jù)值。也可通過shift+空格或鼠標切換為上一張圖片。這樣就實現(xiàn)了上下兩個方向圖片切換。
 
 2、顯示檢查框(checkbox)外觀
    對于Boolean型字段值在dbgrideh組件中自動顯示為檢查框。通常情況下我們需將非Boolean型字段值也此外觀顯示,如性別字段為字符型,字段值為男性時為選中,女性時為未選中。需要在keylist編輯器中設(shè)置實際存儲數(shù)據(jù)值,第一行為選中時的值1”,第二行為未選中的值0”,第三行為其它值2”,支持三態(tài)顯示。設(shè)checkbox屬性為True.
 
 3、顯示單、多列下拉列表
    根據(jù)單元格字段值顯示與其相關(guān)的其它表字段內(nèi)容,如部門代碼字段顯示為部門名稱。首先需在當(dāng)前表中新建立一個lookup型字段,設(shè)置好關(guān)聯(lián)表的字段和返回字段。多列下拉列表需在單列基礎(chǔ)上做進一步設(shè)置,在LookupDisplayFields中以;號將關(guān)聯(lián)表中多個字段分隔開,而且返回字段必須作為其中的第一項。具體設(shè)置如下:
 
dropdownshowtitlestrue 
dropdownsizingtrue
dropdownwidth-1 
 
    例:當(dāng)前表中只有部門代碼無部門名稱列,需與部門表建立關(guān)聯(lián),當(dāng)點擊單元格時以部門代碼、部門名稱兩列下拉列表形式顯示。
 
 4、顯示日歷下拉列表
    Date  DateTime類型字段值均可以此形式顯示。外觀與編輯框無異,當(dāng)點擊該單元格時,右側(cè)會出現(xiàn)“▽”符號,點擊之即可出現(xiàn)日歷下拉列表。有時不希望出現(xiàn)日歷下拉列表,只需設(shè)置Column.ButtonStyle屬性為 cbsNone即可,此方法同樣適用于其它組件不以特殊外觀顯示的情況。
 
 53D或平面外觀效果
    設(shè)置OptionsEh屬性 中fixed, frozen, footer  data rows等屬性表格外觀為3D效果,設(shè)置flattrue則為平面外觀效果.
 
 6、鎖定多列不滾動
    當(dāng)表格水平方向信息在一屏幕顯示不下時,此項功能非常有用。例如,工資表格中包含姓名、基本工資、績效工資等信息一屏幕顯示不下,需要通過移動水平滾動條顯示下一屏信息。如果不鎖定關(guān)鍵字段列如姓名,則移動到下一屏?xí)r就不知道此條記錄對應(yīng)的姓名。因此,在實際應(yīng)用中經(jīng)常需鎖定多列不滾動。
 
    例:姓名字段為表格第二列,則設(shè)置FrozenCols=2.這樣當(dāng)一屏幕顯示不下,通過移動水平滾動條顯示下一屏信息時,表格前兩列不滾動,作為參照列。
 
D、導(dǎo)入/導(dǎo)出數(shù)據(jù)
    導(dǎo)入/導(dǎo)出數(shù)據(jù)在實際處理過程中是比較煩瑣的。但是Enlib3.0提供了一系列函數(shù)讓你輕松實現(xiàn)此功能,而且支持的文件格式很多:Text, Csv, HTML, RTF, XLS 和內(nèi)部數(shù)據(jù)格式。除此之外,還可對任意選擇的數(shù)據(jù)區(qū)域進行操作。函數(shù)如下:
 
Pascal: SaveDBGridEhToExportFile(TDBGridEhExportAsText,DBGridEh1,'c:\temp\file1.txt',False);
 
C++: SaveDBGridEhToExportFile(__classid(TDBGridEhExportAsText),DBGridEh1,"c:\\temp\\file1.txt",false);
 
說明:其中false參數(shù)表示導(dǎo)出的是選中的局部數(shù)據(jù)區(qū)域數(shù)據(jù),true則為整個表格數(shù)據(jù)。
 
   例:將當(dāng)前表格中數(shù)據(jù)導(dǎo)出為EXCEL等格式文件。
    在窗體中添加一個SaveDialog組件和導(dǎo)出按鈕B_exp,在導(dǎo)出按鈕的click事件中添加如下代碼:
 
procedure TForm1.B_expClick(Sender: TObject);
var 
  ExpClass:TDBGridEhExportClass;
  Ext:String;
begin
  SaveDialog1.FileName := 'file1';
  if (ActiveControl is TDBGridEh) then
    if SaveDialog1.Execute then
    begin
      case SaveDialog1.FilterIndex of
        1: begin ExpClass := TDBGridEhExportAsText; Ext := 'txt'; end;
        2: begin ExpClass := TDBGridEhExportAsCSV; Ext := 'csv'; end;
        3: begin ExpClass := TDBGridEhExportAsHTML; Ext := 'htm'; end;
        4: begin ExpClass := TDBGridEhExportAsRTF; Ext := 'rtf'; end;
        5: begin ExpClass := TDBGridEhExportAsXLS; Ext := 'xls'; end;
      else
        ExpClass := nil; Ext := '';
    end;
    if ExpClass <> nil then
    begin
      if UpperCase(Copy(SaveDialog1.FileName,Length(SaveDialog1.FileName)-2,3)) <> UpperCase(Ext) then
        SaveDialog1.FileName := SaveDialog1.FileName + '.' + Ext;
      SaveDBGridEhToExportFile(ExpClass,DBGridEh1,SaveDialog1.FileName,False);
      //其中false為局部數(shù)據(jù)
    end;
  end;
end;
 
E、將存在的DBGrid組件轉(zhuǎn)換為DBGridEh組件. 
    通過筆者上述介紹,想必你已經(jīng)對Enlib組件包產(chǎn)生好感而且越越欲試了,那就趕快下載使用吧。但是,使用一段時間并且喜歡上它后,你又有新的問題產(chǎn)生了,那就是為了保持界面風(fēng)格一致,能否將已經(jīng)開發(fā)完成的應(yīng)用程序中的DBGrid組件能否轉(zhuǎn)換為DBGridEh組件,進行一次徹底革命?答案是肯定的。盡管DBGridEh并不是繼承于CustomDBGrid組件, 但是DBGridEhDBGrid它們之間有許多相同之處.因此可以相互轉(zhuǎn)換。
 
具體步驟如下:
 1、在Delphi IDE下打開TDBGrid組件.
 2、通過組合鍵Alt-F12form 以文本方式顯示;
 3、將所有TDBGrid 對象名改變?yōu)?span lang="EN-US"> TDBGridEh對象名,如:DBGrid1: TDBGrid改為 DBGrid1: TDBGridEh
 4、再次通過組合鍵Alt-F12將文本方式恢復(fù)為form 顯示;
 5、將form各相關(guān)事件中定義的所有TDBGrid改為TDBGrideh,DBGrid1: TDBGrid改為DBGrid1: TDBGridEh;
 6、重新編譯應(yīng)用程序。
 
 
 
DBGRIDEH Enlib 3.0組件包中的組件之一。Enlib 3.0組件包是一位俄國人為增強Borland系列開發(fā)工具功能而開發(fā)的第三方組件,它具有界面友好、功能強大、開發(fā)效率高、、快速制作預(yù)覽/打印簡單中國式報表等特點。因此,一推出即受到廣大Borland程序員的青睞。目前這個版本支持Borland Delphi versions 4,567  Borland C++ Builder versions 4 & 5 ,可極大地提高數(shù)據(jù)庫應(yīng)用系統(tǒng)客戶端的性能。許多商品軟件如《速達2000》等都使用了該組件。下面本人將使用該組件在實際系統(tǒng)開發(fā)過程中的經(jīng)驗總結(jié)如下。 
 
Enlib3.0組件包中最重要而且功能最強大的莫過于dbgrideh組件,本文介紹的所有實例均在Delphi 7開發(fā)環(huán)境下調(diào)試通過。
 
一、DBGridEh(增強型表格組件)功能詳解
DBGridEh組件無論在外觀上還是功能上都非常類似Borland開發(fā)工具中現(xiàn)有的dbgrid組件,它除了提供dbgrid組件的全部功能外,還增加了下列新功能:
 
任意選擇多行、列或矩形區(qū)域的數(shù)據(jù).
為多列標題設(shè)定共同的父標題行.
表格底部(Footer) 區(qū)顯示求和、計數(shù)和其它統(tǒng)計信息.
自動調(diào)整組件寬度與客戶區(qū)域等寬.
設(shè)置標題行、數(shù)據(jù)行的高度.
超長的標題行、數(shù)據(jù)行文本自動折行處理.
標題行可作為按鈕使用,并可選擇是否顯示排序標志符(降序升序).
點擊列標題可對當(dāng)前列自動排序而無需編寫代碼.
能夠自動設(shè)置刪除超長文本顯示不下的多余部分,并以省略號()代替.
自動搜索字段(Lookup)數(shù)據(jù)單元格以單、多列字段下拉列表形式顯示.
自動搜索字段(Lookup)數(shù)據(jù)單元格可進行增量搜索.
可鎖定任意列數(shù)在屏幕水平方向不滾動.
日期時間控件DateTime picker 可支持TDateField and TDateTimeField兩種日期格式.
根據(jù)字段不同值顯示關(guān)聯(lián)的ImageList 對象圖片組中的圖片.
隱藏任意列.
顯示3D風(fēng)格的數(shù)據(jù)區(qū)、表尾區(qū)和鎖定滾動列,制作3D外觀表格.
顯示Memo類型字段值.
BOOLEAN型數(shù)據(jù)外,其它數(shù)據(jù)類型也可以檢查框( checkbox )形式顯示數(shù)據(jù).
使用專門的函數(shù)和過程來存取以regini文件格式保存的表格布局(包含各數(shù)據(jù)列表、數(shù)據(jù)列訪問順序、列寬、索引標識、行高等信息)文件。
通過設(shè)置數(shù)據(jù)單元格的hintToolTips屬性,當(dāng)移動鼠標到該單元格時,可以顯示單元格容納不下的文本內(nèi)容.
將組件中數(shù)據(jù)導(dǎo)入/導(dǎo)出到Text, Csv, HTML, RTF, XLS 和內(nèi)部數(shù)據(jù)等多種格式的文件中.
 
DBGridEh組件主要屬性見下表(其它屬性參見dbgrid):
 
DBGridEh組件事件基本與DBGrid相同,在此不再贅述。
 
 
 
DBGridEH 所有列寬自動適應(yīng)的實現(xiàn)
interface
THackDBGridEH = class(TCustomdbgrideh)
end;
 
procedure OptimizeGrid(AGrid: TCustomDbGridEh);
 
implementation
procedure OptimizeGrid(AGrid: TCustomDbGridEh);
var
i: integer;
begin
// 優(yōu)化GRID的寬度
for i := 0 to TDBGridEh(AGrid).Columns.count - 1 do
THackDBGridEH(AGrid).OptimizeSelectedColsWidth(TDBGridEh(AGrid).Columns[i]);
end;
 
 
 
dbgrideh中直接點擊title就可按點擊的那個字段排序的方法
第一種方法(未測試)
procedure TForm1.DBGridEh1TitleClick(Column: TColumnEh);
begin
//點擊GridEh標題排序
if (Column.Title.SortMarker = smNoneEh) or (Column.Title.SortMarker = smDownEh) then
  begin
   ADOQuery1.SORT := COLUMN.FIELDNAME;
   Column.Title.SortMarker := smUpEh
  end
else
  begin
   ADOQuery1.SORT := COLUMN.FIELDNAME + ' DESC';
   Column.Title.SortMarker := smDownEh
  end;
end; 
 
第二種方法(未測試)
procedure TPrintMai_frm.DBGridEh1TitleClick(Column: TColumnEh);
var
sortstring: string;
begin //進行排序
with Column do
begin
  if FieldName = '' then
   Exit;
  case Title.SortMarker of
   smNoneEh:
    begin
     Title.SortMarker := smDownEh;
     sortstring := Column.FieldName + ' ASC';
    end;
   smDownEh: sortstring := Column.FieldName + ' ASC';
   smUpEh: sortstring := Column.FieldName + ' DESC';
  end; //數(shù)據(jù)集排序。
  try
   DM.DataModule1.qry2.Sort := sortstring //dataset為實際數(shù)據(jù)集變量名
  except
  end;
end;
end; 
 
 
 
 
 
 
Delphi 7中的安裝方法
 
  1.  EhLib 中的 common  DataService 文件拷貝到 Delphi7 目錄中.
  2. TOOLS->Environment Options->Library->Library Path 中添入EHLIB路徑。
  3.打開新建文件夾中的 EHLIB70.DPK ,編譯一下,但不要安裝。
  4.打開Ehlib中的DclEhLib70.DPK,編譯,安裝 
  5. Delphi 7中打開DclEhLib70.dpk,編譯并安裝。
  6. 組件面板中出現(xiàn)一個EhLib的組件頁。
  7. 打開附帶的DEMOS,編譯并運行,測試安裝成功。
 
 
 
以下是EHLIB的導(dǎo)出代碼:(其實EHLIBDEMO1中已有)
 
procedure TInvoiceManager.ppmSaveSelectionClick(Sender: TObject);
var ExpClass:TDBGridEhExportClass;
  Ext:String;
begin
 SaveDialog1.FileName := 'file1';
 if (ActiveControl is TDBGridEh) then
  if SaveDialog1.Execute then
  begin
   case SaveDialog1.FilterIndex of
    1: begin ExpClass := TDBGridEhExportAsText; Ext := 'txt'; end;
    2: begin ExpClass := TDBGridEhExportAsCSV; Ext := 'csv'; end;
    3: begin ExpClass := TDBGridEhExportAsHTML; Ext := 'htm'; end;
    4: begin ExpClass := TDBGridEhExportAsRTF; Ext := 'rtf'; end;
    5: begin ExpClass := TDBGridEhExportAsXLS; Ext := 'xls'; end;
   else
    ExpClass := nil; Ext := '';
   end;
   if ExpClass <> nil then
   begin
    if UpperCase(Copy(SaveDialog1.FileName,Length(SaveDialog1.FileName)-2,3)) <>
      UpperCase(Ext) then
     SaveDialog1.FileName := SaveDialog1.FileName + '.' + Ext;
    SaveDBGridEhToExportFile(ExpClass,TDBGridEh(ActiveControl),
       SaveDialog1.FileName,False);
   end;
  end;
end;
 
 
 
// 功能:設(shè)定 DbGridEh 合計行信息
// 參數(shù): pDbGrid:TDBGridEh;
//     pcFields : string ; 字段列表,字段用逗號分隔
//     pvtType : TFooterValueType ; 統(tǒng)計類型 TFooterValueType = (fvtNon, fvtSum, fvtAvg, fvtCount, fvtFieldValue, fvtStaticText);
// 引用:
// 例如:DbGridEhFoot( DbGridEh1, &acute;Number,Sum&acute;, fvtSum ); 設(shè)定數(shù)量和金額字段為合計統(tǒng)計
//--------------------------------------------------------------------------------
Procedure DbGridEhFoot( pDbGrid:TDBGridEh; pcFields: string; pvtType : TFooterValueType );
var nFldLoop : integer ;
   cFieldName : string ;
   tmpFldList : TStrings ;
begin
  pDbGrid.FooterRowCount := 1;   // 指定網(wǎng)格尾部統(tǒng)計行行數(shù)
  pDbGrid.SumList.Active := true;  // 激活統(tǒng)計
  pDbGrid.FooterColor   := clBtnFace ;  // 指定統(tǒng)計行顏色
  tmpFldList := TStringList.Create ;
  StrToStringList( Uppercase(pcFields),&acute;,&acute;,tmpFldList );  // 將字符串轉(zhuǎn)換為串列表
  For nFldLoop := 0 to pDbGrid.Columns.Count -1 do
  begin
    cFieldName := pDbGrid.Columns[nFldLoop].FieldName ;  // 網(wǎng)格列字段名
    if tmpFldList.IndexOf( uppercase( cFieldName ) ) >= 0 then
    begin
     pDbGrid.Columns[nFldLoop].Footer.ValueType := pvtType ;  // 統(tǒng)計類型
    end;
  end ;
  tmpFldList.Free ;
end;
 
 
 
 
 
 
一、如何在程序中確定Ehlib定義的報表表頭顏色? 
在執(zhí)行打印之前,加上下面的語句:
DBGridEh1.FixedColor:=clLime;//clLime可以換成你想要的顏色比如clRed是紅色等等
PrintDBgridEh1.Options:=[pghColored,pghFitingByColWidths];//方括號里的"pghColored"是必需的,其它的根據(jù)你的需要決定取舍
 
二、Ehlib中的PrintDBGridEh如何印頁碼,即第幾頁共幾頁
PrintDBGridEh.PageFooter.CenterText:='&[Page]頁 共&[Pages]'
在對象管理器中的相關(guān)位置有設(shè)
 
三、本人剛學(xué)習(xí)使用ehlib包,現(xiàn)在要實現(xiàn)類似ehlib所帶demo1中一個功能:點擊dbgrid某列值
的下拉按紐(可以設(shè)置),彈出一個下拉列表(其內(nèi)容是另一個dataset的)。我看demo1
好象是使用了DBLookupComboboxEh,可我參看demo1怎么設(shè)置也不行,請指教。謝謝。
 
呵呵,這個效果沒有用DBLoookupComboBoxEh,它是利用查找字段實現(xiàn)的。
它在Query1中,對應(yīng)VName字段,增加了一個叫VName1的查找字段,這個
字段從qrVendors查找相關(guān)的數(shù)據(jù),再在DBGridEh1顯示VName1,這樣顯示
就是那種效果了。 
 
四、Ehlib 怎樣固定某幾列?
只要設(shè)置dbGridEhFrozenCols屬性為幾列即可.
3 使用 TDBGridEh 組件
理解 TDBGridEh, TDataLink 以及 TDataSet. 
All below text in equal degrees pertains as to TDBGridEh component as to TDBGrid component.
A TDBGridEh control lets you view and edit records in a dataset in a tabular grid format.
TDBGridEh does not story data in in itself, it only show data from dataset via TDataLink object. Every database control have internal TDataLink object to interaction with dataset. You can connect TDBGridEh to dataset using DataSource property. If you already use TStringGrid component you can see that data shows in TStringGrid and in TDBGridEh very similarly, but mechanism that use to show data in TStringGrid and in TDBGridEh very different. In TStringGrid count of rows is equal of rows in array of strings while in TDBGridEh (and TDBGrid) count of rows always not more then count of visible rows and although vertical scrollbar can display thumb position regarding the count of record in dataset it take info not from grid but directly from dataset. TDataSet don't allows us to work with data as with array of data i.e. we can not quickly get value of the field from certain record, some types of dataset have not even such notion as record number (in such datasets we can only know that we inhere in the begin of dataset or in the end of its or somewhere between, in that case DBGrid shows vertical vertical scrollbar only in three positions). But to have possibility to draw several record simultaneously TDataLink object allows to have buffer of records (record buffer window) with quick readonly access. DBGrid use this possibility of datalink and set size of record buffer window equal of count of visible rows in grid. We can not control what record must be first in this buffer, DataLink itself scroll record buffer window then we navigate through the dataset and it control the scrolling of record buffer window as that the active record as always in record buffer window. It is a reason why the active record change position when users change thumb position of vertical scrollbar using mouse. 
 
TDBGridEh和縱向滾動條 
If you works with different type of dataset you can notice that for some type of dataset DBGrid show vertical scrollbar validly but for over vertical scrollbar have only three position independently of record count in dataset. To set vertical scrollbar accomodation DBGrid use RecordCount and RecNo property of DataSet component. But some dataset and even same dataset under some condition holds -1 in RecordCount and RecNo. DataSet function IsSequenced indicates whether the underlying database table uses record numbers to indicate the order of records. When IsSequenced returns True, applications can safely use the RecNo property to navigate to records in the dataset and DBGrid use RecNo property to show thumb position in vertical scrollbar. But when IsSequenced returns False DBGrid can not define current record position and show vertical scrollbar in three positions. DBGridEh component have possibility to show proportional scrollbar for no sequenced dataset. To do it need to activate SumList and create list of record bookmars. Set SumList.Active to True and SumList.VirtualRecords to True. SumList will run through dataset and create list of record bookmarks, if you use client/sever technology to access database SumList will force dataset to fetch all records, so it operation can take much time. Keep in mind that VirtualRecords will work only for full relationship bookmarks dataset, it means that DataSet.ComapreBookmark function has to return > 0 if bookmark1 > bookmark1 (i.e. record to which indicates bookmark1 have to be after record to which indicates bookmark1), = 0 if bookmark1 = bookmark1, < 0 if bookmark1 = bookmark1. TBDEDataSet in most cases support full relationship bookmarks.
 
 
定制網(wǎng)格標題 
復(fù)雜標題 
TDBGridEh 允許在多列上創(chuàng)建標題,例如: 
 
設(shè)置 DBGridEh.UseMultiTitle 屬性為 True 并且填充字段的標簽或列標題的標題,可以使用下面的規(guī)則:字段標簽中的文本部分或列標題必須由幾部分組成,并且用 "|" 分割,幾個列的每一個通用部分都設(shè)置為相同。其它字段或標題必須在相應(yīng)的部分包含同樣的文本。 
 
例如:
 
Field1.DisplayLabel := 'Title1|SubTitle1';
Field2.DisplayLabel := 'Title1|SubTitle2';
DBGridEh.Columns[0].Title.Caption := 'Title1|SubTitle1';
DBGridEh.Columns[1].Title.Caption := 'Title1|SubTitle2'; 
按鈕式標題 
設(shè)置Column.Title.TitleButton  True可以強制標題單元為按鈕式。寫 OnTitleBtnClick事件來控制用戶單擊標題單元時的操作。 
 
在標題中顯示位圖 
To show bitmap in titles instead of caption use TitleImages property of TDBGridEh and ImageIndex property of TColumnTitleEh. 
 
自動用位置標識排序標題. 
TDBGridEh allows to show special sortmarking bitmaps (small triangles) in the right part of title cell. In order to automatically marking title by sortmarking bitmaps add dghAutoSortMarking to OptionsEh property. Add dghMultiSortMarking too in order to allow sortmarking several columns simultaneously. Set Column.Title.TitleButton to true for titles which will have possibility to change sortmarkers at run time. At runtime clicking on title will change sortmarking. Holding Ctrl key allows to mark several columns simultaneously. After user change sormarking grid call OnSortMarkingChanged event. You can write this event to change sorting and reopen in dataset. Use SortMarkedColumns property to access to sortmarked columns. 
 
標題屬性的默認值 
使用TDBGridEh.ColumnDefValues.Title來設(shè)置標題屬性的默認值。 
 
定制網(wǎng)格頁腳 
頁腳以及統(tǒng)計值 
TDBGridEh allows to show special row (footer) or rows at bottom part. Use FooterRowCount property to specify the number of footer rows in the grid. Use Footer or Footers property of TColumnEh object to specify information which need to show in footer cells. Footers property useful then you have more then one footer rows. Footers is a collection of TColumnFooterEh objects where information from i-th aliment of collection will be show in i-th cell of footer column. In footer cell, it is possible to show: Sum value for specified field, record count, value of a specified field or static text. Use property Footer.ValueType or Footers[i].ValueType to specify which type of value will be show in footer cell. If ValueType = fvtStaticText, then set the property Value to specify text which need to show. If ValueType = fvtFieldValue, then you need to set property FieldName to specify field, value of which need to show. To force grid to calculate total values need to activate SumList (DBGridEh.SumList.Active := True). Set ValueType to fvtSum and grid must to show sum value of the column field in the footer cell, you can also specify Column.Footer.FieldName to calculate total value of the other field. Set ValueType to fvtCount to force grid to show count of records in the footer cell.
 
 
定制網(wǎng)格數(shù)據(jù)單元 
在數(shù)據(jù)單元中顯示字段值為圖形。 
TDBGridEh allows to show bitmaps from TImageList component depending on field values. To show bitmaps depending on field values need: Fill list of field values to Column.KeyList property (every value in separate line) and set Column.ImageList property to ImageList control that has the bitmap in according index. Set Column.NotInKeyListIndex to index of bitmap that will be shown if field's value does not correspond to any value in KeyList (for instance you can set index of image for Null field value). At run time you are not allowed to edit bitmap in column cell. Use blank key and mouse click to set next value from Column.KeyList to the field; Shift-blank key and Shift-Mouse click to set previous value from Column.KeyList. Set Column.DblClickNextval to True have allows to change value on mouse double click. 
 
檢查框式的邏輯及非邏輯值
Grid automatically shows checkboxes for boolean field. To show checkboxes for non boolean fields fill first line of Column.KeyList property that corresponds to the checked state of the checkbox, second line - non checked state, and set Column.Checkboxes ptoperty to True. Line of KeyList can represent more than one value in a semicolon-delimited list of items. 
 
數(shù)據(jù)行高度 
使用 RowHeight  RowLines 屬性來指定數(shù)據(jù)行高。完整的數(shù)據(jù)行高 = 行線高度+行高。設(shè)置 RowSizingAllowed  True 以允許可以在運行是使用鼠標來改變行高。
 
設(shè)置Column.WordWrapTrue可以使數(shù)據(jù)行中文本多行顯示。如果行高>文本行,它就換行。 
 
顯示備注字段 
設(shè)置 DrawMemoText to True來顯示文本式的備注字段。. 
 
定制單元格字體及顏色 
TDBGridEh 中的 Font  Color 屬性描述了數(shù)據(jù)網(wǎng)格中繪制單元格的字體和顏色。
TColumnEh 中的 Font  Color 屬性描述了指定列中繪制單元格的字體和顏色。 
 
事件定制單元格字體及顏色 
有幾個事件可以讓你能夠在繪制單元格前定制單元格字體和顏色。你可以寫TDBGridEhOnDrawColumnCellEvent事件句柄來在控制在網(wǎng)格單元中繪制數(shù)據(jù)。你可以使用Canvas屬性的方法來繪制單元格。但是如果你只想改變字體或顏色的屬性,我建議你使用下面的事件。你可以寫TDBGridEhOnGetCellParams事件來控制在繪制數(shù)據(jù)單元以前所指定的操作。你可以改變繪制字體及背景色。這個事件適合你在想改變整行的字體或顏色時使用。如果你想改變指定列中單元格的屬性,你可以使用TColumnEh.OnGetCellParams。寫這個事件用來控制在一列數(shù)據(jù)單元被重繪或編輯時的操作。在一列數(shù)據(jù)單元被重繪以前,你可以改變繪制字體,背景色,對齊方式,圖像索引,文本或檢查框。在編輯一列數(shù)據(jù)單元以前,你可以改變編輯字體,背景色,文本或只讀狀態(tài)。 
 
列屬性的默認值 
 
使用ColumnDefValues屬性來設(shè)置列屬性的默認值。新創(chuàng)建的列將從ColumnDefValues屬性中獲得屬性值,并且直到第一次為其指定值為止。
 
 
在網(wǎng)格的適當(dāng)位置放置編輯器. 
在下拉列表中顯示幾個字段。 
在下拉列表中顯示幾個下拉字段,需要設(shè)置列的LookupDisplayFields屬性到字段的Semicolons屬性來分割多個字段名。命名為Column.Field.LookupResultField的屬性必須位于LookupDisplayFields列表中。多字段的下拉列表只能應(yīng)用于下拉字段。 
 
顯示下拉方式的列 
 
你可以通過KeyList  PickList 屬性在相關(guān)的的字段中顯示其它文本。KeyList顯示包含在字段的值而非PickList索引所包含的值。 Column.NotInKeyListIndex to index of text from PickList that will be shown if field value do not contain in KeyList (for instance you can set index of text for Null field value). Set Column.DblClickNextval to True to change value on mouse double click. 
 
下拉式計算器 
對于 TDateField  TDateTimeField 字段,inplace 編輯器將顯示下拉按鈕以顯示顯示下拉計算器。設(shè)置 Column.ButtonStyle  cbsNone 以禁止顯示下拉按鈕。 
 
設(shè)置編輯器顏色和字體 
Inplace編輯器可以設(shè)置數(shù)據(jù)單元的顏色和字體。數(shù)據(jù)單元使用OnGetCellParams 事件來控制列的顏色和字體。 Inplace 編輯器在行高>一行的高度時自動設(shè)置為多行模式并且將列的屬性 WordWrap 設(shè)置為True.
 
 
自動填充網(wǎng)格列寬到網(wǎng)格客戶區(qū) 
設(shè)置AutoFitColWidthsTrue以自動重置列寬來設(shè)置網(wǎng)格的寬度等于客戶區(qū)寬度。 MinAutoFitWidth 屬性決定網(wǎng)格的最小寬度,列寬將會被重新計算。 
3D或平面外觀 
使用 OptionsEh 屬性來顯示/隱藏固定的3D框架,冷區(qū),頁腳以及數(shù)據(jù)行。 
 
使用 Flat 屬性來設(shè)置用平面方式顯示數(shù)據(jù)網(wǎng)格。 
 
從多種格式導(dǎo)入/導(dǎo)出數(shù)據(jù)到TDBGridEh。 
EhLib 的函數(shù)集可以從DBGridEh導(dǎo)出數(shù)據(jù)到Text, Csv, HTML, RTF, XLS以及其內(nèi)部格式。它可以保存數(shù)據(jù)到流(TStream對象)或文件。 
 
例子
Pascal: SaveDBGridEhToExportFile(TDBGridEhExportAsText,DBGridEh1,'c:\temp\file1.txt',False);
C++: SaveDBGridEhToExportFile(__classid(TDBGridEhExportAsText),DBGridEh1,"c:\\temp\\file1.txt",false); 
 
EhLib 的函數(shù)集可以從 Text以及其內(nèi)部格式的數(shù)據(jù)導(dǎo)入到DBGridEh的數(shù)據(jù)集中。它可以從文件中讀取數(shù)據(jù)或讀取數(shù)據(jù)到流(TStream對象)。 
 
其它特性 
lookup 編輯器,你可以在運行時清空(設(shè)置為Null LookupKeyField 值。比如選擇整個文本然后按Delete鍵。 
 
冷區(qū) 
冷區(qū)是數(shù)據(jù)網(wǎng)格列集左邊顯示的不可滾動的區(qū)域。與固定列不同的是,冷區(qū)的列可以獲得編輯焦點??梢酝ㄟ^設(shè)置FrozenCols屬性來設(shè)置右邊不可滾動的列集。 
 
增量搜索 
 
TDBGridEh 允許用戶在網(wǎng)格列中實現(xiàn)特定的增量搜索。當(dāng)用戶進入增量搜索時他可以顯示字符以及網(wǎng)格,并且在當(dāng)前的列中查找文本。使用 dghIncSearch  dghPreferIncSearch的值(在OptionsEh 選項中) 在數(shù)據(jù)網(wǎng)格中操作增量搜索。 dghIncSearch 值允許在數(shù)據(jù)網(wǎng)格中進行增量搜索。運行時你能夠使用下面的鍵進行增量搜索:
 
Ctrl+F - 開始增量搜索。
Ctrl+Enter - 查找下一個匹配記錄。
Ctrl+Shift+Enter - 查找前一個匹配記錄。
 
如果OptionsEh選項中的 dghIncSearch 是只讀的,那么網(wǎng)絡(luò)將自動設(shè)置增量模式在第一次按鍵以及1.5秒后返回普通模式。 dghPreferIncSearch 值決定網(wǎng)格設(shè)置自動增量搜索模式在第一次按鍵時替代單元編輯。
 
水平或垂直滾動條 
使用 VertSctollbar, HorzSctollbar 屬性來顯示/隱藏以及跟蹤水平或垂直滾動條。 
 
多選 
TDBGridEh 允許在選定的區(qū)域上進行選擇記錄,列以及矩形區(qū)域等操作: 
 
允許多選會影響下面這些屬性: 
 
Options選項中的dgMultiSelect屬性 - 設(shè)置是否允許多選。 
 
Options選項中的dghClearSelection 屬性- 設(shè)置在用戶移到下一個單元時是否清除已選記錄。 
 
Options選項中的EditActions屬性 -設(shè)置用戶可以在已選記錄上執(zhí)行哪些操作(比如,拷貝,剪切,刪除,粘貼,全選等)。 
 
Options選項中的AllowedSelections屬性-設(shè)置允許選定記錄的類型(比如,行,列,矩形區(qū)域等)。 
 
Options選項中的Selection屬性-設(shè)置一個當(dāng)前的多選狀態(tài),已選記錄,列或矩形區(qū)域以及存取它們的屬性和函數(shù)。 
 
從注冊表或ini文件中保存或恢復(fù)網(wǎng)格和列的層次。 
TDBGridEh 有一個常規(guī)設(shè)置來從注冊表或ini文件中保存和恢復(fù)網(wǎng)絡(luò)以及列的層次: 
 
RestoreColumnsLayout - 從注冊表中恢復(fù)列的次序,寬度,排序標志。 
 
RestoreColumnsLayoutIni - ini文件中恢復(fù)列的次序,寬度,排序標志。 
 
RestoreGridLayout - 從注冊表中恢復(fù)列的次序,寬度,可視,排序標志,排序索引或行高。 
 
RestoreGridLayoutIni - ini文件中恢復(fù)列的次序,寬度,可視,排序標志,排序索引或行高。 
 
SaveColumnsLayout - 保存列的次序,寬度,排序標志到注冊表中。 
 
SaveColumnsLayoutIni - 保存列的次序,寬度,排序標志到ini文件中。 
 
SaveGridLayout - 保存列的次序,寬度,可視,排序標志,排序索引或行高到注冊表中。 
 
SaveGridLayoutIni - 保存列的次序,寬度,可視,排序標志,排序索引或行高到ini文件中。 
 
當(dāng)前版本的TDBGridEh不支持的特性: 
這個版本的TDBGridEh不支持下面的特性: 
 
TDBGridEh 不能設(shè)置每一個數(shù)據(jù)窗口中單獨行的行高。 
 
TDBGridEh 不能象TreeView那樣工作。它不能擁有節(jié)點和枝葉。 
 
TDBGridEh 不能橫向或縱向合并數(shù)據(jù)單元。 
 
將已存在的TDBGrid組件轉(zhuǎn)換為TDBGridEh組件: 
 
盡管TDBGridEh并不是從TCustomDBGrid組件繼承而來的,但是在TDBGridEhTDBGrid中還是有一些相似的屬性。 
 
它允許僅用一點點代價來轉(zhuǎn)換已存在的TDBGrid組件到TDBGridEh。 
 
可以按照下面的提示來轉(zhuǎn)轉(zhuǎn)換已存在的TDBGrid組件到TDBGridEh 
 
DelphiIDE中打開包含有TDBGrid組件的應(yīng)用程序。 
 
設(shè)置視圖方式為文本方式(Alt-F12)。 
 
將所有的TDBGrid對象改名為TDBGridEh'比如,將object DBGrid1: TDBGrid'改為 'object DBGrid1: TDBGridEh')
 
重新編譯你的應(yīng)用程序。 
 
 
 
5 使用 TDBSumList 組件
TDBSumList說明 
你可以使用TDBSumList在可視動態(tài)變化數(shù)據(jù)集中進行記錄統(tǒng)計。在你想查看的數(shù)據(jù)集中設(shè)置相關(guān)的數(shù)據(jù)字段,然后寫 SumListChanged 事件來指定在TDBSumList發(fā)生改變后所要做的操作。TDBSumList  SumCollection 屬性上一個 TDBSum 對象容器。每個 TDBSum 對象是一個可以指定集合值的元件。 FieldName  GroupOperation 決定了集合值的類型,SumValue 控制當(dāng)前的集合值。
 
TDBSumList 被埋藏于 DBGridEh 組件中,因此所的下面有關(guān)TDBGridEh.SumList 的說明與TDBGirdEhTDBGridEh.SumList 屬性的說明是一樣的。
 
如何工作以及為什么有時SumList的集合值計算不正確?
你知道 data-aware 控件與數(shù)據(jù)集是通過 TDataLink 對象相連接的。 TDataLink 不允許快速重新計算集合值。例如,當(dāng)從數(shù)據(jù)集中刪除記錄時,數(shù)據(jù)集發(fā)送deDataSetChange事件給所有的TDataLink對象,在當(dāng)前局部過濾發(fā)生改變時,同樣的事件也被發(fā)送了。因此當(dāng) TDataLink 接收到該事件時,它不得不在所有的數(shù)據(jù)集中重新計算集合值,甚至于僅從數(shù)據(jù)集中刪除一條記錄。在激活后, TDBSumList 重載了數(shù)據(jù)集的下列事件: OnAfterEdit, OnAfterInsert, OnAfterOpen, OnAfterPost, OnAfterScroll, OnBeforeDelete, OnAfterClose。這種方法避免了在所有的數(shù)據(jù)集中它。在不需要它時,又會出現(xiàn)其它的問題,比如: T
運行時指定這些事件。在指定上述事件之一以前,關(guān)閉 SumList。
在下面的情形下,SumList 發(fā)出存取錯誤信息。 SumList 試著向數(shù)據(jù)集返回事件,但數(shù)據(jù)集數(shù)據(jù)并未被刪除。在SumList(或網(wǎng)格)以及數(shù)據(jù)集放置在不同的窗體(數(shù)據(jù)模塊)這種情況下,可以顯示數(shù)據(jù)。在這種情況下,試著在從數(shù)據(jù)集或數(shù)據(jù)模塊中數(shù)據(jù)集數(shù)據(jù)被刪除時關(guān)閉SumList。
如果你使用SetRangeApplyRange事件,SumList 將不能跟蹤數(shù)據(jù)集中發(fā)生的變化。但可以調(diào)用 SumList.RecalAll 方法。
在非BDE數(shù)據(jù)集的主/明細表中,SumList 不能跟蹤數(shù)據(jù)變化。在主數(shù)據(jù)集激活狀態(tài)數(shù)據(jù)集發(fā)生變化后,調(diào)用 SumList.RecalAll 方法。
在任何其它情況下,如果你發(fā)現(xiàn)了其它SumList的計算值不正確,你都可以調(diào)用 RecalAll方法。 
 
 
4 使用 TPrintDBGridEh 組件 
 Delphi  IDE 組件面板中選擇 TPrintDBGridEh 并拖放其到 form 上。在 其 DBGridEh 屬性中設(shè)置為相應(yīng)的 TDBGridEh 組件。使用 Print 以及 Preview 方法在打印機上打印網(wǎng)格或者在預(yù)覽窗體中預(yù)覽網(wǎng)格。 
網(wǎng)格前以網(wǎng)格后的 RTF 文本 
TPrintDBGridEh 允許在打印 / 打印預(yù)覽網(wǎng)格前或網(wǎng)格后的 RTF 文本。使用 AfterGridText 以及 BeforeGridText 屬性來指定文本。 你可以在打印 / 打印預(yù)覽時通過 BeforeGridText  AfterGridText 屬性來替換文本內(nèi)容。 
當(dāng)前版本的 TPrintDBGridEh 不支持的特性。 
這個版本不支持下面的特性: 
TPrintDBGridEh 不能在一頁中打印 / 打印預(yù)覽多個網(wǎng)格。 
問答列表 : 
 : 怎樣進行橫向打印 / 打印預(yù)覽? 
 : TPrintDBGridEh 并沒有專六的屬性來設(shè)置頁面特性。在調(diào)用打印或打印預(yù)覽方法前,你必須設(shè)置你將要執(zhí)行打印的打印源( Orientation )。 
uses ......, PrViewEh, Printers. 
.............. 
procedure TForm1.bPreviewClick(Sender: TObject); 
begin 
PrinterPreview.Orientation := poLandscape; 
PrintDBGridEh1.Preview; 
end ;
 
 
DBGrid介紹:
 Delphi 語言的數(shù)據(jù)庫編程中,DBGrid 是顯示數(shù)據(jù)的主要手段之一。但是 DBGrid 缺省的外觀未免顯得單調(diào)和缺乏創(chuàng)意。其實,我們完全可以在我們的程序中通過編程來達到美化DBGrid 外觀的目的。通過編程,我們可以改變 DBGrid 的表頭、網(wǎng)格、網(wǎng)格線的前景色和背景色,以及相關(guān)的字體的大小和風(fēng)格。
   以下的示例程序演示了對 DBGrid 各屬性的設(shè)置,使 Delphi 顯示的表格就像網(wǎng)頁中的表格一樣漂亮美觀。
   示例程序的運行:
    Form1 上放置 DBGrid1Query1、DataSource1 三個數(shù)據(jù)庫組件,設(shè)置相關(guān)的屬性,使 DBGrid1 能顯示表中的數(shù)據(jù)。然后,在 DBGrid1  onDrawColumnCell 事件中鍵入以下代碼,然后運行程序,就可以看到神奇的結(jié)果了。本代碼在 Windows98、Delphi5.0 環(huán)境下調(diào)試通過。
procedure TMainForm.DBGrid1DrawColumnCell(Sender: TObject;
 const Rect: TRect; DataCol: Integer; Column: TColumn;State: TGridDrawState);
var i :integer;
begin
 if gdSelected in State then Exit;
//定義表頭的字體和背景顏色:
   for i :=0 to (Sender as TDBGrid).Columns.Count-1 do
   begin
     (Sender as TDBGrid).Columns[i].Title.Font.Name :='宋體'; //字體
     (Sender as TDBGrid).Columns[i].Title.Font.Size :=9; //字體大小
     (Sender as TDBGrid).Columns[i].Title.Font.Color :=$000000ff; //字體顏色(紅色)
     (Sender as TDBGrid).Columns[i].Title.Color :=$0000ff00; //背景色(綠色)
   end;
//隔行改變網(wǎng)格背景色:
 if Query1.RecNo mod 2 = 0 then
   (Sender as TDBGrid).Canvas.Brush.Color := clInfoBk //定義背景顏色
 else
   (Sender as TDBGrid).Canvas.Brush.Color := RGB(191, 255, 223); //定義背景顏色
//定義網(wǎng)格線的顏色:
   DBGrid1.DefaultDrawColumnCell(Rect,DataCol,Column,State);
 with (Sender as TDBGrid).Canvas do // cell 的邊框
 begin
   Pen.Color := $00ff0000; //定義畫筆顏色(藍色)
   MoveTo(Rect.Left, Rect.Bottom); //畫筆定位
   LineTo(Rect.Right, Rect.Bottom); //畫藍色的橫線
   Pen.Color := $0000ff00; //定義畫筆顏色(綠色)
   MoveTo(Rect.Right, Rect.Top); //畫筆定位
   LineTo(Rect.Right, Rect.Bottom); //畫綠色的豎線
 end;
end; 
 
 問題: Delphi5 - 隔行改變DBGrid網(wǎng)格顏色    Form1 上放置 DBGrid1Query1、DataSource1 三個數(shù)據(jù)庫組件,設(shè)置相關(guān)的屬性,使 DBGrid1 能顯示表中的數(shù)據(jù)。然后,在 DBGrid1  onDrawColumnCell 事件中鍵入以下代碼,然后運行程序
 
代碼:
procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
DataCol: Integer; Column: TColumn; State: TGridDrawState);
 
var i:integer;
begin
 if gdSelected in State then Exit;  //隔行改變網(wǎng)格背景色: 
   if adoQuery1.RecNo mod 2 = 0 then
     (Sender as TDBGrid).Canvas.Brush.Color := clinfobk //定義背景顏色
 else
   (Sender as TDBGrid).Canvas.Brush.Color := RGB(191, 255, 223);  //定義背景顏色
 
 //定義網(wǎng)格線的顏色:
 DBGrid1.DefaultDrawColumnCell(Rect,DataCol,Column,State);
 with (Sender as TDBGrid).Canvas do // cell 的邊框
 begin
   Pen.Color := $00ff0000; //定義畫筆顏色(藍色)
   MoveTo(Rect.Left, Rect.Bottom); //畫筆定位
   LineTo(Rect.Right, Rect.Bottom); //畫藍色的橫線
   Pen.Color := clbtnface; //定義畫筆顏色(蘭色)
   MoveTo(Rect.Right, Rect.Top); //畫筆定位
   LineTo(Rect.Right, Rect.Bottom); //畫綠色
 end;
end;
 
BDE中的table1未能通過,顏色沒有隔行變化。  
 
 
 2003-11-11 17:12:09    DelphiDBGrid中插入其他可視組件   Delphi提供了功能強大的 DBGrid組件,以方便進行數(shù)據(jù)庫應(yīng)用程序設(shè)計。但是如果我們僅僅利用DBGrid組件,每一個獲得焦點(Grid)只是一個簡單的文本編輯框,不方便用戶輸入數(shù)據(jù)。Delphi也提供了一些其他數(shù)據(jù)組件來方便用戶輸入,比如DBComboBox,DBCheckBox等組件,但這些組件卻沒有DBGrid功能強大。Delphi能不能象Visual Foxpro那樣讓DBGrid中獲得焦點網(wǎng)格可以是其它可視數(shù)據(jù)組件以方便用戶呢?其實我們可以通過在DBGrid中插入其他可視組件來實現(xiàn)這一點。
 
   DelphiDBGrid處理的內(nèi)部機制,就是在網(wǎng)格上浮動一個組件——DBEdit組件。你輸入數(shù)據(jù)的網(wǎng)格其實是浮動DBEdit組件,其他未獲得焦點地方不過是圖像罷了。所以,在DBGrid中插入其他可視組件就是在網(wǎng)格上浮動一個可視組件。因此任何組件,包括從簡單的DbCheckBox到復(fù)雜的對話框,都可以在DBGrid中插入。下面就是一個如何在DBGrid中插入DBComboBox組件的步驟,采用同樣的辦法可以插入其他組件。
 
 1、在Delphi 4.0中新建一個項目。
 
 2、分別拖動的Data Access組件板上DataSource、Table,Data Controls組件板上DBGridDBComboBox四個組件到Form1上。
 
 3、設(shè)置各個組件的屬性如下:
 
rcf1對象 屬性 設(shè)定植
Form1 Caption 'DBGrid中插入SpinEdit組件示例'
DataSource1 DataSet Table1
Table1 DatabaseName DBDEMOS
TableName 'teacher.DBF'
Active True
DBGrid1 DataSource DataSource1
DBComboBox1 DataField SEX
DataSource DataSource1
Visible False
Strings Items. ''| ''
 
注意:我在這里用了Teacher.dbf,那是反映教職工的性別,只能是或者是。
 
 4DrawDataCell事件是繪制單元格,當(dāng)獲得焦點網(wǎng)格所對應(yīng)的字段與組合框所對應(yīng)的字段一致時,移動組合框到獲得焦點的網(wǎng)格上,并且使組合框可視,從而達到在DBGrid指定列上顯示DBComboBox的功能。設(shè)置DBGrid1OnDrawDataCell事件如下:
 
procedure TForm1.DBGrid1DrawDataCell(Sender: TObject; const Rect: TRect; Field: TField; State: TGridDrawState);
begin
 if (gdFocused in State) then
 begin
   if (Field.FieldName = DBComboBox1.DataField ) then
   begin
     DBComboBox1.Left := Rect.Left + DBGrid1.Left;
     DBComboBox1.Top := Rect.Top + DBGrid1.top;
     DBComboBox1.Width := Rect.Right - Rect.Left;
     DBComboBox1.Height := Rect.Bottom - Rect.Top;
     DBComboBox1.Visible := True;
   end;
 end;
end;
 
 5、DBGrid指定單元格未獲得焦點時不顯示DBComboBox,設(shè)置DBGrid1OnColExit事件如下:
procedure TForm1.DBGrid1ColExit(Sender: TObject);
begin
 If DBGrid1.SelectedField.FieldName = DBComboBox1.DataField then
   begin
     DBComboBox1.Visible := false;
   end;
end;
 
 6、當(dāng)DBGrid指定列獲得焦點時DrawDataCell事件只是繪制單元格,并顯示DBComboBox,但是DBComboBox并沒有獲得焦點,數(shù)據(jù)的輸入還是在單元格上進行。在DBGrid1KeyPress事件中調(diào)用SendMessage這個 Windows API函數(shù)將數(shù)據(jù)輸入傳輸?shù)?span lang="EN-US">DBComboBox上,從而達到在DBComboBox上進行數(shù)據(jù)輸入。因此還要設(shè)置KeyPress事件如下:
 
procedure TForm1.DBGrid1KeyPress(Sender: TObject; var Key: Char);
begin
 if (key < > chr(9)) then
 begin
   if (DBGrid1.SelectedField.FieldName =DBComboBox1.DataField) then
   begin
     DBComboBox1.SetFocus;
     SendMessage(DBComboBox1.HandleWM_Char,word(Key)0);
   end;
 end;
end;
 
   程序在中文Windows 98,Delphi 4.015 下調(diào)試通過。希望本文能使你可以更加方便快捷的開發(fā)數(shù)據(jù)庫應(yīng)用程序。  
 
 
 2003-11-11 17:17:56    鎖定DBGrid左邊的列    我在使用 Delphi3 進行數(shù)據(jù)庫編程的時候,希望 DBGRID 構(gòu)件在顯示數(shù)據(jù)的時候能象FoxPro  BROWSE 命令一樣,鎖定左邊指定的幾列不進行滾動,請問應(yīng)用什么方法來實現(xiàn)?
 
   我們知道 Delphi  TStringGrid 有一個屬性 FixedCols 來指定不滾動的列。雖然TDBGrid 不能直接使用這一屬性,但通過強制類型轉(zhuǎn)換也可以首先這一功能,因為這兩個類都來自 TCustomGrid 類。下面我們以 Delphi 3.0 Demos\Db\CtrlGrid 為例來說明具體的用法。在這個例子的 TFmCtrlGrid.FormShow 過程中加入如下一行: 
 
   TStringGrid(DbGrid1).FixedCols := 2; 
 
   運行該程序,在左右移動各列時,Symbol 列不會移動。除了這種方法,也可以采用下面的方法:首先在 Form 聲明部分加上
 
   type TMyGrid = Class(TDBGrid) end; 
 
   然后在 TFmCtrlGrid.FormShow 過程中加入: 
 
   TMyGrid(DbGrid1).FixedCols := 2; 
 
   兩者從形式上略有不同,但實質(zhì)都是一樣的。我們這里設(shè)置 FixedCols  2,這是因為在 DBGrid 構(gòu)件最左側(cè)有個指示列,如果你將 DBGrid  Options 屬性的 dgIndicator 設(shè)為False,則應(yīng)設(shè)置 FixedCols 1。  
 
 
 2003-11-11 17:21:36    使dbgrid的某幾筆資料變色   你可在 DBGrid 元件的 DrawDataCell 事件中依資料的條件性來改變格子或文字的顏色.
 :
 
OnDrawDataCell(...)
begin
 with TDBGrid(Sender) do
 begin
   if (條件) then
     Canvas.TextOut(Rect.Left + 4
   Rect.Top + 2
 
'要顯示的文字如表格的資料');
end;
 
   而你會看到 DBGrid 的顯示資料怎麼有重疊的情況那是因為原本DBGrid要顯示的資料與 TextOut 所顯示的資料重疊
   解決方法 :
    Query 元件所加入的欄位(在元件上按右鍵會有 Add Fields...的選單)在不要顯示資料的欄位的 OnGetText 事件中有一參數(shù)設(shè)定為 False;
 
procedure TForm1.Query1Detail1GetText(Sender: TField; var Text: string;
DisplayText: Boolean);
begin
 // 決定在 DBGrid 得知表格資料時要不要顯示所得到的資料False -> 不顯示
 // 就可避免與 TextOut 的文字重疊了
 DisplayText : = False;
end;
end;
 
   如果用 Delphi 3 處理很簡單.例如:對表中某字段當(dāng)其數(shù)值小于0時為紅字其他為黑字.
 DBGrid.OnDrawColumnCell(...) :
 
begin
 if TableField.AsInteger < 0 then
   DBGrid.Canvas.Font.Color := clRed
 else
   DBGrid.Canvas.Font.Color := clBlack;
 DBGrid.DefaultDrawColumnCell(...);
end;
 
這樣對 Field 指定的格式仍舊生效不必重寫.  
 
 
 2003-11-11 17:25:29    實戰(zhàn)Delphi數(shù)據(jù)網(wǎng)格色彩特效   Delphi中的數(shù)據(jù)網(wǎng)格控件(TDbGrid)對于顯示和編輯數(shù)據(jù)庫中大量的數(shù)據(jù)起著十分重要的作用;然而,在使用數(shù)據(jù)網(wǎng)格控件的同時,也往往因為表格中大量的數(shù)據(jù)不易區(qū)分,而令操作者眼花繚亂。如何提高網(wǎng)格控件的易用性,克服它的此項不足呢?本文從改變數(shù)據(jù)網(wǎng)格的色彩配置角度,提出了一種解決辦法。
 
   以下為數(shù)據(jù)網(wǎng)格控件的6種特殊效果的實現(xiàn)方法,至于數(shù)據(jù)網(wǎng)格控件與數(shù)據(jù)集如何連接的方法從略。
 
1. 縱向斑馬線效果:實現(xiàn)網(wǎng)格的奇數(shù)列和偶數(shù)列分別以不同的顏色顯示以區(qū)別相鄰的數(shù)據(jù)列。
file://DbGridDrawColumnCell事件中編寫如下代碼:
 
Case DataCol Mod 2 = 0 of
True: DbGrid1.Canvas.Brush.Color:= clBlue; file://偶數(shù)列用藍色
False: DbGrid1.Canvas.Brush.Color:= clAqua; file://奇數(shù)列用淺綠色
End;
DbGrid1.Canvas.Pen.Mode:=pmMask;
DbGrid1.DefaultDrawColumnCell (Rect
DataCol
Column
State);
 
2. 縱向斑馬線,同時以紅色突出顯示當(dāng)前單元格效果:以突出顯示當(dāng)前選中的字段。
 
file://將上述代碼修改為:
Case DataCol Mod 2 = 0 of
True: DbGrid1.Canvas.Brush.Color:= clBlue; file://偶數(shù)列用藍色
False: DbGrid1.Canvas.Brush.Color:= clAqua; file://奇數(shù)列用淺綠色
End;
If ((State = [gdSelected]) or (State=[gdSelectedgdFocused])) then
If Not DbGrid1.SelectedRows.CurrentRowSelected then
DbGrid1.Canvas.Brush.Color:=clRed; file://當(dāng)前選中單元格顯示紅色
DbGrid1.Canvas.Pen.Mode:=pmMask;
DbGrid1.DefaultDrawColumnCell (Rect 
DataCol 
Column 
State);
 
上述兩種方法突出了列的顯示效果。
 
3.在數(shù)據(jù)網(wǎng)格中以紅色突出顯示當(dāng)前選中的行。
   設(shè)置DbGrid控件的Options屬性中的dgRowSelect屬性為真,Color屬性為clAqua(背景色)
DbGridDrawColumnCell事件中編寫如下代碼:
 
if ((State = [gdSelected]) or (State=[gdSelected gdFocused])) then
DbGrid1.Canvas.Brush.color:=clRed; file://當(dāng)前行以紅色顯示,其它行使用背景的淺綠色
DbGrid1.Canvas.pen.mode:=pmmask;
DbGrid1.DefaultDrawColumnCell (Rect
DataCol
Column
State);
 
4.行突顯的斑馬線效果:既突出當(dāng)前行,又區(qū)分不同的列(字段)。
 
file://其它屬性設(shè)置同3,將上述代碼修改為:
if ((State = [gdSelected]) or (State=[gdSelectedgdFocused])) then
begin
Case DataCol Mod 2 = 0 of
True : DbGrid1.Canvas.Brush.color:=clRed; file://當(dāng)前選中行的偶數(shù)列顯示紅色
False: DbGrid1.Canvas.Brush.color:=clblue; file://當(dāng)前選中行的奇數(shù)列顯示藍色
end;
DbGrid1.Canvas.pen.mode:=pmmask;
DbGrid1.DefaultDrawColumnCell (Rect
DataCol
Column
State);
end;
 
5.橫向斑馬線, 同時以紅色突顯當(dāng)前行效果。
 
file://其它屬性設(shè)置同3,將上述代碼修改為:
Case Table1.RecNo mod 2 = 0 of file://根據(jù)數(shù)據(jù)集的記錄號進行判斷
True : DbGrid1.Canvas.Brush.color:=clAqua; file://偶數(shù)行用淺綠色顯示
False: DbGrid1.Canvas.Brush.color:=clblue; file://奇數(shù)行用藍色表示
end;
if ((State = [gdSelected]) or (State=[gdSelectedgdFocused])) then file://選中行用紅色顯示
DbGrid1.Canvas.Brush.color:=clRed;
DbGrid1.Canvas.pen.mode:=pmMask;
DbGrid1.DefaultDrawColumnCell (Rect
DataCol
Column
State);
 
6.雙向斑馬線效果:即行間用不同色區(qū)分,同時,選中行以縱向斑馬線效果區(qū)分不同的列。
 
file://其它屬性設(shè)置同3,將上述代碼修改為:
Case Table1.RecNo mod 2 = 0 of file://根據(jù)數(shù)據(jù)集的記錄號進行判斷
True : DbGrid1.Canvas.Brush.color:=clAqua; file://偶數(shù)行用淺綠色顯示
False: DbGrid1.Canvas.Brush.color:= clblue; file://奇數(shù)行用藍色表示
end;
If ((State = [gdSelected]) or (State=[gdSelectedgdFocused])) then
Case DataCol mod 2 = 0 of
True : DbGrid1.Canvas.Brush.color:=clRed; file://當(dāng)前選中行的偶數(shù)列用紅色
False: DbGrid1.Canvas.Brush.color:= clGreen; file://當(dāng)前選中行的奇數(shù)列用綠色表示
end;
DbGrid1.Canvas.pen.mode:=pmMask;
DbGrid1.DefaultDrawColumnCell (Rect
DataCol
Column
State);
 
   上述6種方法分別就數(shù)據(jù)網(wǎng)格控件的列和行的色彩進行了設(shè)置,讀者可以根據(jù)自己的需要設(shè)置特效。該程序在Delphi5中測試通過。  
 
 
 2003-11-13 11:11:31    點擊DBGridTitle對查詢結(jié)果排序 關(guān)鍵詞:DBGrid 排序  
 
   欲實現(xiàn)點擊DBGridTitle對查詢結(jié)果排序,想作一個通用程序,不是一事一議,例如不能在SQL語句中增加Order by ...,因為SQL可能原來已經(jīng)包含Order by ...,而且點擊另一個Title時又要另外排序,目的是想作到象資源管理器那樣隨心所欲。
 
procedure TFHkdata.SortQuery(Column:TColumn);
var
 SqlStr,myFieldName,TempStr: string;
 OrderPos: integer;
 SavedParams: TParams;
begin
 if not (Column.Field.FieldKind in [fkData,fkLookup]) then exit;
 if Column.Field.FieldKind =fkData then
   myFieldName := UpperCase(Column.Field.FieldName)
 else
   myFieldName := UpperCase(Column.Field.KeyFields);
 while Pos(myFieldName,';')<>0 do
 myFieldName := copy(myFieldName,1,Pos(myFieldName,';')-1)+ ',' + copy(myFieldName,Pos(myFieldName,';')+1,100);
 with TQuery(TDBGrid(Column.Grid).DataSource.DataSet) do
 begin
   SqlStr := UpperCase(Sql.Text);
   // if pos(myFieldName,SqlStr)=0 then exit;
   if ParamCount>0 then
   begin
     SavedParams := TParams.Create;
     SavedParams.Assign(Params);
   end;
   OrderPos := pos('ORDER',SqlStr);
   if (OrderPos=0) or (pos(myFieldName,copy(SqlStr,OrderPos,100))=0) then
     TempStr := ' Order By ' + myFieldName + ' Asc'
   else if pos('ASC',SqlStr)=0 then
     TempStr := ' Order By ' + myFieldName + ' Asc'
   else
     TempStr := ' Order By ' + myFieldName + ' Desc';
   if OrderPos<>0 then SqlStr := Copy(SqlStr,1,OrderPos-1);
   SqlStr := SqlStr + TempStr;
   Active := False;
   Sql.Clear;
   Sql.Text := SqlStr;
   if ParamCount>0 then
   begin
     Params.AssignValues(SavedParams);
     SavedParams.Free;
   end;
   Prepare;
   Open;
 end;
end;
 
 
 
 2003-11-13 11:13:57    去掉DbGrid的自動添加功能 
關(guān)鍵詞:DbGrid  
 
   移動到最后一條記錄時再按一下就會追加一條記錄,如果去掉這項功能 
   procedure TForm1.DataSource1Change(Sender: TObject; Field: TField);
   begin
     if TDataSource(Sender).DataSet.Eof then TDataSource(Sender).DataSet.Cancel;
   end;
 
 
 
 
 
 2003-11-16 12:05:46    DBGrid不支持鼠標的上下移動的解決代碼(感謝 wangxian11 提供)自己捕捉WM_MOUSEWHEEL消息處理
private
 OldGridWnd : TWndMethod;
procedure NewGridWnd (var Message : TMessage);
public
 
procedure TForm1.NewGridWnd(var Message: TMessage);
var
 IsNeg : Boolean;
begin
 if Message.Msg = WM_MOUSEWHEEL then
 begin
   IsNeg := Short(Message.WParamHi) < 0;
   if IsNeg then
     DBGrid1.DataSource.DataSet.MoveBy(1)
   else
     DBGrid1.DataSource.DataSet.MoveBy(-1)
 end
 else
   OldGridWnd(Message);
end;
 
procedure TForm1.FormCreate(Sender: TObject);
begin
 OldGridWnd := DBGrid1.WindowProc ;
 DBGrid1.WindowProc := NewGridWnd;
end;      
 
 
 2003-11-17 14:46:56    dbgrid中移動焦點到指定的行和列   dbgrid是從TCustomGrid繼承下來的,它有colrow屬性,只不過是protected的,不能直接訪問,要處理一下,可以這樣:
 
   TDrawGrid(dbgrid1).row:=row;
   TDrawGrid(dbgrid1).col:=col;
   dbgrid1.setfocus;
就可以看到效果了。
 
   1 這個方法是絕對有問題的,它會引起DBGrid內(nèi)部的混亂,因為DBGrid無法定位當(dāng)前紀錄,如果DBGrid只讀也就罷了(只讀還是會出向一些問題,比如原本只能單選的紀錄現(xiàn)在可以出現(xiàn)多選等等,你可以自己去試試),如果DBGrid可編輯那問題就可大了,因為當(dāng)前紀錄的關(guān)系,你更改的數(shù)據(jù)字段很可能不是你想象中的
   2 我常用的解決辦法是將上程序改為(隨便設(shè)置col是安全的,沒有一點問題)
 
   Query1.first;
   TDrawGrid(dbgrid1).col:=1;
   dbgrid1.setfocus;
 
   這就讓焦點移到第一行第一列當(dāng)中 
 
 
 2003-11-17 14:55:26    如何使DBGRID網(wǎng)格的顏色隨此格中的數(shù)據(jù)值的變化而變化?   在做界面的時候,有時候為了突出顯示數(shù)據(jù)的各個特性(如過大或者過小等),需要通過改變字體或者顏色,本文就是針對這個情況進行的說明。
 
   如何使DBGRID網(wǎng)格的顏色隨此格中的數(shù)據(jù)值的變化而變化。如<60的網(wǎng)格為紅色?
   Delphi中數(shù)據(jù)控制構(gòu)件DBGrid是用來反映數(shù)據(jù)表的最重要、也是最常用的構(gòu)件。在應(yīng)用程序中,如果以彩色的方式來顯示DBGrid,將會增加其可視性,尤其在顯示一些重要的或者是需要警示的數(shù)據(jù)時,可以改變這些數(shù)據(jù)所在的行或列的前景和背景的顏色。
  DBGrid屬性DefaultDrawing是用來控制Cell(網(wǎng)格)的繪制。若DefaultDrawing的缺省設(shè)置為True,意思是Delphi使用DBGrid的缺省繪制方法來制作網(wǎng)格和其中所包含的數(shù)據(jù),數(shù)據(jù)是按與特定列相連接的Tfield構(gòu)件的DisplayFormatEditFormat特性來繪制的;若將DBGridDefaultDrawing特性設(shè)置成False,Delphi就不繪制網(wǎng)格或其內(nèi)容,必須自行在TDBGridOnDrawDataCell事件中提供自己的繪制例程(自畫功能)。
  在這里將用到DBGrid的一個重要屬性:畫布Canvas,很多構(gòu)件都有這一屬性。Canvas代表了當(dāng)前被顯示DBGrid的表面,你如果把另行定義的顯示內(nèi)容和風(fēng)格指定給DBGrid對象的CanvasDBGrid對象會把Canvas屬性值在屏幕上顯示出來。具體應(yīng)用時,涉及到CanvasBrush屬性和FillRect方法及TextOut方法。Brush屬性規(guī)定了DBGrid.Canvas顯示的圖像、顏色、風(fēng)格以及訪問Windows GDI 對象句柄,FillRect方法使用當(dāng)前Brush屬性填充矩形區(qū)域,方法TextOut輸出Canvas的文本內(nèi)容。
 
  以下用一個例子來詳細地說明如何顯示彩色皼/div>

 

本站僅提供存儲服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊舉報。
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
DBGridEh使用技巧
DbgirdEh的一些用法
DbgridEh的一些用法(三)
用DBGridEh實現(xiàn)點擊列頭自動排序數(shù)據(jù)
dbgrideh的drawroll
ClientDataSet的隱含功能-計算機二級考試-考試大
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長圖 關(guān)注 下載文章
綁定賬號成功
后續(xù)可登錄賬號暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點擊這里聯(lián)系客服!

聯(lián)系客服