码迷,mamicode.com
首页 > Windows程序 > 详细

Delphi CxGrid 汇总(4)

时间:2018-03-04 11:45:50      阅读:244      评论:0      收藏:0      [点我收藏+]

标签:options   lis   pen   col   GridView   filter   btn   erb   表格   

1.     CxGrid汇总功能

① OptionsView-Footer设置为True,显示页脚   ② CxGrid的Summary选项卡定义要汇总的列和字段名及汇总方式,Footer选项卡定义单个汇总,Default For Groups定义按组汇总。OptionsView-GroupFooters设置为gfAlwaysVisible则显示按组汇总。设置后界面如图。

   技术分享图片

技术分享图片

2.       CxGrid的样式设置

 当设置了Kind时,NativeStyle必须设置为False,如果指定了SkinName则Kind属性失效。

  技术分享图片

下图是设置skinname为MoneyTwins后效果

 

 技术分享图片 

4.       取某个单元格的值

 Cxgrid.DataController.Values[i,j]

5.       列操作,选择CxGrid控件后,点击“Customize”新建一列,在Columns集合中选中新建的列,选择propertites属性可以设置该列的显示形式。下面介绍常用的几个

①     Properties选择CheckBox,则该列显示一个复选框,如下:

 技术分享图片判断是否选中 if  Cxgrid.DataController.Values[i,j]=’1’   选中

②     Properties选择ButtonEdit,并对该列的属性编辑器设置如下属性Buttons属性添加按钮项,对按钮项设置可以设置kind属性定义按钮样式;ViewStyle属性设置为vsButtonsOnly,Options-ShowEditButton设置为isebAlways。可以编写点击事件如下:

procedure TForm1.cxgrdbtblvwGrid1DBTableView1Column1PropertiesButtonClick(

  Sender: TObject; AButtonIndex: Integer);

begin

  ShowMessage(‘aaa‘);

end;

③ImageComboBox,可以关联一个imagelist,显示图片。如下关联imagelist后效果。

 

 技术分享图片

 

6.动态添加列和记录行

var

Column:   TcxGridColumn;

i:integer;

acount:integer;

 

begin

    Column:= cxgrd1TableView1.CreateColumn;

    Column.Caption   :=   ‘Test ‘;

    cxgrd1TableView1.DataController.AppendRecord;

    cxgrd1TableView1.DataController.Values[0,   0]   :=   ‘ABC ‘;

cxgrd1TableView1.DataController.Post;

 

//添加多条记录

for i:=1 to 4 do

  begin

    acount:=cxgrd1TableView1.DataController.RecordCount;

    cxgrd1TableView1.DataController.AppendRecord;

    cxgrd1TableView1.DataController.Values[acount,   0]   :=IntToStr(i*1);

    cxgrd1TableView1.DataController.Post();

  end;

end;

 

  //删除记录

   cxgrd1TableView1.DataController.DeleteRecord(0);

end;

 

50 保存/恢复带汇总行的布局

<TableView>.StoreToIniFile(‘c:\Grid.ini‘, True, [gsoUseSummary]);
<GridView>.RestoreFromIniFile(<inifilename>,True,False {or True, optional},[gsoUseSummary]);

****************************************************************************
51 取消过滤时移到第一行
解决:
uses  
      cxCustomData;  
   
procedure   TYour_Form.AViewDataControllerFilterChanged(Sender:   TObject);  
var  
      Filter:   TcxDataFilterCriteria;  
begin  
      with   Sender   as   TcxDataFilterCriteria   do  
          if   IsEmpty   then  
              DataController.FocusedRowIndex   :=   0;  
end;
****************************************************************************
52 排序后移到第一行
解决:
可以设置DataController.Options.FocusTopRowAfterSorting   :=   True,也可以使用如下的代码:  
   
uses  
      cxCustomData;  
   
procedure   TYour_Form.Your_ViewDataControllerSortingChanged(Sender:   TObject);  
begin  
      TcxCustomDataController(Sender).FocusedRowIndex   :=   0;  
end;
****************************************************************************
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 保存后的行设置为当前行
解决:

