天天看點

cxGrid使用彙總4

49指定Filter   Builder打開/儲存過濾檔案的預設路徑

解決:

uses  

      ...,   cxFilterControlDialog;  

procedure   TForm.GridView1FilterControlDialogShow(  

      Sender:   TObject);  

begin  

      TfmFilterControlDialog(Sender).OpenDialog.InitialDir   :=   'D:/'  

end;

****************************************************************************

50在主從TableView中根據主TableView得到對應的從TableView

var  

      ADetailDC:   TcxGridDataController;  

      AView:   TcxCustomGridTableView;  

      with   cxGrid1DBTableView1.DataController   do  

          ADetailDC   :=   TcxGridDataController(GetDetailDataController(FocusedRecordIndex,   0));  

      AView   :=   ADetailDC.GridView;  

51取消過濾時移到第一行

      cxCustomData;  

procedure   TYour_Form.AViewDataControllerFilterChanged(Sender:   TObject);  

      Filter:   TcxDataFilterCriteria;  

      with   Sender   as   TcxDataFilterCriteria   do  

          if   IsEmpty   then  

              DataController.FocusedRowIndex   :=   0;  

52排序後移到第一行

可以設定DataController.Options.FocusTopRowAfterSorting   :=   True,也可以使用如下的代碼:  

procedure   TYour_Form.Your_ViewDataControllerSortingChanged(Sender:   TObject);  

      TcxCustomDataController(Sender).FocusedRowIndex   :=   0;  

53判斷目前行是否第一行或最後一行

可以使用DataController的IsBOF,   IsEOF方法,或者:  

<AView>.Controller.Controller.FocusedRow.IsFirst  

<AView>.Controller.Controller.FocusedRow.IsLast

54根據指定值查找記錄

DataController提供了好幾個方法來得到指定值對應的RecordIndex  

對于Bound   View可以使用FindRecordIndexByKeyValue方法

55 編輯和顯示Blob字段

該字段的Properties設定為BlobEdit,并将BlobPaintStyle   屬性設為   bpsText

56得到可見行數

<View>.ViewInfo.VisibleRecordCount

57儲存後的行設定為目前行

const  

      CM_SETFOCUSEDRECORD   =   WM_USER   +   1002;  

type  

      TForm1   =   class(TForm)  

          cxGrid1DBTableView1:   TcxGridDBTableView;  

          cxGrid1Level1:   TcxGridLevel;  

          cxGrid1:   TcxGrid;  

          dxMemData1:   TdxMemData;  

          dxMemData1Field1:   TStringField;  

          dxMemData1Field2:   TIntegerField;  

          DataSource1:   TDataSource;  

          cxGrid1DBTableView1RecId:   TcxGridDBColumn;  

          cxGrid1DBTableView1Field1:   TcxGridDBColumn;  

          cxGrid1DBTableView1Field2:   TcxGridDBColumn;  

          Timer1:   TTimer;  

          CheckBox1:   TCheckBox;  

          procedure   Timer1Timer(Sender:   TObject);  

          procedure   dxMemData1AfterPost(DataSet:   TDataSet);  

          procedure   CheckBox1Click(Sender:   TObject);  

      private  

          procedure   CMSetFocusedRecord(var   Msg:   TMessage);   message   CM_SETFOCUSEDRECORD;  

      public  

          {   Public   declarations   }  

      end;  

      Form1:   TForm1;  

      FocusedIdx:   Integer;  

implementation  

{$R   *.dfm}  

procedure   TForm1.Timer1Timer(Sender:   TObject);  

      dxMemData1.AppendRecord(['',   IntToStr(Random(1000)),   Random(1000)]);  

end;  

procedure   TForm1.dxMemData1AfterPost(DataSet:   TDataSet);  

      PostMessage(Handle, CM_SETFOCUSEDRECORD,   Integer(cxGrid1DBTableView1),   MakeLParam(cxGrid1DBTableView1.Controller.FocusedRowIndex,   cxGrid1DBTableView1.Controller.TopRowIndex));  

procedure   TForm1.CMSetFocusedRecord(var   Msg:   TMessage);  

      TcxGridDBTableView(msg.WParam).Controller.FocusedRowIndex   :=   Msg.LParamLo;  

      TcxGridDBTableView(msg.WParam).Controller.TopRowIndex   :=   Msg.LParamHi;  

procedure   TForm1.CheckBox1Click(Sender:   TObject);  

      Timer1.Enabled   :=   TCheckBox(Sender).Checked;  

end.

58删除記錄并獲得焦點

