Delphi CxGrid 汇总(3)

列  
解决:
      <aColumn>.GroupIndex   :=   -1;  
      <aColumn>.Visible   :=   True;
****************************************************************************
39 保存修改到数据库
解决:
procedure   <aForm>.FormClose(Sender:   TObject;   var   Action:   TCloseAction);  
begin  
if   (<aGrid>.FocusedView <> nil)
and   (<aGrid>.FocusedView.DataController.EditState <> []) then  
          <aGrid>.FocusedView.DataController.Post;  
end;
****************************************************************************
40 设置内置右键菜单
解决:
内置右键菜单包括二个菜单:cxGridStdHeaderMenu,   TcxGridStdFooterMenu  

[delphi] view plaincopy
  1. uses   cxGridStdPopupMenu;    
  2.      
  3. procedure   TForm1.cxGridPopupMenu1Popup(ASenderMenu:   TComponent;    
  4.       AHitTest:   TcxCustomGridHitTest;   X,   Y:   Integer;   var   AllowPopup:   Boolean);    
  5. begin    
  6.       if   ASenderMenu   is   TcxGridStdHeaderMenu   then    
  7.           TcxGridStdHeaderMenu(ASenderMenu).OnPopup   :=   StdHeaderMenuPopup;    
  8. end;    
  9.      
  10. procedure   TForm1.StdHeaderMenuPopup(Sender:   TObject);    
  11. var    
  12.       I:   Integer;    
  13. begin    
  14.       with   TcxGridStdHeaderMenu(Sender).Items   do    
  15.           for   I   :=   0   to   Count   -   1   do    
  16.               if   Items[I].Caption   =   'Group   By   Box'   then    
  17.               begin    
  18.                   Items[I].Enabled   :=   False;    
  19.                   System.Break;    
  20.               end    
  21. end;  

****************************************************************************
41 得到选中记录的值
解决:

[delphi] view plaincopy
  1. 1)   View.DataController.DataModeController.GridMode   =   False时    
  2.      
  3.       RecIdx   :=   View.Controller.SelectedRecords[i].RecordIndex;    
  4.       ColIdx   :=   View.DataController.GetItemByFieldName(AFieldName).Index;    
  5.       OutputVal   :=   View.DataController.Values[RecIdx,   ColIdx];    
  6.      
  7.       //RecID   :=   View.DataController.GetRecordId(RecIdx);    
  8.       //OutputVal   :=   ADataSet.Lookup(View.DataController.KeyFieldNames,   RecID,   AFieldName);    
  9.      
  10. 2)   View.DataController.DataModeController.GridMode   =   True时    
  11.       Bkm   :=   View.DataController.GetSelectedBookmark(ASelectedRecordIndex);    
  12.       if   ADataSet.BookmarkValid(TBookmark(Bkm))   then    
  13.       begin    
  14.           ADataSet.Bookmark   :=   TBookmark(Bkm);    
  15.           OutputVal   :=   ADataSet.FieldByName(AFieldName).Value;    
  16.       end;    
  17.      
  18.       View.BeginUpdate;    
  19.       View.DataController.BeginLocate;    
  20.       try    
  21.           //   make   changes   here…    
  22.       finally    
  23.           View.DataController.EndLocate;    
  24.           View.EndUpdate;    
  25.       end;  

****************************************************************************
42 在GridMode禁用内置的右键Footer菜单
解决:
uses   cxGridStdPopupMenu;  
   
procedure   cxGridPopupMenuOnPopup(...)  
begin  
      if   (ASenderMenu   is   TcxGridStdFooterMenu)   and  
              <GridView>.DataController.DataModeController.GridMode   then  
          AllowPopup   :=   False;  
end;
****************************************************************************
43 主从表任何时候只能展开一个组
解决:

[delphi] view plaincopy
  1. procedure   TForm1.ADetailDataControllerCollapsin(  ADataController:  TcxCustomDataController;    
  2.   
  3. ARecordIndex:   Integer;  var   AAllow:   Boolean);    
  4. var    
  5.       I:   Integer;    
  6.       C:   Integer;    
  7. begin    
  8.       AAllow   :=   False;    
  9.       C   :=   0;    
  10.       for   I   :=   0   to   ADataController.RecordCount   -   1   do    
  11.       begin    
  12.           if   ADataController.GetDetailExpanding(I)   then    
  13.               Inc(C);    
  14.           if   C   >   1   then    
  15.               AAllow   :=   True;    
  16.         end;    
  17. end;    
  18.      
  19. procedure   TForm1.ADetailDataControllerExpanding(    
  20.       ADataController:   TcxCustomDataController;   ARecordIndex:   Integer;    
  21.       var   AAllow:   Boolean);    
  22. begin    
  23.       ADataController.CollapseDetails;    
  24. end;    
  25.      
  26. procedure   TForm1.FormCreate(Sender:   TObject);    
  27. begin        cxGrid1DBTableView1.DataController.OnDetailExpanding:=ADetailDataControllerExpanding;         cxGrid1DBTableView1.DataController.OnDetailCollapsing:=ADetailDataControllerCollapsing;    
  28. end;  
  29. ****************************************************************************  
  30. 44 动态创建层次(Level)和视图(View)  
  31. 解决:  
  32. var        
  33.       Grid:   TcxGrid;        
  34.       Level:   TcxGridLevel;        
  35.       View:   TcxGridDBTableView;        
  36. begin    
  37.       //   Creates   a   Grid   instance    
  38.       Grid   :=   TcxGrid.Create(SomeOwner);        
  39.       Grid.Parent   :=   SomeParent;        
  40.       //   Creates   a   Level    
  41.       Level   :=   Grid.Levels.Add;        
  42.       Level.Name   :=   'SomeLevelName';    
  43.       //   Creates   a   View    
  44.       View   :=   Grid.CreateView(TcxGridDBTableView)   as   TcxGridDBTableView;        
  45.       View.Name   :=   'SomeViewName';    
  46.       //   …   and   binds   it   to   the   Level    
  47.       Level.GridView   :=   View;        
  48.       //   Hooks   up   the   View   to   the   data    
  49.       View.DataController.DataSource   :=   SomeDataSource;        
  50.       //   …   and   creates   all   columns    
  51.       View.DataController.CreateAllItems;        
  52. end;  