[delphi] view plaincopy
 
  1. const    
  2.       CM_SETFOCUSEDRECORD   =   WM_USER   +   1002;    
  3.      
  4. type    
  5.       TForm1   =   class(TForm)    
  6.           cxGrid1DBTableView1:   TcxGridDBTableView;    
  7.           cxGrid1Level1:   TcxGridLevel;    
  8.           cxGrid1:   TcxGrid;    
  9.           dxMemData1:   TdxMemData;    
  10.           dxMemData1Field1:   TStringField;    
  11.           dxMemData1Field2:   TIntegerField;    
  12.           DataSource1:   TDataSource;    
  13.           cxGrid1DBTableView1RecId:   TcxGridDBColumn;    
  14.           cxGrid1DBTableView1Field1:   TcxGridDBColumn;    
  15.           cxGrid1DBTableView1Field2:   TcxGridDBColumn;    
  16.           Timer1:   TTimer;    
  17.           CheckBox1:   TCheckBox;    
  18.           procedure   Timer1Timer(Sender:   TObject);    
  19.           procedure   dxMemData1AfterPost(DataSet:   TDataSet);    
  20.           procedure   CheckBox1Click(Sender:   TObject);    
  21.       private    
  22.           procedure   CMSetFocusedRecord(var   Msg:   TMessage);   message   CM_SETFOCUSEDRECORD;    
  23.       public    
  24.           {   Public   declarations   }    
  25.       end;    
  26.      
  27. var    
  28.       Form1:   TForm1;    
  29.       FocusedIdx:   Integer;    
  30.      
  31.      
  32. implementation    
  33.      
  34. {$R   *.dfm}    
  35.      
  36. procedure   TForm1.Timer1Timer(Sender:   TObject);    
  37. begin    
  38.       dxMemData1.AppendRecord([‘‘,   IntToStr(Random(1000)),   Random(1000)]);    
  39. end;    
  40.      
  41. procedure   TForm1.dxMemData1AfterPost(DataSet:   TDataSet);    
  42. begin    
  43.       PostMessage(Handle, CM_SETFOCUSEDRECORD,   Integer(cxGrid1DBTableView1),   MakeLParam(cxGrid1DBTableView1.Controller.FocusedRowIndex,   cxGrid1DBTableView1.Controller.TopRowIndex));    
  44. end;    
  45.      
  46. procedure   TForm1.CMSetFocusedRecord(var   Msg:   TMessage);    
  47. begin    
  48.       TcxGridDBTableView(msg.WParam).Controller.FocusedRowIndex   :=   Msg.LParamLo;    
  49.       TcxGridDBTableView(msg.WParam).Controller.TopRowIndex   :=   Msg.LParamHi;    
  50. end;    
  51.      
  52. procedure   TForm1.CheckBox1Click(Sender:   TObject);    
  53. begin    
  54.       Timer1.Enabled   :=   TCheckBox(Sender).Checked;    
  55. end;    
  56.      
  57. end.  
  58. ****************************************************************************  
  59. 58 删除记录并获得焦点  
  60. 解决:  
  61. procedure   TForm1.BtnDeleteClick(Sender:   TObject);    
  62. var    
  63.       FocusedRow,   TopRow:   Integer;    
  64.       View:   TcxGridTableView;    
  65.       DataController:   TcxGridDataController;    
  66. begin    
  67.       View   :=   cxGrid1.FocusedView   as   TcxGridTableView;    
  68.       DataController   :=   View.DataController;    
  69.      
  70.       //   Remember   the   top   row   (the   vertical   scrollbar   position)    
  71.       TopRow   :=   View.Controller.TopRowIndex;    
  72.       //   Remember   the   focused   row(!)   index    
  73.       FocusedRow   :=   DataController.FocusedRowIndex;    
  74.      
  75.       DataController.DeleteFocused;    
  76.      
  77.       //   After   deletion   the   same   row   must   be   focused,    
  78.       //   although   it   will   correspond   to   a   different   data   record    
  79.       DataController.FocusedRowIndex   :=   FocusedRow;    
  80.       //   Restore   the   top   row    
  81.       View.Controller.TopRowIndex   :=   TopRow;    
  82. end;  
  83. ****************************************************************************  
  84. 59 cxGrid的 TableView 数据排序与对应的数据集同步  
  85. 解决:  
  86. COPYRIGHT BY cnCharles, ALL RIGHTS RESERVED.  
  87. delphi群: 16497064, blog: http://hi.baidu.com/cnCharles     
  88.   
  89.      //描述: cxGrid的 TableView 数据排序与对应的数据集同步, 该方法主要用于打印时  
  90.        //         的排序与所见到的排序保持一致;  
  91.        //参数: @tv: 排序的cxGridTableView  
  92.        //说明: @tv: 对应的数据集只支持 ADOQuery与 ClientDataSet;  
  93.        procedure cxGridSortSyncToDataSet(tv: TcxGridDBTableView); overload;  
  94.   
  95.        //描述: 功能同上, 实现代码一样, 如果有更改就同步更改  
  96.        procedure cxGridSortSyncToDataSet(tv: TcxGridDBBandedTableView); overload;  
  97.   
  98. procedure cxGridSortSyncToDataSet(tv: TcxGridDBTableView);  
  99. const  
  100.      SortArray: array[soAscending..soDescending] of string = (’ASC’, ’DESC’);  
  101. var  
  102.      AscFields, DescFields, S, SortOrder: string;  
  103.      IndexPrint: string;  
  104.      I: integer;  
  105.      Index: integer;  
  106.      cds: TClientDataSet;  
  107. begin  
  108.      S := ’’;  
  109.      AscFields := ’’;  
  110.      DescFields := ’’;  
  111.      if tv.SortedItemCount = then  
  112.        Exit;  
  113.      if tv.DataController.DataSource.DataSet is TADOQuery then begin  
  114.        for I := to tv.SortedItemCount - do begin  
  115.          SortOrder := SortArray[tv.SortedItems[I].SortOrder];  
  116.          if S <> ’’ then  
  117.            S := S + ’, ’;  
  118.          Index := tv.SortedItems[I].Index;  
  119.          S := S + tv.Columns[Index].DataBinding.Field.FieldName + ’ ’ + SortOrder;  
  120.        end;  
  121.        (tv.DataController.DataSource.DataSet as TADOQuery).Sort := S;  
  122.      end else if (tv.DataController.DataSource.DataSet is TClientDataSet)     then begin  
  123.        Cds := tv.DataController.DataSource.DataSet as TClientDataSet;  
  124.        for I := to tv.SortedItemCount - do begin  
  125.          Index := tv.SortedItems[I].Index;  
  126.          S := tv.Columns[Index].DataBinding.Field.FieldName +’;’;  
  127.          AscFields := AscFields + S;  
  128.          if tv.SortedItems[I].SortOrder = soDescending then  
  129.            DescFields := DescFields + S;  
  130.        end;  
  131.        if AscFields <> ’’ then  
  132.          Delete(AscFields, Length(AscFields), 1); //删除 ;  
  133.   
  134.        if DescFields <> ’’ then  
  135.          Delete(DescFields, Length(DescFields), 1);  
  136.        IndexPrint := TimeToStr(Now());  
  137.        Cds.IndexDefs.Clear;  
  138.        IndexPrint := TimeToStr(Now());  
  139.        cds.AddIndex(IndexPrint, AscFields, [], DescFields);  
  140.        cds.IndexName := IndexPrint;  
  141.      end;  
  142. end;  
  143. ****************************************************************************  
  144. 60 cxGRID怎么遍历已经选择的单元格  
  145. 解决:  
  146. n := cxGrid1DBTableView1.DataController.GetSelectedCount;    
  147.         
  148.       for   i:=0   to   n   -   1   do    
  149.       begin    
  150.           Index   :=   cxGrid1DBTableView1.DataController.GetSelectedRowIndex(i);    
  151.           if   Index   <   0   then   continue;    
  152.           AccID   :=    
  153.                           cxGrid1DBTableView1.DataController.GetRowvalue(    
  154.                           cxGrid1DBTableView1.DataController.GetRowInfo(Index)    
  155.                           ,0);    
  156.           AccID   :=   dsData.DataSet.FieldByName(’AccountID’).AsString;    
  157.       
  158.       end;  
  159. n := cxGrid1DBTableView1.DataController.GetSelectedCount;    
  160.       for   i:=0   to   n   -   1   do    
  161.       begin    
  162.           Index   :=   cxGrid1DBTableView1.DataController.GetSelectedRowIndex(i);    
  163.           if   Index   <   0   then   continue;    
  164.           AccID   := cxGrid1DBTableView1.DataController.GetRowvalue(    
  165.                   cxGrid1DBTableView1.DataController.GetRowInfo(Index)    
  166.                   ,0);//这里的0是列的索引,能指定,也可用通过GridView获取    
  167.       
  168.       end;  
  169. ****************************************************************************  
  170. 61 动态设置显示格式  
  171. 解决:  
  172. procedure SetDisplayFormat(ACtrlData: TClientDataSet;  
  173. TbView: TcxGridDBTableView);  
  174. var  
  175. i: integer;  
  176. begin  
  177. if ACtrlData.RecordCount <= then Exit;  
  178. try  
  179.     TbView.ClearItems;  
  180.     ACtrlData.First;  
  181.     for i := to ACtrlData.RecordCount - do  
  182.     begin  
  183.       if ACtrlData.FieldByName(‘SQBF_DisplayInGrid‘).AsString = ‘1‘ then //在表格中显示  
  184.       with TbView.CreateColumn do  
  185.       begin  
  186.         DataBinding.FieldName := ACtrlData.FieldByName(‘SQBF_FieldName‘).AsString;  
  187.         Caption := ACtrlData.FieldByName(‘SQBF_Caption‘).AsString; //字段中文标题  
  188.         Hint := ACtrlData.FieldByName(‘SQBF_Hint‘).AsString;  
  189.         Width := ACtrlData.FieldByName(‘SQBF_Width‘).AsInteger;  
  190.         HeaderAlignmentHorz := taCenter;  
  191.       end;  
  192.       ACtrlData.Next;  
  193.     end;  
  194. except  
  195.     on E: Exception do  
  196.       SaveLog(‘设置显示格式时出错:‘ + E.Message);  
  197. end;  
  198. end;  

