Delphi CxGrid 汇总(3)

本文汇总了Delphi CxGrid组件的常见操作,包括激活和隐藏内置编辑控件、移除分组列、保存数据到数据库、设置右键菜单、获取选中记录值、禁用GridMode下的Footer菜单、以及过滤记录后的访问等实用技巧。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

35 在当前View插入记录 
解决:
使用FocusedView属性得到当前焦点View,用View.DataController得到对应的Data   Controller, 
之后使用Data   Controller的方法来操作数据: 
-   Append 
-   Insert 
-   Post 
-   Cancel 
-   DeleteFocused 
-   DeleteSelection 
  
示例: 
var 
      ARecIndex:   Integer; 
… 
      View.DataController.Append; 
      ARecIndex   :=   View.DataController.FocusedRecordIndex; 
      View.DataController.Values[ARecIndex,   SomeItemIndex]   :=   SomeValue; 
      View.DataController.Post; 
  
另外一种方法是使用View.DataController.DataSource.DataSet得到底层数据集后,再用数据集的方法来操作数据。

 

****************************************************************************
36 激活内置编辑控件
解决:
    1)   <aView>.Controller.EditingController.ShowEdit(<aColumn>); 
    2)   <aView>.Controller.EditingController.StartEditShowingTimer(<aColumn>); 
    3)   <aView>.Controller.EditingItem   :=   <aColumn>; 
    4)   <aColumn>.Editing   :=   True;
****************************************************************************
37 隐藏内置编辑控件
解决:
<aView>.Controller.EditingController.HideEdit(True);
****************************************************************************
38 移除一个分组列 
解决:
      <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 

uses   cxGridStdPopupMenu;  
   
procedure   TForm1.cxGridPopupMenu1Popup(ASenderMenu:   TComponent;  
      AHitTest:   TcxCustomGridHitTest;   X,   Y:   Integer;   var   AllowPopup:   Boolean);  
begin  
      if   ASenderMenu   is   TcxGridStdHeaderMenu   then  
          TcxGridStdHeaderMenu(ASenderMenu).OnPopup   :=   StdHeaderMenuPopup;  
end;  
   
procedure   TForm1.StdHeaderMenuPopup(Sender:   TObject);  
var  
      I:   Integer;  
begin  
      with   TcxGridStdHeaderMenu(Sender).Items   do  
          for   I   :=   0   to   Count   -   1   do  
              if   Items[I].Caption   =   'Group   By   Box'   then  
              begin  
                  Items[I].Enabled   :=   False;  
                  System.Break;  
              end  
end;

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

1)   View.DataController.DataModeController.GridMode   =   False时  
   
      RecIdx   :=   View.Controller.SelectedRecords[i].RecordIndex;  
      ColIdx   :=   View.DataController.GetItemByFieldName(AFieldName).Index;  
      OutputVal   :=   View.DataController.Values[RecIdx,   ColIdx];  
   
      //RecID   :=   View.DataController.GetRecordId(RecIdx);  
      //OutputVal   :=   ADataSet.Lookup(View.DataController.KeyFieldNames,   RecID,   AFieldName);  
   
2)   View.DataController.DataModeController.GridMode   =   True时  
      Bkm   :=   View.DataController.GetSelectedBookmark(ASelectedRecordIndex);  
      if   ADataSet.BookmarkValid(TBookmark(Bkm))   then  
      begin  
          ADataSet.Bookmark   :=   TBookmark(Bkm);  
          OutputVal   :=   ADataSet.FieldByName(AFieldName).Value;  
      end;  
   
      View.BeginUpdate;  
      View.DataController.BeginLocate;  
      try  
          //   make   changes   here…  
      finally  
          View.DataController.EndLocate;  
          View.EndUpdate;  
      end;

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

procedure   TForm1.ADetailDataControllerCollapsin(  ADataController:  TcxCustomDataController;  

ARecordIndex:   Integer;  var   AAllow:   Boolean);  
var  
      I:   Integer;  
      C:   Integer;  
begin  
      AAllow   :=   False;  
      C   :=   0;  
      for   I   :=   0   to   ADataController.RecordCount   -   1   do  
      begin  
          if   ADataController.GetDetailExpanding(I)   then  
              Inc(C);  
          if   C   >   1   then  
              AAllow   :=   True;  
        end;  
end;  
   
