cxGrid技巧汇总
- 格式:doc
- 大小:95.50 KB
- 文档页数:24
cxGrid技巧汇总2009-04-01 12:48==========================================================================在主从TableView中根据主TableView得到对应的从TableViewvarADetailDC: TcxGridDataController;AView: TcxCustomGridTableView;beginwith cxGrid1DBTableView1.DataController doADetailDC := TcxGridDataController(GetDetailDataController(FocusedRecordIndex, 0));AView := ADetailDC.GridView;end;============================================================================== 定位在第一行并显示内置编辑器cxDBVerticalGrid1.FocusedRow := cxDBVerticalGrid1.Rows[0];cxDBVerticalGrid1.ShowEdit;============================================================================== 隐藏"<No data to display>" 字符串该文本存储在scxGridNoDataInfoText资源字符串,可以将该资源字符串的内容设为空来隐藏该文本。
uses cxClasses, cxGridStrs;...cxSetResourceString(@scxGridNoDataInfoText, '');//如果"<No data to display>" 字符串已经显示,需要调用:<View>.LayoutChanged;============================================================删除应用过滤后的行varI: Integer;beginwith <GridView> dofor I := 0 to ViewData.RecordCount - 1 dobeginViewData.Records[0].Focused := True;DataController.DataSet.Delete;end;=============================================================根据单元的值设置样式procedure <aForm>.<aColumn>StylesGetContentStyle(Sender: TcxCustomGridTableView; ARecord: TcxCustomGridRecord;AItem: TcxCustomGridTableItem; out AStyle: TcxStyle);beginif ARecord.Values[AItem.Index] = aSomeValue thenAStyle := <aSomeStyle>;end;procedure <aForm>.<aView>StylesGetContentStyle(Sender: TcxCustomGridTableView; ARecord: TcxCustomGridRecord;AItem: TcxCustomGridTableItem; out AStyle: TcxStyle);varAColumn: TcxCustomGridTableItem;beginAColumn := (Sender as TcxGridDBTableView).GetColumnByFieldName('Email');if VarToStr(ARecord.Values[AColumn.Index]) = '' thenAStyle := cxStyleNullEmail;end;==============================================================================TcxCustomGridTableView.FindItemByName, TcxGridDBTableView.GetColumnByFieldName or TcxGridDBDataController.GetItemByFieldNamewith cxGrid1DBBandedTableView1.DataController doAValue := Values[FocusedRecordIndex, GetItemByFieldName('SomeFieldName').Index];===================================================================动态生成BandedViewvarAView: TcxCustomGridView;beginAView := <cxGrid>.CreateView(TcxGridDBBandedTableView); TcxGridDBBandedTableView(AView).DataController.DataSource := <DataSource>; TcxGridDBBandedTableView(AView).Bands.Add;with TcxGridDBBandedTableView(AView).Bands.Add dobeginVisible := False;FixedKind := fkLeft;end;TcxGridDBBandedTableView(AView).DataController.CreateAllItems;<cxGridLevel>.GridView := AView;======================================================================当底层数据集为空时显示一条空记录procedure <Form>.<cxGrid>Enter(Sender: TObject);varView: TcxGridDBTableView;beginView := TcxGridDBTableView((Sender as TcxGrid).FocusedView);if View.DataController.DataSet.IsEmpty thenbeginView.DataController.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 := View.DataController.FocusedRecordIndex;View.DataController.Values[ARecIndex, SomeItemIndex] := SomeValue;View.DataController.Post;另外一种方法是使用View.DataController.DataSource.DataSet得到底层数据集后,再用数据集的方法来操作数据。
========================================================================激活内置编辑控件1) <aView>.Controller.EditingController.ShowEdit(<aColumn>);2) <aView>.Controller.EditingController.StartEditShowingTimer(<aColumn>);3) <aView>.Controller.EditingItem := <aColumn>;4) <aColumn>.Editing := True;隐藏内置编辑控件<aView>.Controller.EditingController.HideEdit(True);===========================================================================移除一个分组列<aColumn>.GroupIndex := -1;<aColumn>.Visible := True;===========================================================================保存修改到数据库procedure <aForm>.FormClose(Sender: TObject; var Action: TCloseAction);beginif (<aGrid>.FocusedView <> nil) and (<aGrid>.FocusedView.DataController.EditState <> []) then <aGrid>.FocusedView.DataController.Post;end;============================================================================设置内置右键菜单内置右键菜单包括二个菜单:cxGridStdHeaderMenu, TcxGridStdFooterMenuuses cxGridStdPopupMenu;procedure TForm1.cxGridPopupMenu1Popup(ASenderMenu: TComponent;AHitTest: TcxCustomGridHitTest; X, Y: Integer; var AllowPopup: Boolean);beginif ASenderMenu is TcxGridStdHeaderMenu thenTcxGridStdHeaderMenu(ASenderMenu).OnPopup := StdHeaderMenuPopup;end;procedure TForm1.StdHeaderMenuPopup(Sender: TObject);varI: Integer;beginwith TcxGridStdHeaderMenu(Sender).Items dofor I := 0 to Count - 1 doif Items[I].Caption = 'Group By Box' thenbeginItems[I].Enabled := False;System.Break;endend;=========================================================================== 得到选中记录的值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)) thenbeginADataSet.Bookmark := TBookmark(Bkm);OutputVal := ADataSet.FieldByName(AFieldName).Value;end;View.BeginUpdate;View.DataController.BeginLocate;try// make changes here…finallyView.DataController.EndLocate;View.EndUpdate;end;============================================================= 在GridMode禁用内置的右键Footer菜单uses cxGridStdPopupMenu;procedure cxGridPopupMenuOnPopup(...)beginif (ASenderMenu is TcxGridStdFooterMenu) and<GridView>.DataController.DataModeController.GridMode then AllowPopup := False;end;============================================================== 主从表任何时候只能展开一个组procedure TForm1.ADetailDataControllerCollapsing( ADataController: TcxCustomDataController; ARecordIndex: Integer;var AAllow: Boolean);varI: Integer;C: Integer;beginAAllow := False;C := 0;for I := 0 to 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;procedure TForm1.FormCreate(Sender: TObject);begincxGrid1DBTableView1.DataController.OnDetailExpanding := ADetailDataControllerExpanding; cxGrid1DBTableView1.DataController.OnDetailCollapsing := ADetailDataControllerCollapsing; end;=================================================================动态创建层次(Level)和视图(View)varGrid: TcxGrid;Level: TcxGridLevel;View: TcxGridDBTableView;begin// Creates a Grid instanceGrid := TcxGrid.Create(SomeOwner);Grid.Parent := SomeParent;// Creates a LevelLevel := Grid.Levels.Add; := 'SomeLevelName';// Creates a ViewView := Grid.CreateView(TcxGridDBTableView) as TcxGridDBTableView; := 'SomeViewName';// … and binds it to 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: TcxCanvas;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 column beginAGridRecord := TcxGridRowFooterCellViewInfo(AViewInfo).GridRecord;ALevel := TcxGridRowFooterCellViewInfo(AViewInfo).Container.GroupLevel; ADataGroupIndex := Sender.DataController.Groups.DataGroupIndexByRowIndex[AGridRecord.Index];if ADataGroupIndex <> -1 thenbeginAGroupRecord := AGridRecord;while AGroupRecord.Level <> ALevel doAGroupRecord := AGroupRecord.ParentRecord;AViewInfo.Text := AGroupRecord.DisplayTexts[0];end;end;end;===========================================================================访问过滤之后的记录varI: Integer;beginMemo1.Lines.Clear;with cxGrid1DBTableView1.DataController dofor I := 0 to FilteredRecordCount - 1 doMemo1.Lines.Add(DisplayTexts[FilteredRecordIndex[I], 0]);end;============================================================================获得单元的FontcxGrid1DBTableView1.ViewInfo.RecordsViewInfo.Items[1].GetCellViewInfoByItem(cxGrid1DBTableView1Company).EditViewInfo.Font;============================================================================ 根据Level名称找到Level对象function GetLevelByName(AGrid: TcxGrid; ALevelName: string): TcxGridLevel;function LoopThroughLevels(ALevel: TcxGridLevel; ALevelName: string): TcxGridLevel;varI: Integer;beginResult := nil;for I := 0 to ALevel.Count - 1 dobeginif ALevel[I].Name = ALevelName thenbeginResult := ALevel[I];Exit;end;if ALevel[I].Count > 0 thenbeginResult := LoopThroughLevels(ALevel[I], ALevelName);if Result <> nil thenExit;end;end;end;varI: Integer;beginResult := nil;for I := 0 to AGrid.Levels.Count - 1 dobeginif AGrid.Levels[I].Name = ALevelName thenbeginResult := AGrid.Levels[I];Exit;end;if AGrid.Levels[I].Count > 0 thenbeginResult := LoopThroughLevels(AGrid.Levels[I], ALevelName);if Result <> nil thenExit;end;end;end;============================================================================指定Filter Builder打开/保存过滤文件的默认路径uses..., cxFilterControlDialog;procedure TForm.GridView1FilterControlDialogShow(Sender: TObject);beginTfmFilterControlDialog(Sender).OpenDialog.InitialDir := 'D:\'end;============================================================================保存/恢复带汇总行的布局<TableView>.StoreToIniFile('c:\Grid.ini', True, [gsoUseSummary]);<GridView>.RestoreFromIniFile(<inifilename>,True,False {or True, optional},[gsoUseSummary]); ============================================================================取消过滤时移到第一行usescxCustomData;procedure TYour_Form.AViewDataControllerFilterChanged(Sender: TObject);varFilter: TcxDataFilterCriteria;beginwith Sender as TcxDataFilterCriteria doif IsEmpty thenDataController.FocusedRowIndex := 0;end;============================================================================= 排序后移到第一行可以设置DataController.Options.FocusTopRowAfterSorting := True,也可以使用如下的代码:usescxCustomData;procedure TYour_Form.Your_ViewDataControllerSortingChanged(Sender: TObject);beginTcxCustomDataController(Sender).FocusedRowIndex := 0;end;============================================================================== 判断当前行是否第一行或最后一行可以使用DataController的IsBOF, IsEOF方法,或者:<AView>.Controller.Controller.FocusedRow.IsFirst<AView>.Controller.Controller.FocusedRow.IsLast============================================================================== 根据指定值查找记录DataController提供了好几个方法来得到指定值对应的RecordIndex对于Bound View可以使用FindRecordIndexByKeyValue方法=============================================================================== 编辑和显示Blob字段该字段的Properties设置为BlobEdit,并将BlobPaintStyle 属性设为bpsText=============================================================================== 得到可见行数<View>.ViewInfo.VisibleRecordCount=============================================================================== 保存后的行设置为当前行constCM_SETFOCUSEDRECORD = WM_USER + 1002;typeTForm1 = class(TForm)cxGrid1DBTableView1: TcxGridDBTableView;cxGrid1Level1: TcxGridLevel;cxGrid1: TcxGrid;dxMemData1: TdxMemData;dxMemData1Field1: TStringField;dxMemData1Field2: TIntegerField;DataSource1: TDataSource;cxGrid1DBTableView1RecId: TcxGridDBColumn;cxGrid1DBTableView1Field1: TcxGridDBColumn;cxGrid1DBTableView1Field2: TcxGridDBColumn;Timer1: TTimer;CheckBox1: TCheckBox;procedure Timer1Timer(Sender: TObject);procedure dxMemData1AfterPost(DataSet: TDataSet);procedure CheckBox1Click(Sender: TObject);privateprocedure CMSetFocusedRecord(var Msg: TMessage); message CM_SETFOCUSEDRECORD; public{ Public declarations }end;varForm1: TForm1;FocusedIdx: Integer;implementation{$R *.dfm}procedure TForm1.Timer1Timer(Sender: TObject);begindxMemData1.AppendRecord(['', IntToStr(Random(1000)), Random(1000)]);end;procedure TForm1.dxMemData1AfterPost(DataSet: TDataSet);beginPostMessage(Handle, CM_SETFOCUSEDRECORD, Integer(cxGrid1DBTableView1), MakeLParam(cxGrid1DBTableView1.Controller.FocusedRowIndex,cxGrid1DBTableView1.Controller.TopRowIndex));end;procedure TForm1.CMSetFocusedRecord(var Msg: TMessage);beginTcxGridDBTableView(msg.WParam).Controller.FocusedRowIndex := Msg.LParamLo; TcxGridDBTableView(msg.WParam).Controller.TopRowIndex := Msg.LParamHi;end;procedure TForm1.CheckBox1Click(Sender: TObject);beginTimer1.Enabled := TCheckBox(Sender).Checked;end;end.=============================================================================== ==删除记录并获得焦点procedure TForm1.BtnDeleteClick(Sender: TObject);varFocusedRow, TopRow: Integer;View: TcxGridTableView;DataController: TcxGridDataController;beginView := cxGrid1.FocusedView as TcxGridTableView;DataController := View.DataController;// Remember the top row (the vertical scrollbar position)TopRow := View.Controller.TopRowIndex;// Remember the focused row(!) indexFocusedRow := DataController.FocusedRowIndex;DataController.DeleteFocused;// After deletion the same row must be focused,// although it will correspond to a different data recordDataController.FocusedRowIndex := FocusedRow;// Restore the top rowView.Controller.TopRowIndex := TopRow;end;分享一下ExpressQuantumGrid4的cxGrid的一些使用方法和经验日期:2006年2月6日作者:wangxian11 人气:9427 查看:[大字体中字体小字体]使用cxGrid有一些时间了,在这里总结一下使用cxGrid的一些方法,希望给刚开始接触cxGrid的人一些帮助。