procedure   TForm1.BtnDeleteClick(Sender:   TObject);  

      FocusedRow,   TopRow:   Integer;  

      View:   TcxGridTableView;  

      DataController:   TcxGridDataController;  

      View   :=   cxGrid1.FocusedView   as   TcxGridTableView;  

      DataController   :=   View.DataController;  

      //   Remember   the   top   row   (the   vertical   scrollbar   position)  

      TopRow   :=   View.Controller.TopRowIndex;  

      //   Remember   the   focused   row(!)   index  

      FocusedRow   :=   DataController.FocusedRowIndex;  

      DataController.DeleteFocused;  

      //   After   deletion   the   same   row   must   be   focused,  

      //   although   it   will   correspond   to   a   different   data   record  

      DataController.FocusedRowIndex   :=   FocusedRow;  

      //   Restore   the   top   row  

      View.Controller.TopRowIndex   :=   TopRow;  

59cxGrid的 TableView 資料排序與對應的資料集同步

COPYRIGHT BY cnCharles, ALL RIGHTS RESERVED.

     //描述: cxGrid的 TableView 資料排序與對應的資料集同步, 該方法主要用于列印時

       //         的排序與所見到的排序保持一緻;

       //參數: @tv: 排序的cxGridTableView

       //說明: @tv: 對應的資料集隻支援 ADOQuery與 ClientDataSet;

       procedure cxGridSortSyncToDataSet(tv: TcxGridDBTableView); overload;

       //描述: 功能同上, 實作代碼一樣, 如果有更改就同步更改

       procedure cxGridSortSyncToDataSet(tv: TcxGridDBBandedTableView); overload;

procedure cxGridSortSyncToDataSet(tv: TcxGridDBTableView);

const

     SortArray: array[soAscending..soDescending] of string = (’ASC’, ’DESC’);

var

     AscFields, DescFields, S, SortOrder: string;

     IndexPrint: string;

     I: integer;

     Index: integer;

     cds: TClientDataSet;

begin

     S := ’’;

     AscFields := ’’;

     DescFields := ’’;

     if tv.SortedItemCount = 0 then

       Exit;

     if tv.DataController.DataSource.DataSet is TADOQuery then begin

       for I := 0 to tv.SortedItemCount - 1 do begin

         SortOrder := SortArray[tv.SortedItems[I].SortOrder];

         if S <> ’’ then

           S := S + ’, ’;

         Index := tv.SortedItems[I].Index;

         S := S + tv.Columns[Index].DataBinding.Field.FieldName + ’ ’ + SortOrder;

       end;

       (tv.DataController.DataSource.DataSet as TADOQuery).Sort := S;

     end else if (tv.DataController.DataSource.DataSet is TClientDataSet)     then begin

       Cds := tv.DataController.DataSource.DataSet as TClientDataSet;

         S := tv.Columns[Index].DataBinding.Field.FieldName +’;’;

         AscFields := AscFields + S;

         if tv.SortedItems[I].SortOrder = soDescending then

           DescFields := DescFields + S;

       if AscFields <> ’’ then

         Delete(AscFields, Length(AscFields), 1); //删除 ;

       if DescFields <> ’’ then

         Delete(DescFields, Length(DescFields), 1);

       IndexPrint := TimeToStr(Now());

       Cds.IndexDefs.Clear;

       cds.AddIndex(IndexPrint, AscFields, [], DescFields);

       cds.IndexName := IndexPrint;

     end;

60cxGRID怎麼周遊已經選擇的單元格

n := cxGrid1DBTableView1.DataController.GetSelectedCount;  

      for   i:=0   to   n   -   1   do  

      begin  

          Index   :=   cxGrid1DBTableView1.DataController.GetSelectedRowIndex(i);  

          if   Index   <   0   then   continue;  

          AccID   :=  

                          cxGrid1DBTableView1.DataController.GetRowvalue(  

                          cxGrid1DBTableView1.DataController.GetRowInfo(Index)  

                          ,0);  

          AccID   :=   dsData.DataSet.FieldByName(’AccountID’).AsString;  

      end;

          AccID   := cxGrid1DBTableView1.DataController.GetRowvalue(  

                  cxGrid1DBTableView1.DataController.GetRowInfo(Index)  

                  ,0);//這裡的0是列的索引,能指定,也可用通過GridView擷取  

61動态設定顯示格式

procedure SetDisplayFormat(ACtrlData: TClientDataSet;

TbView: TcxGridDBTableView);

i: integer;

if ACtrlData.RecordCount <= 0 then Exit;

try

    TbView.ClearItems;

    ACtrlData.First;

    for i := 0 to ACtrlData.RecordCount - 1 do

    begin

      if ACtrlData.FieldByName('SQBF_DisplayInGrid').AsString = '1' then //在表格中顯示

      with TbView.CreateColumn do

      begin

        DataBinding.FieldName := ACtrlData.FieldByName('SQBF_FieldName').AsString;

        Caption := ACtrlData.FieldByName('SQBF_Caption').AsString; //字段中文标題

        Hint := ACtrlData.FieldByName('SQBF_Hint').AsString;

        Width := ACtrlData.FieldByName('SQBF_Width').AsInteger;

        HeaderAlignmentHorz := taCenter;

      ACtrlData.Next;

    end;