****************************************************************************
45 获得Group   Footer合计行对应的记录
解决:

[delphi] view plaincopy
  1. procedure   TForm1.cxGrid1DBTableView1CustomDrawFooterCell(    
  2.       Sender:   TcxGridTableView;   ACanvas:   TcxCanvas;    
  3.       AViewInfo:   TcxGridColumnHeaderViewInfo;   var   ADone:   Boolean);    
  4. var    
  5.       ALevel,   ADataGroupIndex:   Integer;    
  6.       AGridRecord,   AGroupRecord:   TcxCustomGridRecord;    
  7. begin    
  8.       if   AViewInfo   is   TcxGridRowFooterCellViewInfo   and    //   Row   footer    
  9.             (TcxGridDBColumn(AViewInfo.Column).DataBinding.FieldName   =   'Area')   then     //   Area   column    
  10.    begin    
  11.         AGridRecord:=   TcxGridRowFooterCellViewInfo(AViewInfo).GridRecord;    
  12.         ALevel:= TcxGridRowFooterCellViewInfo(AViewInfo).Container.GroupLevel;    
  13. ADataGroupIndex:=Sender.DataController.Groups.DataGroupIndexByRowIndex[AGridRecord.Index];    
  14.          if   ADataGroupIndex   <>   -1   then    
  15.          begin    
  16.             AGroupRecord   :=   AGridRecord;    
  17.             while   AGroupRecord.Level   <>   ALevel   do    
  18.             AGroupRecord   :=   AGroupRecord.ParentRecord;    
  19.             AViewInfo.Text   :=   AGroupRecord.DisplayTexts[0];    
  20.          end;    
  21.      end;    
  22. end;  

****************************************************************************
46 访问过滤之后的记录
解决:
var  
      I:   Integer;  
begin  
      Memo1.Lines.Clear;  
      with   cxGrid1DBTableView1.DataController   do  
          for   I   :=   0   to   FilteredRecordCount   -   1   do  
              Memo1.Lines.Add(DisplayTexts[FilteredRecordIndex[I],   0]);  
end;

****************************************************************************
47 获得单元的Font
解决:
cxGrid1DBTableView1.ViewInfo.RecordsViewInfo.Items[1].GetCellViewInfoByItem(  
      cxGrid1DBTableView1Company).EditViewInfo.Font;
****************************************************************************
48 根据Level名称找到Level对象
解决:

[delphi] view plaincopy
  1. function   GetLevelByName(AGrid:   TcxGrid;   ALevelName:   string):   TcxGridLevel;    
  2.      
  3.       function   LoopThroughLevels(ALevel:   TcxGridLevel;   ALevelName:   string):   TcxGridLevel;    
  4.       var    
  5.           I:   Integer;    
  6.       begin    
  7.           Result   :=   nil;    
  8.           for   I   :=   0   to   ALevel.Count   -   1   do    
  9.           begin    
  10.               if   ALevel[I].Name   =   ALevelName   then    
  11.               begin    
  12.                   Result   :=   ALevel[I];    
  13.                   Exit;    
  14.               end;    
  15.               if   ALevel[I].Count   >   0   then    
  16.               begin    
  17.                   Result   :=   LoopThroughLevels(ALevel[I],   ALevelName);    
  18.                   if   Result   <>   nil   then    
  19.                       Exit;    
  20.               end;    
  21.           end;    
  22.       end;    
  23.      
  24. var    
  25.       I:   Integer;    
  26. begin    
  27.       Result   :=   nil;    
  28.       for   I   :=   0   to   AGrid.Levels.Count   -   1   do    
  29.       begin    
  30.           if   AGrid.Levels[I].Name   =   ALevelName   then    
  31.           begin    
  32.               Result   :=   AGrid.Levels[I];    
  33.               Exit;    
  34.           end;    
  35.           if   AGrid.Levels[I].Count   >   0   then    
  36.           begin    
  37.               Result   :=   LoopThroughLevels(AGrid.Levels[I],   ALevelName);    
  38.               if   Result   <>   nil   then    
  39.                   Exit;    
  40.           end;    
  41.       end;    
  42. end;  

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

 49 指定Filter   Builder打开/保存过滤文件的默认路径
解决:
uses  
      ...,   cxFilterControlDialog;  
   
procedure   TForm.GridView1FilterControlDialogShow(  
      Sender:   TObject);  
begin  
      TfmFilterControlDialog(Sender).OpenDialog.InitialDir   :=   'D:/'  
end;

原文地址:https://www.cnblogs.com/jupt/p/3922932.html