乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      轉(zhuǎn)載DBGrid和DBGridEH

       木子示羊 2010-08-18

      轉(zhuǎn)載DBGridDBGridEH

      二、應(yīng)用實(shí)例
          Enlib3.0組件包安裝成功后
      A、定制標(biāo)題行
       1、制作復(fù)雜標(biāo)題行
          標(biāo)題行可設(shè)為2行以上高度,并可以為多列創(chuàng)建一個(gè)共同的父標(biāo)題行。為實(shí)現(xiàn)這個(gè)效果,需在各個(gè)列標(biāo)題屬性中以分隔父標(biāo)題和子標(biāo)題,如辦公用品包括代碼和名稱兩部分,具體屬性設(shè)置如下:
       
      usemultititile=true;
      titlelines=2
      DBGridEh.Columns[0].Title.Caption := '辦公用品|代碼';
      DBGridEh.Columns[1].Title.Caption := '辦公用品|名稱';
       
       2、標(biāo)題行顯示圖片 
          實(shí)現(xiàn)圖2中的購(gòu)買人標(biāo)題行顯示效果。首先添加一個(gè)imagelist組件img1并在其中添加一組bmp,ico格式的圖片。然后將DBGridEhTitleImages設(shè)置為img1.最后在需要顯示圖片的列標(biāo)題的imageindex中設(shè)置需要顯示的img1中圖片的序號(hào)。按F9執(zhí)行一下程序,是不是很酷!
       
       3、自動(dòng)顯示標(biāo)題行的升降排序標(biāo)志符(降序升序)并做相應(yīng)排序
          DBGridEh組件可以在標(biāo)題行單元格中顯示小三角形升、降排序標(biāo)志符圖片,在運(yùn)行時(shí)可點(diǎn)擊標(biāo)題行,圖片自動(dòng)切換并做相應(yīng)排序。具體屬性設(shè)置如下:
       
      OptionsEhdghAutoSortMarking 
      Column.Title.TitleButtontrue 
       
      SortMarkedColumns 為當(dāng)前排序列可在運(yùn)行時(shí)使用.
      然后在該列的ontitleclick事件中添加代碼:
      procedure TForm_Query.DBGridEh1TitleBtnClick(Sender: TObject; ACol: Integer; Column: TColumnEh);
      var
        sortstring:string; //排序列
      begin
        //進(jìn)行排序
        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;
        //進(jìn)行排序
          try
            dataset.Sort := sortstring //dataset為實(shí)際數(shù)據(jù)集變量名
          except
          end;
        end;
      end;
       
      切記lookup型字段不可做上述設(shè)置,否則系統(tǒng)會(huì)提示錯(cuò)誤。
       
        
       
      B、定制表格底部(footer)區(qū)域的匯總統(tǒng)計(jì)行
          DBGridEh 組件可以在表格底部顯示匯總行,如記錄數(shù)合計(jì)、列字段累加和等信息。在FooterRowCount中設(shè)置底部顯示的行數(shù);然后在Footers 編輯器中添加一個(gè)或多個(gè)顯示列,顯示列可以是字段值累加和、記錄數(shù)合計(jì)、字段值或靜態(tài)文件等集合類型,可以在設(shè)計(jì)時(shí)在ValueType屬性中設(shè)置,也可在運(yùn)行時(shí)通過(guò)設(shè)置Footers[i].ValueType指定其類型。其含義見(jiàn)下表:
       
          切記設(shè)置DBGridEh.SumList.Active  True,才會(huì)進(jìn)行匯總統(tǒng)計(jì)運(yùn)算。需注意的是,如顯示類型為不是當(dāng)前列的累加和,則需在fieldname屬性中指定匯總列,其它類型則無(wú)此要求。 
       
       
       
      C、定制表格數(shù)據(jù)單元外觀
       1、根據(jù)不同字段值顯示相應(yīng)的小圖片
          如根據(jù)庫(kù)存材料的不同狀態(tài)在數(shù)據(jù)單元格中顯示相應(yīng)圖片,具體設(shè)置如下:
          添加一個(gè)imagelist組件img1并在其中添加一組bmp,ico格式的圖片。然后將需要顯示圖片的列的imagelist屬性設(shè)置為img1;在keylist屬性中添加實(shí)際數(shù)據(jù)存儲(chǔ)值,一行為一個(gè)值,切記一定要與imagelist中圖片順序一一對(duì)應(yīng),否則會(huì)張冠李戴,面目全非。還可在picklist中添加提示信息,也要求是一行為一個(gè)值,并設(shè)tooltiptrue,那么,運(yùn)行時(shí)當(dāng)鼠標(biāo)移動(dòng)到該數(shù)據(jù)單元格時(shí)在顯示圖片的同時(shí)還顯示提示信息,怎么樣,功能夠強(qiáng)大吧!可使用空格鍵或鼠標(biāo)切換下一張圖片,圖片切換的同時(shí)也改變了實(shí)際存儲(chǔ)數(shù)據(jù)值。也可通過(guò)shift+空格或鼠標(biāo)切換為上一張圖片。這樣就實(shí)現(xiàn)了上下兩個(gè)方向圖片切換。
       
       2、顯示檢查框(checkbox)外觀
          對(duì)于Boolean型字段值在dbgrideh組件中自動(dòng)顯示為檢查框。通常情況下我們需將非Boolean型字段值也此外觀顯示,如性別字段為字符型,字段值為男性時(shí)為選中,女性時(shí)為未選中。需要在keylist編輯器中設(shè)置實(shí)際存儲(chǔ)數(shù)據(jù)值,第一行為選中時(shí)的值1”,第二行為未選中的值0”,第三行為其它值2”,支持三態(tài)顯示。設(shè)checkbox屬性為True.
       
       3、顯示單、多列下拉列表
          根據(jù)單元格字段值顯示與其相關(guān)的其它表字段內(nèi)容,如部門代碼字段顯示為部門名稱。首先需在當(dāng)前表中新建立一個(gè)lookup型字段,設(shè)置好關(guān)聯(lián)表的字段和返回字段。多列下拉列表需在單列基礎(chǔ)上做進(jìn)一步設(shè)置,在LookupDisplayFields中以;號(hào)將關(guān)聯(lián)表中多個(gè)字段分隔開(kāi),而且返回字段必須作為其中的第一項(xiàng)。具體設(shè)置如下:
       
      dropdownshowtitlestrue 
      dropdownsizingtrue
      dropdownwidth-1 
       
          例:當(dāng)前表中只有部門代碼無(wú)部門名稱列,需與部門表建立關(guān)聯(lián),當(dāng)點(diǎn)擊單元格時(shí)以部門代碼、部門名稱兩列下拉列表形式顯示。
       
       4、顯示日歷下拉列表
          Date  DateTime類型字段值均可以此形式顯示。外觀與編輯框無(wú)異,當(dāng)點(diǎn)擊該單元格時(shí),右側(cè)會(huì)出現(xiàn)“▽”符號(hào),點(diǎn)擊之即可出現(xiàn)日歷下拉列表。有時(shí)不希望出現(xiàn)日歷下拉列表,只需設(shè)置Column.ButtonStyle屬性為 cbsNone即可,此方法同樣適用于其它組件不以特殊外觀顯示的情況。
       
       5、3D或平面外觀效果
          設(shè)置OptionsEh屬性 中fixed, frozen, footer  data rows等屬性表格外觀為3D效果,設(shè)置flattrue則為平面外觀效果.
       
       6、鎖定多列不滾動(dòng)
          當(dāng)表格水平方向信息在一屏幕顯示不下時(shí),此項(xiàng)功能非常有用。例如,工資表格中包含姓名、基本工資、績(jī)效工資等信息一屏幕顯示不下,需要通過(guò)移動(dòng)水平滾動(dòng)條顯示下一屏信息。如果不鎖定關(guān)鍵字段列如姓名,則移動(dòng)到下一屏?xí)r就不知道此條記錄對(duì)應(yīng)的姓名。因此,在實(shí)際應(yīng)用中經(jīng)常需鎖定多列不滾動(dòng)。
       
          例:姓名字段為表格第二列,則設(shè)置FrozenCols=2.這樣當(dāng)一屏幕顯示不下,通過(guò)移動(dòng)水平滾動(dòng)條顯示下一屏信息時(shí),表格前兩列不滾動(dòng),作為參照列。
       
      D、導(dǎo)入/導(dǎo)出數(shù)據(jù)
          導(dǎo)入/導(dǎo)出數(shù)據(jù)在實(shí)際處理過(guò)程中是比較煩瑣的。但是Enlib3.0提供了一系列函數(shù)讓你輕松實(shí)現(xiàn)此功能,而且支持的文件格式很多:Text, Csv, HTML, RTF, XLS 和內(nèi)部數(shù)據(jù)格式。除此之外,還可對(duì)任意選擇的數(shù)據(jù)區(qū)域進(jìn)行操作。函數(shù)如下:
       
      Pascal: SaveDBGridEhToExportFile(TDBGridEhExportAsText,DBGridEh1,'c:\temp\file1.txt',False);
       
      C++: SaveDBGridEhToExportFile(__classid(TDBGridEhExportAsText),DBGridEh1,"c:\\temp\\file1.txt",false);
       
      說(shuō)明:其中false參數(shù)表示導(dǎo)出的是選中的局部數(shù)據(jù)區(qū)域數(shù)據(jù),true則為整個(gè)表格數(shù)據(jù)。
       
         例:將當(dāng)前表格中數(shù)據(jù)導(dǎo)出為EXCEL等格式文件。
          在窗體中添加一個(gè)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組件. 
          通過(guò)筆者上述介紹,想必你已經(jīng)對(duì)Enlib組件包產(chǎn)生好感而且越越欲試了,那就趕快下載使用吧。但是,使用一段時(shí)間并且喜歡上它后,你又有新的問(wèn)題產(chǎn)生了,那就是為了保持界面風(fēng)格一致,能否將已經(jīng)開(kāi)發(fā)完成的應(yīng)用程序中的DBGrid組件能否轉(zhuǎn)換為DBGridEh組件,進(jìn)行一次徹底革命?答案是肯定的。盡管DBGridEh并不是繼承于CustomDBGrid組件, 但是DBGridEhDBGrid它們之間有許多相同之處.因此可以相互轉(zhuǎn)換。
       
      具體步驟如下:
       1、在Delphi IDE下打開(kāi)TDBGrid組件.
       2、通過(guò)組合鍵Alt-F12form 以文本方式顯示;
       3、將所有TDBGrid 對(duì)象名改變?yōu)?span lang=EN-US> TDBGridEh對(duì)象名,如:DBGrid1: TDBGrid改為 DBGrid1: TDBGridEh;
       4、再次通過(guò)組合鍵Alt-F12將文本方式恢復(fù)為form 顯示;
       5、將form各相關(guān)事件中定義的所有TDBGrid改為TDBGrideh,DBGrid1: TDBGrid改為DBGrid1: TDBGridEh;
       6、重新編譯應(yīng)用程序。
       
       
       
      DBGRIDEH Enlib 3.0組件包中的組件之一。Enlib 3.0組件包是一位俄國(guó)人為增強(qiáng)Borland系列開(kāi)發(fā)工具功能而開(kāi)發(fā)的第三方組件,它具有界面友好、功能強(qiáng)大、開(kāi)發(fā)效率高、、快速制作預(yù)覽/打印簡(jiǎn)單中國(guó)式報(bào)表等特點(diǎn)。因此,一推出即受到廣大Borland程序員的青睞。目前這個(gè)版本支持Borland Delphi versions 45,67  Borland C++ Builder versions 4 & 5 ,可極大地提高數(shù)據(jù)庫(kù)應(yīng)用系統(tǒng)客戶端的性能。許多商品軟件如《速達(dá)2000》等都使用了該組件。下面本人將使用該組件在實(shí)際系統(tǒng)開(kāi)發(fā)過(guò)程中的經(jīng)驗(yàn)總結(jié)如下。 
       
      Enlib3.0組件包中最重要而且功能最強(qiáng)大的莫過(guò)于dbgrideh組件,本文介紹的所有實(shí)例均在Delphi 7開(kāi)發(fā)環(huán)境下調(diào)試通過(guò)。
       
      一、DBGridEh(增強(qiáng)型表格組件)功能詳解
      DBGridEh組件無(wú)論在外觀上還是功能上都非常類似Borland開(kāi)發(fā)工具中現(xiàn)有的dbgrid組件,它除了提供dbgrid組件的全部功能外,還增加了下列新功能:
       
      任意選擇多行、列或矩形區(qū)域的數(shù)據(jù).
      為多列標(biāo)題設(shè)定共同的父標(biāo)題行.
      表格底部(Footer) 區(qū)顯示求和、計(jì)數(shù)和其它統(tǒng)計(jì)信息.
      自動(dòng)調(diào)整組件寬度與客戶區(qū)域等寬.
      設(shè)置標(biāo)題行、數(shù)據(jù)行的高度.
      超長(zhǎng)的標(biāo)題行、數(shù)據(jù)行文本自動(dòng)折行處理.
      標(biāo)題行可作為按鈕使用,并可選擇是否顯示排序標(biāo)志符(降序升序).
      點(diǎn)擊列標(biāo)題可對(duì)當(dāng)前列自動(dòng)排序而無(wú)需編寫代碼.
      能夠自動(dòng)設(shè)置刪除超長(zhǎng)文本顯示不下的多余部分,并以省略號(hào)()代替.
      自動(dòng)搜索字段(Lookup)數(shù)據(jù)單元格以單、多列字段下拉列表形式顯示.
      自動(dòng)搜索字段(Lookup)數(shù)據(jù)單元格可進(jìn)行增量搜索.
      可鎖定任意列數(shù)在屏幕水平方向不滾動(dòng).
      日期時(shí)間控件DateTime picker 可支持TDateField and TDateTimeField兩種日期格式.
      根據(jù)字段不同值顯示關(guān)聯(lián)的ImageList 對(duì)象圖片組中的圖片.
      隱藏任意列.
      顯示3D風(fēng)格的數(shù)據(jù)區(qū)、表尾區(qū)和鎖定滾動(dòng)列,制作3D外觀表格.
      顯示Memo類型字段值.
      BOOLEAN型數(shù)據(jù)外,其它數(shù)據(jù)類型也可以檢查框( checkbox )形式顯示數(shù)據(jù).
      使用專門的函數(shù)和過(guò)程來(lái)存取以regini文件格式保存的表格布局(包含各數(shù)據(jù)列表、數(shù)據(jù)列訪問(wèn)順序、列寬、索引標(biāo)識(shí)、行高等信息)文件。
      通過(guò)設(shè)置數(shù)據(jù)單元格的hintToolTips屬性,當(dāng)移動(dòng)鼠標(biāo)到該單元格時(shí),可以顯示單元格容納不下的文本內(nèi)容.
      將組件中數(shù)據(jù)導(dǎo)入/導(dǎo)出到Text, Csv, HTML, RTF, XLS 和內(nèi)部數(shù)據(jù)等多種格式的文件中.
       
      DBGridEh組件主要屬性見(jiàn)下表(其它屬性參見(jiàn)dbgrid):
       
      DBGridEh組件事件基本與DBGrid相同,在此不再贅述。
       
       
       
      DBGridEH 所有列寬自動(dòng)適應(yīng)的實(shí)現(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中直接點(diǎn)擊title就可按點(diǎn)擊的那個(gè)字段排序的方法
      第一種方法(未測(cè)試)
      procedure TForm1.DBGridEh1TitleClick(Column: TColumnEh);
      begin
      //點(diǎn)擊GridEh標(biāo)題排序
      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; 
       
      第二種方法(未測(cè)試)
      procedure TPrintMai_frm.DBGridEh1TitleClick(Column: TColumnEh);
      var
      sortstring: string;
      begin //進(jìn)行排序
      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í)際數(shù)據(jù)集變量名
        except
        end;
      end;
      end; 
       
       
       
       
       
       
      Delphi 7中的安裝方法
       
        1.  EhLib 中的 common  DataService 文件拷貝到 Delphi7 目錄中.
        2. TOOLS->Environment Options->Library->Library Path 中添入EHLIB路徑。
        3.打開(kāi)新建文件夾中的 EHLIB70.DPK ,編譯一下,但不要安裝。
        4.打開(kāi)Ehlib中的DclEhLib70.DPK,編譯,安裝 
        5. Delphi 7中打開(kāi)DclEhLib70.dpk,編譯并安裝。
        6. 組件面板中出現(xiàn)一個(gè)EhLib的組件頁(yè)。
        7. 打開(kāi)附帶的DEMOS,編譯并運(yùn)行,測(cè)試安裝成功。
       
       
       
      以下是EHLIB的導(dǎo)出代碼:(其實(shí)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 合計(jì)行信息
      // 參數(shù): pDbGrid:TDBGridEh;
      //     pcFields : string ; 字段列表,字段用逗號(hào)分隔
      //     pvtType : TFooterValueType ; 統(tǒng)計(jì)類型 TFooterValueType = (fvtNon, fvtSum, fvtAvg, fvtCount, fvtFieldValue, fvtStaticText);
      // 引用:
      // 例如:DbGridEhFoot( DbGridEh1, &acute;Number,Sum&acute;, fvtSum ); 設(shè)定數(shù)量和金額字段為合計(jì)統(tǒng)計(jì)
      //--------------------------------------------------------------------------------
      Procedure DbGridEhFoot( pDbGrid:TDBGridEh; pcFields: string; pvtType : TFooterValueType );
      var nFldLoop : integer ;
         cFieldName : string ;
         tmpFldList : TStrings ;
      begin
        pDbGrid.FooterRowCount := 1;   // 指定網(wǎng)格尾部統(tǒng)計(jì)行行數(shù)
        pDbGrid.SumList.Active := true;  // 激活統(tǒng)計(jì)
        pDbGrid.FooterColor   := clBtnFace ;  // 指定統(tǒng)計(jì)行顏色
        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)計(jì)類型
          end;
        end ;
        tmpFldList.Free ;
      end;
       
       
       
       
       
       
      一、如何在程序中確定Ehlib定義的報(bào)表表頭顏色? 
      在執(zhí)行打印之前,加上下面的語(yǔ)句:
      DBGridEh1.FixedColor:=clLime;//clLime可以換成你想要的顏色比如clRed是紅色等等
      PrintDBgridEh1.Options:=[pghColored,pghFitingByColWidths];//方括號(hào)里的"pghColored"是必需的,其它的根據(jù)你的需要決定取舍
       
      二、Ehlib中的PrintDBGridEh如何印頁(yè)碼,即第幾頁(yè)共幾頁(yè)
      PrintDBGridEh.PageFooter.CenterText:='&[Page]頁(yè) 共&[Pages]頁(yè)'
      在對(duì)象管理器中的相關(guān)位置有設(shè)
       
      三、本人剛學(xué)習(xí)使用ehlib包,現(xiàn)在要實(shí)現(xiàn)類似ehlib所帶demo1中一個(gè)功能:點(diǎn)擊dbgrid某列值
      的下拉按紐(可以設(shè)置),彈出一個(gè)下拉列表(其內(nèi)容是另一個(gè)dataset的)。我看demo1
      好象是使用了DBLookupComboboxEh,可我參看demo1怎么設(shè)置也不行,請(qǐng)指教。謝謝。
       
      呵呵,這個(gè)效果沒(méi)有用DBLoookupComboBoxEh,它是利用查找字段實(shí)現(xiàn)的。
      它在Query1中,對(duì)應(yīng)VName字段,增加了一個(gè)叫VName1的查找字段,這個(gè)
      字段從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和縱向滾動(dòng)條 
      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)格標(biāo)題 
      復(fù)雜標(biāo)題 
      TDBGridEh 允許在多列上創(chuàng)建標(biāo)題,例如: 
       
      設(shè)置 DBGridEh.UseMultiTitle 屬性為 True 并且填充字段的標(biāo)簽或列標(biāo)題的標(biāo)題,可以使用下面的規(guī)則:字段標(biāo)簽中的文本部分或列標(biāo)題必須由幾部分組成,并且用 "|" 分割,幾個(gè)列的每一個(gè)通用部分都設(shè)置為相同。其它字段或標(biāo)題必須在相應(yīng)的部分包含同樣的文本。 
       
      例如:
       
      Field1.DisplayLabel := 'Title1|SubTitle1';
      Field2.DisplayLabel := 'Title1|SubTitle2';
      DBGridEh.Columns[0].Title.Caption := 'Title1|SubTitle1';
      DBGridEh.Columns[1].Title.Caption := 'Title1|SubTitle2'; 
      按鈕式標(biāo)題 
      設(shè)置Column.Title.TitleButton  True可以強(qiáng)制標(biāo)題單元為按鈕式。寫 OnTitleBtnClick事件來(lái)控制用戶單擊標(biāo)題單元時(shí)的操作。 
       
      在標(biāo)題中顯示位圖 
      To show bitmap in titles instead of caption use TitleImages property of TDBGridEh and ImageIndex property of TColumnTitleEh. 
       
      自動(dòng)用位置標(biāo)識(shí)排序標(biāo)題. 
      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. 
       
      標(biāo)題屬性的默認(rèn)值 
      使用TDBGridEh.ColumnDefValues.Title來(lái)設(shè)置標(biāo)題屬性的默認(rèn)值。 
       
      定制網(wǎng)格頁(yè)腳 
      頁(yè)腳以及統(tǒng)計(jì)值 
      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 屬性來(lái)指定數(shù)據(jù)行高。完整的數(shù)據(jù)行高 = 行線高度+行高。設(shè)置 RowSizingAllowed  True 以允許可以在運(yùn)行是使用鼠標(biāo)來(lái)改變行高。
       
      設(shè)置Column.WordWrapTrue可以使數(shù)據(jù)行中文本多行顯示。如果行高>文本行,它就換行。 
       
      顯示備注字段 
      設(shè)置 DrawMemoText to True來(lái)顯示文本式的備注字段。. 
       
      定制單元格字體及顏色 
      TDBGridEh 中的 Font  Color 屬性描述了數(shù)據(jù)網(wǎng)格中繪制單元格的字體和顏色。
      TColumnEh 中的 Font  Color 屬性描述了指定列中繪制單元格的字體和顏色。 
       
      事件定制單元格字體及顏色 
      有幾個(gè)事件可以讓你能夠在繪制單元格前定制單元格字體和顏色。你可以寫TDBGridEhOnDrawColumnCellEvent事件句柄來(lái)在控制在網(wǎng)格單元中繪制數(shù)據(jù)。你可以使用Canvas屬性的方法來(lái)繪制單元格。但是如果你只想改變字體或顏色的屬性,我建議你使用下面的事件。你可以寫TDBGridEhOnGetCellParams事件來(lái)控制在繪制數(shù)據(jù)單元以前所指定的操作。你可以改變繪制字體及背景色。這個(gè)事件適合你在想改變整行的字體或顏色時(shí)使用。如果你想改變指定列中單元格的屬性,你可以使用TColumnEh.OnGetCellParams。寫這個(gè)事件用來(lái)控制在一列數(shù)據(jù)單元被重繪或編輯時(shí)的操作。在一列數(shù)據(jù)單元被重繪以前,你可以改變繪制字體,背景色,對(duì)齊方式,圖像索引,文本或檢查框。在編輯一列數(shù)據(jù)單元以前,你可以改變編輯字體,背景色,文本或只讀狀態(tài)。 
       
      列屬性的默認(rèn)值 
       
      使用ColumnDefValues屬性來(lái)設(shè)置列屬性的默認(rèn)值。新創(chuàng)建的列將從ColumnDefValues屬性中獲得屬性值,并且直到第一次為其指定值為止。
       
       
      在網(wǎng)格的適當(dāng)位置放置編輯器. 
      在下拉列表中顯示幾個(gè)字段。 
      在下拉列表中顯示幾個(gè)下拉字段,需要設(shè)置列的LookupDisplayFields屬性到字段的Semicolons屬性來(lái)分割多個(gè)字段名。命名為Column.Field.LookupResultField的屬性必須位于LookupDisplayFields列表中。多字段的下拉列表只能應(yīng)用于下拉字段。 
       
      顯示下拉方式的列 
       
      你可以通過(guò)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. 
       
      下拉式計(jì)算器 
      對(duì)于 TDateField  TDateTimeField 字段,inplace 編輯器將顯示下拉按鈕以顯示顯示下拉計(jì)算器。設(shè)置 Column.ButtonStyle  cbsNone 以禁止顯示下拉按鈕。 
       
      設(shè)置編輯器顏色和字體 
      Inplace編輯器可以設(shè)置數(shù)據(jù)單元的顏色和字體。數(shù)據(jù)單元使用OnGetCellParams 事件來(lái)控制列的顏色和字體。 Inplace 編輯器在行高>一行的高度時(shí)自動(dòng)設(shè)置為多行模式并且將列的屬性 WordWrap 設(shè)置為True.
       
       
      自動(dòng)填充網(wǎng)格列寬到網(wǎng)格客戶區(qū) 
      設(shè)置AutoFitColWidthsTrue以自動(dòng)重置列寬來(lái)設(shè)置網(wǎng)格的寬度等于客戶區(qū)寬度。 MinAutoFitWidth 屬性決定網(wǎng)格的最小寬度,列寬將會(huì)被重新計(jì)算。 
      3D或平面外觀 
      使用 OptionsEh 屬性來(lái)顯示/隱藏固定的3D框架,冷區(qū),頁(yè)腳以及數(shù)據(jù)行。 
       
      使用 Flat 屬性來(lái)設(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對(duì)象)或文件。 
       
      例子
      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對(duì)象)。 
       
      其它特性 
      lookup 編輯器,你可以在運(yùn)行時(shí)清空(設(shè)置為Null LookupKeyField 值。比如選擇整個(gè)文本然后按Delete鍵。 
       
      冷區(qū) 
      冷區(qū)是數(shù)據(jù)網(wǎng)格列集左邊顯示的不可滾動(dòng)的區(qū)域。與固定列不同的是,冷區(qū)的列可以獲得編輯焦點(diǎn)??梢酝ㄟ^(guò)設(shè)置FrozenCols屬性來(lái)設(shè)置右邊不可滾動(dòng)的列集。 
       
      增量搜索 
       
      TDBGridEh 允許用戶在網(wǎng)格列中實(shí)現(xiàn)特定的增量搜索。當(dāng)用戶進(jìn)入增量搜索時(shí)他可以顯示字符以及網(wǎng)格,并且在當(dāng)前的列中查找文本。使用 dghIncSearch  dghPreferIncSearch的值(在OptionsEh 選項(xiàng)中) 在數(shù)據(jù)網(wǎng)格中操作增量搜索。 dghIncSearch 值允許在數(shù)據(jù)網(wǎng)格中進(jìn)行增量搜索。運(yùn)行時(shí)你能夠使用下面的鍵進(jìn)行增量搜索:
       
      Ctrl+F - 開(kāi)始增量搜索。
      Ctrl+Enter - 查找下一個(gè)匹配記錄。
      Ctrl+Shift+Enter - 查找前一個(gè)匹配記錄。
       
      如果OptionsEh選項(xiàng)中的 dghIncSearch 是只讀的,那么網(wǎng)絡(luò)將自動(dòng)設(shè)置增量模式在第一次按鍵以及1.5秒后返回普通模式。 dghPreferIncSearch 值決定網(wǎng)格設(shè)置自動(dòng)增量搜索模式在第一次按鍵時(shí)替代單元編輯。
       
      水平或垂直滾動(dòng)條 
      使用 VertSctollbar, HorzSctollbar 屬性來(lái)顯示/隱藏以及跟蹤水平或垂直滾動(dòng)條。 
       
      多選 
      TDBGridEh 允許在選定的區(qū)域上進(jìn)行選擇記錄,列以及矩形區(qū)域等操作: 
       
      允許多選會(huì)影響下面這些屬性: 
       
      Options選項(xiàng)中的dgMultiSelect屬性 - 設(shè)置是否允許多選。 
       
      Options選項(xiàng)中的dghClearSelection 屬性- 設(shè)置在用戶移到下一個(gè)單元時(shí)是否清除已選記錄。 
       
      Options選項(xiàng)中的EditActions屬性 -設(shè)置用戶可以在已選記錄上執(zhí)行哪些操作(比如,拷貝,剪切,刪除,粘貼,全選等)。 
       
      Options選項(xiàng)中的AllowedSelections屬性-設(shè)置允許選定記錄的類型(比如,行,列,矩形區(qū)域等)。 
       
      Options選項(xiàng)中的Selection屬性-設(shè)置一個(gè)當(dāng)前的多選狀態(tài),已選記錄,列或矩形區(qū)域以及存取它們的屬性和函數(shù)。 
       
      從注冊(cè)表或ini文件中保存或恢復(fù)網(wǎng)格和列的層次。 
      TDBGridEh 有一個(gè)常規(guī)設(shè)置來(lái)從注冊(cè)表或ini文件中保存和恢復(fù)網(wǎng)絡(luò)以及列的層次: 
       
      RestoreColumnsLayout - 從注冊(cè)表中恢復(fù)列的次序,寬度,排序標(biāo)志。 
       
      RestoreColumnsLayoutIni - ini文件中恢復(fù)列的次序,寬度,排序標(biāo)志。 
       
      RestoreGridLayout - 從注冊(cè)表中恢復(fù)列的次序,寬度,可視,排序標(biāo)志,排序索引或行高。 
       
      RestoreGridLayoutIni - ini文件中恢復(fù)列的次序,寬度,可視,排序標(biāo)志,排序索引或行高。 
       
      SaveColumnsLayout - 保存列的次序,寬度,排序標(biāo)志到注冊(cè)表中。 
       
      SaveColumnsLayoutIni - 保存列的次序,寬度,排序標(biāo)志到ini文件中。 
       
      SaveGridLayout - 保存列的次序,寬度,可視,排序標(biāo)志,排序索引或行高到注冊(cè)表中。 
       
      SaveGridLayoutIni - 保存列的次序,寬度,可視,排序標(biāo)志,排序索引或行高到ini文件中。 
       
      當(dāng)前版本的TDBGridEh不支持的特性: 
      這個(gè)版本的TDBGridEh不支持下面的特性: 
       
      TDBGridEh 不能設(shè)置每一個(gè)數(shù)據(jù)窗口中單獨(dú)行的行高。 
       
      TDBGridEh 不能象TreeView那樣工作。它不能擁有節(jié)點(diǎn)和枝葉。 
       
      TDBGridEh 不能橫向或縱向合并數(shù)據(jù)單元。 
       
      將已存在的TDBGrid組件轉(zhuǎn)換為TDBGridEh組件: 
       
      盡管TDBGridEh并不是從TCustomDBGrid組件繼承而來(lái)的,但是在TDBGridEhTDBGrid中還是有一些相似的屬性。 
       
      它允許僅用一點(diǎn)點(diǎn)代價(jià)來(lái)轉(zhuǎn)換已存在的TDBGrid組件到TDBGridEh。 
       
      可以按照下面的提示來(lái)轉(zhuǎn)轉(zhuǎn)換已存在的TDBGrid組件到TDBGridEh 
       
      DelphiIDE中打開(kāi)包含有TDBGrid組件的應(yīng)用程序。 
       
      設(shè)置視圖方式為文本方式(Alt-F12)。 
       
      將所有的TDBGrid對(duì)象改名為TDBGridEh'比如,將object DBGrid1: TDBGrid'改為 'object DBGrid1: TDBGridEh')
       
      重新編譯你的應(yīng)用程序。 
       
       
       
      5 使用 TDBSumList 組件
      TDBSumList說(shuō)明 
      你可以使用TDBSumList在可視動(dòng)態(tài)變化數(shù)據(jù)集中進(jìn)行記錄統(tǒng)計(jì)。在你想查看的數(shù)據(jù)集中設(shè)置相關(guān)的數(shù)據(jù)字段,然后寫 SumListChanged 事件來(lái)指定在TDBSumList發(fā)生改變后所要做的操作。TDBSumList  SumCollection 屬性上一個(gè) TDBSum 對(duì)象容器。每個(gè) TDBSum 對(duì)象是一個(gè)可以指定集合值的元件。 FieldName  GroupOperation 決定了集合值的類型,SumValue 控制當(dāng)前的集合值。
       
      TDBSumList 被埋藏于 DBGridEh 組件中,因此所的下面有關(guān)TDBGridEh.SumList 的說(shuō)明與TDBGirdEhTDBGridEh.SumList 屬性的說(shuō)明是一樣的。
       
      如何工作以及為什么有時(shí)SumList的集合值計(jì)算不正確?
      你知道 data-aware 控件與數(shù)據(jù)集是通過(guò) TDataLink 對(duì)象相連接的。 TDataLink 不允許快速重新計(jì)算集合值。例如,當(dāng)從數(shù)據(jù)集中刪除記錄時(shí),數(shù)據(jù)集發(fā)送deDataSetChange事件給所有的TDataLink對(duì)象,在當(dāng)前局部過(guò)濾發(fā)生改變時(shí),同樣的事件也被發(fā)送了。因此當(dāng) TDataLink 接收到該事件時(shí),它不得不在所有的數(shù)據(jù)集中重新計(jì)算集合值,甚至于僅從數(shù)據(jù)集中刪除一條記錄。在激活后, TDBSumList 重載了數(shù)據(jù)集的下列事件: OnAfterEdit, OnAfterInsert, OnAfterOpen, OnAfterPost, OnAfterScroll, OnBeforeDelete, OnAfterClose。這種方法避免了在所有的數(shù)據(jù)集中它。在不需要它時(shí),又會(huì)出現(xiàn)其它的問(wèn)題,比如: T
      運(yùn)行時(shí)指定這些事件。在指定上述事件之一以前,關(guān)閉 SumList。
      在下面的情形下,SumList 發(fā)出存取錯(cuò)誤信息。 SumList 試著向數(shù)據(jù)集返回事件,但數(shù)據(jù)集數(shù)據(jù)并未被刪除。在SumList(或網(wǎng)格)以及數(shù)據(jù)集放置在不同的窗體(數(shù)據(jù)模塊)這種情況下,可以顯示數(shù)據(jù)。在這種情況下,試著在從數(shù)據(jù)集或數(shù)據(jù)模塊中數(shù)據(jù)集數(shù)據(jù)被刪除時(shí)關(guān)閉SumList
      如果你使用SetRangeApplyRange事件,SumList 將不能跟蹤數(shù)據(jù)集中發(fā)生的變化。但可以調(diào)用 SumList.RecalAll 方法。
      在非BDE數(shù)據(jù)集的主/明細(xì)表中,SumList 不能跟蹤數(shù)據(jù)變化。在主數(shù)據(jù)集激活狀態(tài)數(shù)據(jù)集發(fā)生變化后,調(diào)用 SumList.RecalAll 方法。
      在任何其它情況下,如果你發(fā)現(xiàn)了其它SumList的計(jì)算值不正確,你都可以調(diào)用 RecalAll方法。 
       
       
      4 使用 TPrintDBGridEh 組件 
       Delphi  IDE 組件面板中選擇 TPrintDBGridEh 并拖放其到 form 上。在 其 DBGridEh 屬性中設(shè)置為相應(yīng)的 TDBGridEh 組件。使用 Print 以及 Preview 方法在打印機(jī)上打印網(wǎng)格或者在預(yù)覽窗體中預(yù)覽網(wǎng)格。 
      網(wǎng)格前以網(wǎng)格后的 RTF 文本 
      TPrintDBGridEh 允許在打印 / 打印預(yù)覽網(wǎng)格前或網(wǎng)格后的 RTF 文本。使用 AfterGridText 以及 BeforeGridText 屬性來(lái)指定文本。 你可以在打印 / 打印預(yù)覽時(shí)通過(guò) BeforeGridText  AfterGridText 屬性來(lái)替換文本內(nèi)容。 
      當(dāng)前版本的 TPrintDBGridEh 不支持的特性。 
      這個(gè)版本不支持下面的特性: 
      TPrintDBGridEh 不能在一頁(yè)中打印 / 打印預(yù)覽多個(gè)網(wǎng)格。 
      問(wèn)答列表 : 
      問(wèn) : 怎樣進(jìn)行橫向打印 / 打印預(yù)覽? 
       : TPrintDBGridEh 并沒(méi)有專六的屬性來(lái)設(shè)置頁(yè)面特性。在調(diào)用打印或打印預(yù)覽方法前,你必須設(shè)置你將要執(zhí)行打印的打印源( Orientation )。 
      uses ......, PrViewEh, Printers. 
      .............. 
      procedure TForm1.bPreviewClick(Sender: TObject); 
      begin 
      PrinterPreview.Orientation := poLandscape; 
      PrintDBGridEh1.Preview; 
      end ;
       
       
      DBGrid介紹:
       Delphi 語(yǔ)言的數(shù)據(jù)庫(kù)編程中,DBGrid 是顯示數(shù)據(jù)的主要手段之一。但是 DBGrid 缺省的外觀未免顯得單調(diào)和缺乏創(chuàng)意。其實(shí),我們完全可以在我們的程序中通過(guò)編程來(lái)達(dá)到美化DBGrid 外觀的目的。通過(guò)編程,我們可以改變 DBGrid 的表頭、網(wǎng)格、網(wǎng)格線的前景色和背景色,以及相關(guān)的字體的大小和風(fēng)格。
         以下的示例程序演示了對(duì) DBGrid 各屬性的設(shè)置,使 Delphi 顯示的表格就像網(wǎng)頁(yè)中的表格一樣漂亮美觀。
         示例程序的運(yùn)行:
          Form1 上放置 DBGrid1、Query1、DataSource1 三個(gè)數(shù)據(jù)庫(kù)組件,設(shè)置相關(guān)的屬性,使 DBGrid1 能顯示表中的數(shù)據(jù)。然后,在 DBGrid1  onDrawColumnCell 事件中鍵入以下代碼,然后運(yùn)行程序,就可以看到神奇的結(jié)果了。本代碼在 Windows98、Delphi5.0 環(huán)境下調(diào)試通過(guò)。
      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; //定義畫筆顏色(藍(lán)色)
         MoveTo(Rect.Left, Rect.Bottom); //畫筆定位
         LineTo(Rect.Right, Rect.Bottom); //畫藍(lán)色的橫線
         Pen.Color := $0000ff00; //定義畫筆顏色(綠色)
         MoveTo(Rect.Right, Rect.Top); //畫筆定位
         LineTo(Rect.Right, Rect.Bottom); //畫綠色的豎線
       end;
      end; 
       
       問(wèn)題: Delphi5 - 隔行改變DBGrid網(wǎng)格顏色    Form1 上放置 DBGrid1、Query1DataSource1 三個(gè)數(shù)據(jù)庫(kù)組件,設(shè)置相關(guān)的屬性,使 DBGrid1 能顯示表中的數(shù)據(jù)。然后,在 DBGrid1  onDrawColumnCell 事件中鍵入以下代碼,然后運(yùn)行程序
       
      代碼:
      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; //定義畫筆顏色(藍(lán)色)
         MoveTo(Rect.Left, Rect.Bottom); //畫筆定位
         LineTo(Rect.Right, Rect.Bottom); //畫藍(lán)色的橫線
         Pen.Color := clbtnface; //定義畫筆顏色(蘭色)
         MoveTo(Rect.Right, Rect.Top); //畫筆定位
         LineTo(Rect.Right, Rect.Bottom); //畫綠色
       end;
      end;
       
      BDE中的table1未能通過(guò),顏色沒(méi)有隔行變化。  
       
       
       2003-11-11 17:12:09    DelphiDBGrid中插入其他可視組件   Delphi提供了功能強(qiáng)大的 DBGrid組件,以方便進(jìn)行數(shù)據(jù)庫(kù)應(yīng)用程序設(shè)計(jì)。但是如果我們僅僅利用DBGrid組件,每一個(gè)獲得焦點(diǎn)(Grid)只是一個(gè)簡(jiǎn)單的文本編輯框,不方便用戶輸入數(shù)據(jù)。Delphi也提供了一些其他數(shù)據(jù)組件來(lái)方便用戶輸入,比如DBComboBoxDBCheckBox等組件,但這些組件卻沒(méi)有DBGrid功能強(qiáng)大。Delphi能不能象Visual Foxpro那樣讓DBGrid中獲得焦點(diǎn)網(wǎng)格可以是其它可視數(shù)據(jù)組件以方便用戶呢?其實(shí)我們可以通過(guò)在DBGrid中插入其他可視組件來(lái)實(shí)現(xiàn)這一點(diǎn)。
       
         Delphi對(duì)DBGrid處理的內(nèi)部機(jī)制,就是在網(wǎng)格上浮動(dòng)一個(gè)組件——DBEdit組件。你輸入數(shù)據(jù)的網(wǎng)格其實(shí)是浮動(dòng)DBEdit組件,其他未獲得焦點(diǎn)地方不過(guò)是圖像罷了。所以,在DBGrid中插入其他可視組件就是在網(wǎng)格上浮動(dòng)一個(gè)可視組件。因此任何組件,包括從簡(jiǎn)單的DbCheckBox到復(fù)雜的對(duì)話框,都可以在DBGrid中插入。下面就是一個(gè)如何在DBGrid中插入DBComboBox組件的步驟,采用同樣的辦法可以插入其他組件。
       
       1、在Delphi 4.0中新建一個(gè)項(xiàng)目。
       
       2、分別拖動(dòng)的Data Access組件板上DataSourceTable,Data Controls組件板上DBGrid,DBComboBox四個(gè)組件到Form1上。
       
       3、設(shè)置各個(gè)組件的屬性如下:
       
      rcf1對(duì)象 屬性 設(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)獲得焦點(diǎn)網(wǎng)格所對(duì)應(yīng)的字段與組合框所對(duì)應(yīng)的字段一致時(shí),移動(dòng)組合框到獲得焦點(diǎn)的網(wǎng)格上,并且使組合框可視,從而達(dá)到在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指定單元格未獲得焦點(diǎn)時(shí)不顯示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指定列獲得焦點(diǎn)時(shí)DrawDataCell事件只是繪制單元格,并顯示DBComboBox,但是DBComboBox并沒(méi)有獲得焦點(diǎn),數(shù)據(jù)的輸入還是在單元格上進(jìn)行。在DBGrid1KeyPress事件中調(diào)用SendMessage這個(gè) Windows API函數(shù)將數(shù)據(jù)輸入傳輸?shù)?span lang=EN-US>DBComboBox上,從而達(dá)到在DBComboBox上進(jìn)行數(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.Handle,WM_Char,word(Key),0);
         end;
       end;
      end;
       
         程序在中文Windows 98Delphi 4.015 下調(diào)試通過(guò)。希望本文能使你可以更加方便快捷的開(kāi)發(fā)數(shù)據(jù)庫(kù)應(yīng)用程序。  
       
       
       2003-11-11 17:17:56    鎖定DBGrid左邊的列    我在使用 Delphi3 進(jìn)行數(shù)據(jù)庫(kù)編程的時(shí)候,希望 DBGRID 構(gòu)件在顯示數(shù)據(jù)的時(shí)候能象FoxPro  BROWSE 命令一樣,鎖定左邊指定的幾列不進(jìn)行滾動(dòng),請(qǐng)問(wèn)應(yīng)用什么方法來(lái)實(shí)現(xiàn)?
       
         我們知道 Delphi  TStringGrid 有一個(gè)屬性 FixedCols 來(lái)指定不滾動(dòng)的列。雖然TDBGrid 不能直接使用這一屬性,但通過(guò)強(qiáng)制類型轉(zhuǎn)換也可以首先這一功能,因?yàn)檫@兩個(gè)類都來(lái)自 TCustomGrid 類。下面我們以 Delphi 3.0 Demos\Db\CtrlGrid 為例來(lái)說(shuō)明具體的用法。在這個(gè)例子的 TFmCtrlGrid.FormShow 過(guò)程中加入如下一行: 
       
         TStringGrid(DbGrid1).FixedCols := 2; 
       
         運(yùn)行該程序,在左右移動(dòng)各列時(shí),Symbol 列不會(huì)移動(dòng)。除了這種方法,也可以采用下面的方法:首先在 Form 聲明部分加上
       
         type TMyGrid = Class(TDBGrid) end; 
       
         然后在 TFmCtrlGrid.FormShow 過(guò)程中加入: 
       
         TMyGrid(DbGrid1).FixedCols := 2; 
       
         兩者從形式上略有不同,但實(shí)質(zhì)都是一樣的。我們這里設(shè)置 FixedCols  2,這是因?yàn)樵?span lang=EN-US> DBGrid 構(gòu)件最左側(cè)有個(gè)指示列,如果你將 DBGrid  Options 屬性的 dgIndicator 設(shè)為False,則應(yīng)設(shè)置 FixedCols 1  
       
       
       2003-11-11 17:21:36    使dbgrid的某幾筆資料變色   你可在 DBGrid 元件的 DrawDataCell 事件中依資料的條件性來(lái)改變格子或文字的顏色.
       :
       
      OnDrawDataCell(...)
      begin
       with TDBGrid(Sender) do
       begin
         if (條件) then
           Canvas.TextOut(Rect.Left + 4
         Rect.Top + 2
       
      '要顯示的文字如表格的資料');
      end;
       
         而你會(huì)看到 DBGrid 的顯示資料怎麼有重疊的情況那是因?yàn)樵?span lang=EN-US>DBGrid要顯示的資料與 TextOut 所顯示的資料重疊
         解決方法 :
          Query 元件所加入的欄位(在元件上按右鍵會(huì)有 Add Fields...的選單)在不要顯示資料的欄位的 OnGetText 事件中有一參數(shù)設(shè)定為 False;
       
      procedure TForm1.Query1Detail1GetText(Sender: TField; var Text: string;
      DisplayText: Boolean);
      begin
       // 決定在 DBGrid 得知表格資料時(shí)要不要顯示所得到的資料False -> 不顯示
       // 就可避免與 TextOut 的文字重疊了
       DisplayText : = False;
      end;
      end;
       
         如果用 Delphi 3 處理很簡(jiǎn)單.例如:對(duì)表中某字段當(dāng)其數(shù)值小于0時(shí)為紅字其他為黑字.
       DBGrid.OnDrawColumnCell(...) :
       
      begin
       if TableField.AsInteger < 0 then
         DBGrid.Canvas.Font.Color := clRed
       else
         DBGrid.Canvas.Font.Color := clBlack;
       DBGrid.DefaultDrawColumnCell(...);
      end;
       
      這樣對(duì) Field 指定的格式仍舊生效不必重寫.  
       
       
       2003-11-11 17:25:29    實(shí)戰(zhàn)Delphi數(shù)據(jù)網(wǎng)格色彩特效   Delphi中的數(shù)據(jù)網(wǎng)格控件(TDbGrid)對(duì)于顯示和編輯數(shù)據(jù)庫(kù)中大量的數(shù)據(jù)起著十分重要的作用;然而,在使用數(shù)據(jù)網(wǎng)格控件的同時(shí),也往往因?yàn)楸砀裰写罅康臄?shù)據(jù)不易區(qū)分,而令操作者眼花繚亂。如何提高網(wǎng)格控件的易用性,克服它的此項(xiàng)不足呢?本文從改變數(shù)據(jù)網(wǎng)格的色彩配置角度,提出了一種解決辦法。
       
         以下為數(shù)據(jù)網(wǎng)格控件的6種特殊效果的實(shí)現(xiàn)方法,至于數(shù)據(jù)網(wǎng)格控件與數(shù)據(jù)集如何連接的方法從略。
       
      1. 縱向斑馬線效果:實(shí)現(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ù)列用藍(lán)色
      False: DbGrid1.Canvas.Brush.Color:= clAqua; file://奇數(shù)列用淺綠色
      End;
      DbGrid1.Canvas.Pen.Mode:=pmMask;
      DbGrid1.DefaultDrawColumnCell (Rect
      DataCol
      Column
      State);
       
      2. 縱向斑馬線,同時(shí)以紅色突出顯示當(dāng)前單元格效果:以突出顯示當(dāng)前選中的字段。
       
      file://將上述代碼修改為:
      Case DataCol Mod 2 = 0 of
      True: DbGrid1.Canvas.Brush.Color:= clBlue; file://偶數(shù)列用藍(lán)色
      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ù)列顯示藍(lán)色
      end;
      DbGrid1.Canvas.pen.mode:=pmmask;
      DbGrid1.DefaultDrawColumnCell (Rect
      DataCol
      Column
      State);
      end;
       
      5.橫向斑馬線, 同時(shí)以紅色突顯當(dāng)前行效果。
       
      file://其它屬性設(shè)置同3,將上述代碼修改為:
      Case Table1.RecNo mod 2 = 0 of file://根據(jù)數(shù)據(jù)集的記錄號(hào)進(jìn)行判斷
      True : DbGrid1.Canvas.Brush.color:=clAqua; file://偶數(shù)行用淺綠色顯示
      False: DbGrid1.Canvas.Brush.color:=clblue; file://奇數(shù)行用藍(lán)色表示
      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ū)分,同時(shí),選中行以縱向斑馬線效果區(qū)分不同的列。
       
      file://其它屬性設(shè)置同3,將上述代碼修改為:
      Case Table1.RecNo mod 2 = 0 of file://根據(jù)數(shù)據(jù)集的記錄號(hào)進(jìn)行判斷
      True : DbGrid1.Canvas.Brush.color:=clAqua; file://偶數(shù)行用淺綠色顯示
      False: DbGrid1.Canvas.Brush.color:= clblue; file://奇數(shù)行用藍(lán)色表示
      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)格控件的列和行的色彩進(jìn)行了設(shè)置,讀者可以根據(jù)自己的需要設(shè)置特效。該程序在Delphi5中測(cè)試通過(guò)。  
       
       
       2003-11-13 11:11:31    點(diǎn)擊DBGridTitle對(duì)查詢結(jié)果排序 關(guān)鍵詞:DBGrid 排序  
       
         欲實(shí)現(xiàn)點(diǎn)擊DBGridTitle對(duì)查詢結(jié)果排序,想作一個(gè)通用程序,不是一事一議,例如不能在SQL語(yǔ)句中增加Order by ...,因?yàn)?span lang=EN-US>SQL可能原來(lái)已經(jīng)包含Order by ...,而且點(diǎn)擊另一個(gè)Title時(shí)又要另外排序,目的是想作到象資源管理器那樣隨心所欲。
       
      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的自動(dòng)添加功能 
      關(guān)鍵詞:DbGrid  
       
         移動(dòng)到最后一條記錄時(shí)再按一下就會(huì)追加一條記錄,如果去掉這項(xiàng)功能 
         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不支持鼠標(biāo)的上下移動(dòng)的解決代碼(感謝 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中移動(dòng)焦點(diǎn)到指定的行和列   dbgrid是從TCustomGrid繼承下來(lái)的,它有colrow屬性,只不過(guò)是protected的,不能直接訪問(wèn),要處理一下,可以這樣:
       
         TDrawGrid(dbgrid1).row:=row;
         TDrawGrid(dbgrid1).col:=col;
         dbgrid1.setfocus;
      就可以看到效果了。
       
         1 這個(gè)方法是絕對(duì)有問(wèn)題的,它會(huì)引起DBGrid內(nèi)部的混亂,因?yàn)?span lang=EN-US>DBGrid無(wú)法定位當(dāng)前紀(jì)錄,如果DBGrid只讀也就罷了(只讀還是會(huì)出向一些問(wèn)題,比如原本只能單選的紀(jì)錄現(xiàn)在可以出現(xiàn)多選等等,你可以自己去試試),如果DBGrid可編輯那問(wèn)題就可大了,因?yàn)楫?dāng)前紀(jì)錄的關(guān)系,你更改的數(shù)據(jù)字段很可能不是你想象中的
         2 我常用的解決辦法是將上程序改為(隨便設(shè)置col是安全的,沒(méi)有一點(diǎn)問(wèn)題)
       
         Query1.first;
         TDrawGrid(dbgrid1).col:=1;
         dbgrid1.setfocus;
       
         這就讓焦點(diǎn)移到第一行第一列當(dāng)中 
       
       
       2003-11-17 14:55:26    如何使DBGRID網(wǎng)格的顏色隨此格中的數(shù)據(jù)值的變化而變化?   在做界面的時(shí)候,有時(shí)候?yàn)榱送怀鲲@示數(shù)據(jù)的各個(gè)特性(如過(guò)大或者過(guò)小等),需要通過(guò)改變字體或者顏色,本文就是針對(duì)這個(gè)情況進(jìn)行的說(shuō)明。
       
         如何使DBGRID網(wǎng)格的顏色隨此格中的數(shù)據(jù)值的變化而變化。如<60的網(wǎng)格為紅色?
         Delphi中數(shù)據(jù)控制構(gòu)件DBGrid是用來(lái)反映數(shù)據(jù)表的最重要、也是最常用的構(gòu)件。在應(yīng)用程序中,如果以彩色的方式來(lái)顯示DBGrid,將會(huì)增加其可視性,尤其在顯示一些重要的或者是需要警示的數(shù)據(jù)時(shí),可以改變這些數(shù)據(jù)所在的行或列的前景和背景的顏色。
        DBGrid屬性DefaultDrawing是用來(lái)控制Cell(網(wǎng)格)的繪制。若DefaultDrawing的缺省設(shè)置為True,意思是Delphi使用DBGrid的缺省繪制方法來(lái)制作網(wǎng)格和其中所包含的數(shù)據(jù),數(shù)據(jù)是按與特定列相連接的Tfield構(gòu)件的DisplayFormatEditFormat特性來(lái)繪制的;若將DBGridDefaultDrawing特性設(shè)置成False,Delphi就不繪制網(wǎng)格或其內(nèi)容,必須自行在TDBGridOnDrawDataCell事件中提供自己的繪制例程(自畫功能)。
        在這里將用到DBGrid的一個(gè)重要屬性:畫布Canvas,很多構(gòu)件都有這一屬性。Canvas代表了當(dāng)前被顯示DBGrid的表面,你如果把另行定義的顯示內(nèi)容和風(fēng)格指定給DBGrid對(duì)象的CanvasDBGrid對(duì)象會(huì)把Canvas屬性值在屏幕上顯示出來(lái)。具體應(yīng)用時(shí),涉及到CanvasBrush屬性和FillRect方法及TextOut方法。Brush屬性規(guī)定了DBGrid.Canvas顯示的圖像、顏色、風(fēng)格以及訪問(wèn)Windows GDI 對(duì)象句柄,FillRect方法使用當(dāng)前Brush屬性填充矩形區(qū)域,方法TextOut輸出Canvas的文本內(nèi)容。
       
        以下用一個(gè)例子來(lái)詳細(xì)地說(shuō)明如何顯示彩色皼/div>

       

        本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
        轉(zhuǎn)藏 分享 獻(xiàn)花(0

        0條評(píng)論

        發(fā)表

        請(qǐng)遵守用戶 評(píng)論公約

        類似文章 更多