procedure   TForm1.ADetailDataControllerExpanding(  
      ADataController:   TcxCustomDataController;   ARecordIndex:   Integer;  
      var   AAllow:   Boolean);  
begin  
      ADataController.CollapseDetails;  
end;  
   
procedure   TForm1.FormCreate(Sender:   TObject);  
begin        cxGrid1DBTableView1.DataController.OnDetailExpanding:=ADetailDataControllerExpanding;         cxGrid1DBTableView1.DataController.OnDetailCollapsing:=ADetailDataControllerCollapsing;  
end;
****************************************************************************
44 动态创建层次(Level)和视图(View)
解决:
var      
      Grid:   TcxGrid;      
      Level:   TcxGridLevel;      
      View:   TcxGridDBTableView;      
begin  
      //   Creates   a   Grid   instance  
      Grid   :=   TcxGrid.Create(SomeOwner);      
      Grid.Parent   :=   SomeParent;      
      //   Creates   a   Level  
      Level   :=   Grid.Levels.Add;      
      Level.Name   :=   'SomeLevelName';  
      //   Creates   a   View  
      View   :=   Grid.CreateView(TcxGridDBTableView)   as   TcxGridDBTableView;      
      View.Name   :=   'SomeViewName';  
      //   …   and   binds   it   to   the   Level  
      Level.GridView   :=   View;      
      //   Hooks   up   the   View   to   the   data  
      View.DataController.DataSource   :=   SomeDataSource;      
      //   …   and   creates   all   columns  
      View.DataController.CreateAllItems;      
end;

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

procedure   TForm1.cxGrid1DBTableView1CustomDrawFooterCell(  
      Sender:   TcxGridTableView;   ACanvas:   TcxCanvas;  
      AViewInfo:   TcxGridColumnHeaderViewInfo;   var   ADone:   Boolean);  
var  
      ALevel,   ADataGroupIndex:   Integer;  
      AGridRecord,   AGroupRecord:   TcxCustomGridRecord;  
begin  
      if   AViewInfo   is   TcxGridRowFooterCellViewInfo   and    //   Row   footer  
            (TcxGridDBColumn(AViewInfo.Column).DataBinding.FieldName   =   'Area')   then     //   Area   column  
   begin  
        AGridRecord:=   TcxGridRowFooterCellViewInfo(AViewInfo).GridRecord;  
        ALevel:= TcxGridRowFooterCellViewInfo(AViewInfo).Container.GroupLevel;  
ADataGroupIndex:=Sender.DataController.Groups.DataGroupIndexByRowIndex[AGridRecord.Index];  
         if   ADataGroupIndex   <>   -1   then  
         begin  
            AGroupRecord   :=   AGridRecord;  
            while   AGroupRecord.Level   <>   ALevel   do  
            AGroupRecord   :=   AGroupRecord.ParentRecord;  
            AViewInfo.Text   :=   AGroupRecord.DisplayTexts[0];  
         end;  
     end;  
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对象
解决:

function   GetLevelByName(AGrid:   TcxGrid;   ALevelName:   string):   TcxGridLevel;  
   
      function   LoopThroughLevels(ALevel:   TcxGridLevel;   ALevelName:   string):   TcxGridLevel;  
      var  
          I:   Integer;  
      begin  
          Result   :=   nil;  
          for   I   :=   0   to   ALevel.Count   -   1   do  
          begin  
              if   ALevel[I].Name   =   ALevelName   then  
              begin  
                  Result   :=   ALevel[I];  
                  Exit;  
              end;  
              if   ALevel[I].Count   >   0   then  
              begin  
                  Result   :=   LoopThroughLevels(ALevel[I],   ALevelName);  
                  if   Result   <>   nil   then  
                      Exit;  
              end;  
          end;  
      end;  
   
var  
      I:   Integer;  
begin  
      Result   :=   nil;  
      for   I   :=   0   to   AGrid.Levels.Count   -   1   do  
      begin  
          if   AGrid.Levels[I].Name   =   ALevelName   then  
          begin  
              Result   :=   AGrid.Levels[I];  
              Exit;  
          end;  
          if   AGrid.Levels[I].Count   >   0   then  
          begin  
              Result   :=   LoopThroughLevels(AGrid.Levels[I],   ALevelName);  
              if   Result   <>   nil   then  
                  Exit;  
          end;  
      end;  
end;

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

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值