****************************************************************************
62 给cxGRID加序号列
解决:

[delphi] view plaincopy
 
  1. procedure SetRowNumber(var ASender: TcxGridTableView;  
  2. AViewInfo: TcxCustomGridIndicatorItemViewInfo;  
  3.    var ACanvas: TcxCanvas; var ADone: boolean);  
  4.   
  5. uses cxLookAndFeelPainters;  
  6.   
  7. procedure SetRowNumber(var ASender: TcxGridTableView; AViewInfo: TcxCustomGridIndicatorItemViewInfo;  
  8.    var ACanvas: TcxCanvas; var ADone: boolean);  
  9. var  
  10.    AIndicatorViewInfo: TcxGridIndicatorRowItemViewInfo;  
  11.    ATextRect: TRect;  
  12.    AFont: TFont;  
  13.    AFontTextColor, AColor: TColor;  
  14. begin  
  15.    AFont := ACanvas.Font;  
  16.    AColor := clBtnFace;  
  17.    AFontTextColor := clWindowText ;  
  18. if (AViewInfo is TcxGridIndicatorHeaderItemViewInfo) then begin  
  19.    ATextRect := AViewInfo.Bounds;  
  20.    InflateRect(ATextRect, -1, -1);  
  21.       
  22.    ASender.LookAndFeelPainter.DrawHeader(ACanvas, AViewInfo.Bounds,  
  23.      ATextRect, [], cxBordersAll, cxbsNormal, taCenter, vaCenter,  
  24.      False, False, ‘序号‘, AFont, AFontTextColor, AColor);  
  25.      ADone := True;  
  26.    end ;  
  27. if not (AViewInfo is TcxGridIndicatorRowItemViewInfo) then  
  28.      Exit;  
  29.    ATextRect := AViewInfo.ContentBounds;  
  30.    AIndicatorViewInfo := AViewInfo as TcxGridIndicatorRowItemViewInfo;  
  31.    InflateRect(ATextRect, -1, -1);  
  32.    ASender.LookAndFeelPainter.DrawHeader(ACanvas, AViewInfo.ContentBounds,  
  33.      ATextRect, [], [bBottom, bLeft, bRight], cxbsNormal, taCenter, vaCenter,  
  34.      False, False, IntToStr(AIndicatorViewInfo.GridRecord.Index + 1),  
  35.      AFont, AFontTextColor, AColor);  
  36.    ADone := True;  
  37. ASender.LookAndFeelPainter.DrawIndicatorImage(ACanvas,ATextRect, AIndicatorViewInfo.IndicatorKind);  
  38. end;  
  39.   
  40. 如果你不要行标志的话,你可以不改控件  
  41. 直接注释掉这一行: ASender.LookAndFeelPainter.DrawIndicatorImage(ACanvas, ATextRect, AIndicatorViewInfo.IndicatorKind);  
  42. 要标志的话,在DrawIndicatorImage 从这里跟进去(Ctrl+左键单击)  
  43. 在 cxLookAndFeelPainters 单元中作如下修改:  
  44.   
  45. class procedure TcxCustomLookAndFeelPainter.DrawIndicatorImage(ACanvas: TcxCanvas;  
  46.    const R: TRect; AKind: TcxIndicatorKind);  
  47. var  
  48.    X, Y: Integer;  
  49. begin  
  50.    if AKind = ikNone then Exit;  
  51.    with cxIndicatorImages, R do  
  52.    begin  
  53.      X := (Left + Right - Width);               //靠右  
  54.      Y := (Top + Bottom - Height) div 2;       //居中  
  55.    end;  
  56.    cxIndicatorImages.Draw(ACanvas.Canvas, X, Y, Ord(AKind) - 1);  
  57. end;  
  58.   
  59. 注意,我已注明靠右的那一行, 就是去掉 DIV 2 了,  
  60. 还要改一个地方:  
  61. SKIN控件目录下的dxSkinLookAndFeelPainter单元,找到  
  62. TdxSkinLookAndFeelPainter.DrawIndicatorImage 函数  
  63. 的  
  64. OffsetRect(ARect, (Left + Right - cx div 2) , (Top + Bottom - cy) div 2);  
  65. 这一行,将 (Left + Right - cx div 2) 改为(Left + Right - cx) 也是去掉 div 2 就是靠右;  
  66. 修改后: OffsetRect(ARect, (Left + Right - cx) , (Top + Bottom - cy) div 2);  
  67.   
  68. 使用  
  69. procedure TForm1.cxGrid1DBTableView1CustomDrawIndicatorCell(  
  70.    Sender: TcxGridTableView; ACanvas: TcxCanvas;  
  71.    AViewInfo: TcxCustomGridIndicatorItemViewInfo; var ADone: Boolean);  
  72. begin  
  73.      SetRowNumber(Sender,AviewInfo,ACanvas,ADone);  
  74. end;  
  75.   
  76. 另外序号列的列宽最好改为25以上!  
  77. 效果图:  

****************************************************************************
63 cxGrid自带过滤后数据也数据集同步
解决:
在cxGrid的View Filter事件的OnBeforeChange中写代码就可以了.

[delphi] view plaincopy
 
  1. procedure TForm1.tvcxgd1DBTableView1DataControllerFilterBeforeChange( Sender: TcxDBDataFilterCriteria; ADataSet: TDataSet; const AFilterText: String);   
  2.   
  3. begin   
  4.   
  5. //这里可以增加数据集控件的  
  6.   
  7. filter:=false; //如:adoquery.filter:=false; //如果使用的是cxgrid的汉化版本,可以将AFilterText中的中文等于,小于 替换成 = <等 //adoquery.filter:=替换了中文的AFilterText; ShowMessage(AFilterText); end; 写了上述步骤后可以在tvcxgd1DBTableView1DataControllerFilterChanged写 adoquery.filter:=true; 这样就起到了cxgrid过滤后的数据同步到adoquery  

Delphi CxGrid 汇总(4)

标签:options   lis   pen   col   GridView   filter   btn   erb   表格   

原文地址:https://www.cnblogs.com/westsoft/p/8504045.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!