except

    on E: Exception do

      SaveLog('設定顯示格式時出錯:' + E.Message);

62給cxGRID加序号列

procedure SetRowNumber(var ASender: TcxGridTableView;

AViewInfo: TcxCustomGridIndicatorItemViewInfo;

   var ACanvas: TcxCanvas; var ADone: boolean);

uses cxLookAndFeelPainters;

procedure SetRowNumber(var ASender: TcxGridTableView; AViewInfo: TcxCustomGridIndicatorItemViewInfo;

   AIndicatorViewInfo: TcxGridIndicatorRowItemViewInfo;

   ATextRect: TRect;

   AFont: TFont;

   AFontTextColor, AColor: TColor;

   AFont := ACanvas.Font;

   AColor := clBtnFace;

   AFontTextColor := clWindowText ;

if (AViewInfo is TcxGridIndicatorHeaderItemViewInfo) then begin

   ATextRect := AViewInfo.Bounds;

   InflateRect(ATextRect, -1, -1);

   ASender.LookAndFeelPainter.DrawHeader(ACanvas, AViewInfo.Bounds,

     ATextRect, [], cxBordersAll, cxbsNormal, taCenter, vaCenter,

     False, False, '序号', AFont, AFontTextColor, AColor);

     ADone := True;

   end ;

if not (AViewInfo is TcxGridIndicatorRowItemViewInfo) then

     Exit;

   ATextRect := AViewInfo.ContentBounds;

   AIndicatorViewInfo := AViewInfo as TcxGridIndicatorRowItemViewInfo;

   ASender.LookAndFeelPainter.DrawHeader(ACanvas, AViewInfo.ContentBounds,

     ATextRect, [], [bBottom, bLeft, bRight], cxbsNormal, taCenter, vaCenter,

     False, False, IntToStr(AIndicatorViewInfo.GridRecord.Index + 1),

     AFont, AFontTextColor, AColor);

   ADone := True;

ASender.LookAndFeelPainter.DrawIndicatorImage(ACanvas,ATextRect, AIndicatorViewInfo.IndicatorKind);

如果你不要行标志的話,你可以不改控件

直接注釋掉這一行: ASender.LookAndFeelPainter.DrawIndicatorImage(ACanvas, ATextRect, AIndicatorViewInfo.IndicatorKind);

要标志的話,在DrawIndicatorImage 從這裡跟進去(Ctrl+左鍵單擊)

在 cxLookAndFeelPainters 單元中作如下修改:

class procedure TcxCustomLookAndFeelPainter.DrawIndicatorImage(ACanvas: TcxCanvas;

   const R: TRect; AKind: TcxIndicatorKind);

   X, Y: Integer;

   if AKind = ikNone then Exit;

   with cxIndicatorImages, R do

   begin

     X := (Left + Right - Width);               //靠右

     Y := (Top + Bottom - Height) div 2;       //居中

   end;

   cxIndicatorImages.Draw(ACanvas.Canvas, X, Y, Ord(AKind) - 1);

注意,我已注明靠右的那一行, 就是去掉 DIV 2 了,

還要改一個地方:

SKIN控件目錄下的dxSkinLookAndFeelPainter單元,找到

TdxSkinLookAndFeelPainter.DrawIndicatorImage 函數

OffsetRect(ARect, (Left + Right - cx div 2) , (Top + Bottom - cy) div 2);

這一行,将 (Left + Right - cx div 2) 改為(Left + Right - cx) 也是去掉 div 2 就是靠右;

修改後: OffsetRect(ARect, (Left + Right - cx) , (Top + Bottom - cy) div 2);

使用

procedure TForm1.cxGrid1DBTableView1CustomDrawIndicatorCell(

   Sender: TcxGridTableView; ACanvas: TcxCanvas;

   AViewInfo: TcxCustomGridIndicatorItemViewInfo; var ADone: Boolean);

     SetRowNumber(Sender,AviewInfo,ACanvas,ADone);

另外序号列的列寬最好改為25以上!

效果圖:

cxGrid使用彙總4

63 cxGrid自帶過濾後資料也資料集同步

在cxGrid的View Filter事件的OnBeforeChange中寫代碼就可以了.

procedure TForm1.tvcxgd1DBTableView1DataControllerFilterBeforeChange( Sender: TcxDBDataFilterCriteria; ADataSet: TDataSet; const AFilterText: String); begin //這裡可以增加資料集控件的filter:=false; //如:adoquery.filter:=false; //如果使用的是cxgrid的漢化版本,可以将AFilterText中的 中文等于,小于 替換成 = <等 //adoquery.filter:=替換了中文的AFilterText; ShowMessage(AFilterText); end; 寫了上述步驟後可以在tvcxgd1DBTableView1DataControllerFilterChanged寫adoquery.filter:=true; 這樣就起到了cxgrid過濾後的資料同步到adoquery的資料集,列印的時候就可以列印出符合要求的記錄了。

繼續閱讀