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后效果。

.动态添加列和记录行

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
plain
copy

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

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

62 给cxGRID加序号列

解决:

[delphi] view
plain
copy

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

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

63 cxGrid自带过滤后数据也数据集同步

解决:

在cxGrid的View Filter事件的OnBeforeChange中写代码就可以了.

[delphi] view
plain
copy

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

Delphi CxGrid 汇总(4)的更多相关文章

  1. Delphi CxGrid 汇总(2)

    17. 怎样设计多表头的cxGrid? 解决:cxGrid可以解决如下的表头: --------------------------------- | 说明1 | 说明2 | ------------ ...

  2. Delphi CxGrid 汇总(3)

    列   解决:       <aColumn>.GroupIndex   :=   -1;         <aColumn>.Visible   :=   True; *** ...

  3. delphi cxgrid 使用方法

    delphi cxgrid 使用方法1.绑定数据 方法 cxGrid1DBTableView1.DataController.DataSource:=DataSource12.去掉"Drag ...

  4. 关于Delphi cxGrid主从表中从表只能编辑第一条记录的问题

    在Delphi cxGrid主从表中从表只能编辑第一条记录,这个问题是由于设置主从关联字段错误造成的. 从表DBtableView2的keyfieldnames,DetailKeyFieldNames ...

  5. Delphi cxGrid使用汇总(一)

    1. 去掉cxGrid中台头的Box解决:在tableview1的ptionsview的groupbybox=false; 2.统计功能解决:(1) tableview1. tableview1的op ...

  6. Delphi cxGrid –--> RecordIndex out of Range

    delphi 导出数据时常常出现这样一个错误 < RecordIndex out of Range > 处理办法: 设定 cxGridDBTableView 的 GridModeBuffe ...

  7. Delphi - cxGrid颜色显示相关设置

    1:单元格的值满足某个条件时,该单元格所在整行颜色设置整行字体设置 选中cxGridDBTableView,单击F11调出属性配置面板,在Events中双击OnCustomDrawCell后双击编辑重 ...

  8. Delphi - cxGrid内容xlsx、xls、csv格式导出

    .xls格式导出,uses中添加cxGridExportLink 代码如下: function SaveToExcel(gridMain: TcxGrid; FileName: string): st ...

  9. Delphi - cxGrid设定合并单元格

    在cxGrid中选中需要合并的字段,单击F11调出属性控制面板,展开Options,设置CellMerging的Value为True.

随机推荐

  1. FAQ: Automatic Statistics Collection (文档 ID 1233203.1)

    In this Document   Purpose   Questions and Answers   What kind of statistics do the Automated tasks ...

  2. Grid Infrastructure Single Client Access Name (SCAN) Explained (文档 ID 887522.1)

    APPLIES TO: Oracle Database - Enterprise Edition - Version 11.2.0.1 and laterExalogic Elastic Cloud ...

  3. document.write('<script type=\"text/javascript\"><\/script>')

    document.write('<script type=\"text/javascript\"><\/script>')

  4. Python实现顺时钟回形矩阵

    无意间在网上看到了一个面试题是,写出一个回形矩阵.实现的效果大致如下: [ 1,   2,   3,   4, 5] [16, 17, 18, 19, 6] [15, 24, 25, 20, 7] [ ...

  5. selenium和Firefox版本不兼容

    selenium8.py coding = utf-8 from selenium import webdriver driver = webdriver.Firefox() driver.get(' ...

  6. Android开发-API指南-Content Provider基础

    Content Provider Basics 英文原文:http://developer.android.com/guide/topics/providers/content-provider-ba ...

  7. C++ 什么是句柄?为什么会有句柄?HANDLE

    出处:http://www.cppblog.com/mymsdn/archive/2009/02/19/handle-in-windows.html 从广义上,能够从一个数值拎起一大堆数据的东西都可以 ...

  8. Oracle建立表空间和用户

    Oracle建立表空间和用户 建立表空间和用户的步骤: 用户 建立:create user 用户名 identified by "密码"; 授权:grant create sess ...

  9. telnet localhost 8089 ==》》命令使用

    GET /ccc/abc.html HTTP/1.1 host:localhost     客户端连上web服务器后,若想获得web服务器中的某个web资源,需遵守一定的通讯格式, HTTP协议用于定 ...

  10. javascript设计模式-享元模式

    享元模式采用一个共享来有效的支持大量细小粒度的对象,避免大量有相同内容的类的开销(如内存耗费),共享一个元类. 应用场景:页面存在大量的资源密集型对象:他们具备一些共性,可以分离出公共操作的数据. 一 ...