1、cxGrid技巧汇总一些常用代码cxGrid技巧汇总=在主从TableView中根据主TableView得到对应的从TableViewvarADetailDC: TcxGridDataController;AView: TcxCustomGridTableView;beginwith cxGrid1DBTableView1.DataController doADetailDC := TcxGridDataController(GetDetailDataController(FocusedRecordIndex, 0);AView := ADetailDC.GridView;end;=定位在第一
2、行并显示内置编辑器cxDBVerticalGrid1.FocusedRow := cxDBVerticalGrid1.Rows0;cxDBVerticalGrid1.ShowEdit;=隐藏 字符串该文本存储在scxGridNoDataInfoText资源字符串,可以将该资源字符串的内容设为空来隐藏该文本。uses cxClasses, cxGridStrs;.cxSetResourceString(scxGridNoDataInfoText, );/如果 字符串已经显示,需要调用:.LayoutChanged;=删除应用过滤后的行varI: Integer;beginwith dofor I
3、 := 0 to ViewData.RecordCount - 1 dobeginViewData.Records0.Focused := True;DataController.DataSet.Delete;end;=根据单元的值设置样式procedure .StylesGetContentStyle(Sender: TcxCustomGridTableView; ARecord: TcxCustomGridRecord;AItem: TcxCustomGridTableItem; out AStyle: TcxStyle);beginif ARecord.ValuesAItem.Index
4、 = aSomeValue thenAStyle := ;end;procedure .StylesGetContentStyle(Sender: TcxCustomGridTableView; ARecord: TcxCustomGridRecord;AItem: TcxCustomGridTableItem; out AStyle: TcxStyle);varAColumn: TcxCustomGridTableItem;beginAColumn := (Sender as TcxGridDBTableView).GetColumnByFieldName(Email);if VarToSt
5、r(ARecord.ValuesAColumn.Index) = thenAStyle := cxStyleNullEmail;end;=TcxCustomGridTableView.FindItemByName, TcxGridDBTableView.GetColumnByFieldName orTcxGridDBDataController.GetItemByFieldNamewith cxGrid1DBBandedTableView1.DataController doAValue := ValuesFocusedRecordIndex, GetItemByFieldName(SomeF
6、ieldName).Index;=动态生成BandedViewvarAView: TcxCustomGridView;beginAView := .CreateView(TcxGridDBBandedTableView);TcxGridDBBandedTableView(AView).DataController.DataSource := ;TcxGridDBBandedTableView(AView).Bands.Add;with TcxGridDBBandedTableView(AView).Bands.Add dobeginVisible := False;FixedKind := f
7、kLeft;end;TcxGridDBBandedTableView(AView).DataController.CreateAllItems;.GridView := AView;=当底层数据集为空时显示一条空记录procedure .Enter(Sender: TObject);varView: TcxGridDBTableView;beginView := TcxGridDBTableView(Sender as TcxGrid).FocusedView);if View.DataController.DataSet.IsEmpty thenbeginView.DataControlle
8、r.DataSet.Append;View.Controller.EditingController.ShowEdit;end;end;=在当前View插入记录使用FocusedView属性得到当前焦点View,用View.DataController得到对应的Data Controller,之后使用Data Controller的方法来操作数据:- Append- Insert- Post- Cancel- DeleteFocused- DeleteSelection示例:varARecIndex: Integer;View.DataController.Append;ARecIndex :
9、= View.DataController.FocusedRecordIndex;View.DataController.ValuesARecIndex, SomeItemIndex := SomeValue;View.DataController.Post;另外一种方法是使用View.DataController.DataSource.DataSet得到底层数据集后,再用数据集的方法来操作数据。=激活内置编辑控件1) .Controller.EditingController.ShowEdit();2) .Controller.EditingController.StartEditShowi
10、ngTimer();3) .Controller.EditingItem := ;4) .Editing := True;隐藏内置编辑控件.Controller.EditingController.HideEdit(True);=移除一个分组列.GroupIndex := -1;.Visible := True;=保存修改到数据库procedure .FormClose(Sender: TObject; var Action: TCloseAction);beginif (.FocusedView nil) and (.FocusedView.DataController.EditState
11、) then.FocusedView.DataController.Post;end;=设置内置右键菜单内置右键菜单包括二个菜单:cxGridStdHeaderMenu, TcxGridStdFooterMenuuses cxGridStdPopupMenu;procedure TForm1.cxGridPopupMenu1Popup(ASenderMenu: TComponent;AHitTest: TcxCustomGridHitTest; X, Y: Integer; var AllowPopup: Boolean);beginif ASenderMenu is TcxGridStdHe
12、aderMenu thenTcxGridStdHeaderMenu(ASenderMenu).OnPopup := StdHeaderMenuPopup;end;procedure TForm1.StdHeaderMenuPopup(Sender: TObject);varI: Integer;beginwith TcxGridStdHeaderMenu(Sender).Items dofor I := 0 to Count - 1 doif ItemsI.Caption = Group By Box thenbeginItemsI.Enabled := False;System.Break;
13、endend;=得到选中记录的值1) View.DataController.DataModeController.GridMode = False时RecIdx := View.Controller.SelectedRecordsi.RecordIndex;ColIdx := View.DataController.GetItemByFieldName(AFieldName).Index;OutputVal := View.DataController.ValuesRecIdx, ColIdx;/RecID := View.DataController.GetRecordId(RecIdx)
14、;/OutputVal := ADataSet.Lookup(View.DataController.KeyFieldNames, RecID, AFieldName);2) View.DataController.DataModeController.GridMode = True时Bkm := View.DataController.GetSelectedBookmark(ASelectedRecordIndex);if ADataSet.BookmarkValid(TBookmark(Bkm) thenbeginADataSet.Bookmark := TBookmark(Bkm);Ou
15、tputVal := ADataSet.FieldByName(AFieldName).Value;end;View.BeginUpdate;View.DataController.BeginLocate;try/ make changes herefinallyView.DataController.EndLocate;View.EndUpdate;end;=在GridMode禁用内置的右键Footer菜单uses cxGridStdPopupMenu;procedure cxGridPopupMenuOnPopup(.)beginif (ASenderMenu is TcxGridStdF
16、ooterMenu) and.DataController.DataModeController.GridMode thenAllowPopup := False;end;=主从表任何时候只能展开一个组procedure TForm1.ADetailDataControllerCollapsing(ADataController: TcxCustomDataController; ARecordIndex: Integer;var AAllow: Boolean);varI: Integer;C: Integer;beginAAllow := False;C := 0;for I := 0 t
17、o ADataController.RecordCount - 1 dobeginif ADataController.GetDetailExpanding(I) thenInc(C);if C 1 thenAAllow := True;end;end;procedure TForm1.ADetailDataControllerExpanding(ADataController: TcxCustomDataController; ARecordIndex: Integer;var AAllow: Boolean);beginADataController.CollapseDetails;end
18、;procedure TForm1.FormCreate(Sender: TObject);begincxGrid1DBTableView1.DataController.OnDetailExpanding := ADetailDataControllerExpanding;cxGrid1DBTableView1.DataController.OnDetailCollapsing := ADetailDataControllerCollapsing;end;=动态创建层次(Level)和视图(View)varGrid: TcxGrid;Level: TcxGridLevel;View: Tcx
19、GridDBTableView;begin/ Creates a Grid instanceGrid := TcxGrid.Create(SomeOwner);Grid.Parent := SomeParent;/ Creates a LevelLevel := Grid.Levels.Add;Level.Name := SomeLevelName;/ Creates a ViewView := Grid.CreateView(TcxGridDBTableView) as TcxGridDBTableView;View.Name := SomeViewName;/ and binds it t
20、o the LevelLevel.GridView := View;/ Hooks up the View to the dataView.DataController.DataSource := SomeDataSource;/ and creates all columnsView.DataController.CreateAllItems;end;=获得Group Footer合计行对应的记录procedure TForm1.cxGrid1DBTableView1CustomDrawFooterCell(Sender: TcxGridTableView; ACanvas: TcxCanv
21、as;AViewInfo: TcxGridColumnHeaderViewInfo; var ADone: Boolean);varALevel, ADataGroupIndex: Integer;AGridRecord, AGroupRecord: TcxCustomGridRecord;beginif AViewInfo is TcxGridRowFooterCellViewInfo and / Row footer(TcxGridDBColumn(AViewInfo.Column).DataBinding.FieldName = Area) then / Area columnbegin
22、AGridRecord := TcxGridRowFooterCellViewInfo(AViewInfo).GridRecord;ALevel := TcxGridRowFooterCellViewInfo(AViewInfo).Container.GroupLevel;ADataGroupIndex := Sender.DataController.Groups.DataGroupIndexByRowIndexAGridRecord.Index;if ADataGroupIndex -1 thenbeginAGroupRecord := AGridRecord;while AGroupRe
23、cord.Level ALevel doAGroupRecord := AGroupRecord.ParentRecord;AViewInfo.Text := AGroupRecord.DisplayTexts0;end;end;end;=访问过滤之后的记录varI: Integer;beginMemo1.Lines.Clear;with cxGrid1DBTableView1.DataController dofor I := 0 to FilteredRecordCount - 1 doMemo1.Lines.Add(DisplayTextsFilteredRecordIndexI, 0)
24、;end;=获得单元的FontcxGrid1DBTableView1.ViewInfo.RecordsViewInfo.Items1.GetCellViewInfoByItem(cxGrid1DBTableView1Company).EditViewInfo.Font;=根据Level名称找到Level对象function GetLevelByName(AGrid: TcxGrid; ALevelName: string): TcxGridLevel;function LoopThroughLevels(ALevel: TcxGridLevel; ALevelName: string): Tc
25、xGridLevel;varI: Integer;beginResult := nil;for I := 0 to ALevel.Count - 1 dobeginif ALevelI.Name = ALevelName thenbeginResult := ALevelI;Exit;end;if ALevelI.Count 0 thenbeginResult := LoopThroughLevels(ALevelI, ALevelName);if Result nil thenExit;end;end;end;varI: Integer;beginResult := nil;for I := 0 to AGrid.Levels.Count - 1 dobeginif AGrid.LevelsI.Name = ALevelName thenbeginResult := AGrid.LevelsI;Exit;end;if AGrid.LevelsI.Count 0 thenbeginResult := LoopThroughLevels(AGrid.LevelsI, ALevelName);if Result nil the