cxGrid技巧汇总一些常用代码.docx

上传人:b****8 文档编号:9948097 上传时间:2023-02-07 格式:DOCX 页数:17 大小:20.12KB
下载 相关 举报
cxGrid技巧汇总一些常用代码.docx_第1页
第1页 / 共17页
cxGrid技巧汇总一些常用代码.docx_第2页
第2页 / 共17页
cxGrid技巧汇总一些常用代码.docx_第3页
第3页 / 共17页
cxGrid技巧汇总一些常用代码.docx_第4页
第4页 / 共17页
cxGrid技巧汇总一些常用代码.docx_第5页
第5页 / 共17页
点击查看更多>>
下载资源
资源描述

cxGrid技巧汇总一些常用代码.docx

《cxGrid技巧汇总一些常用代码.docx》由会员分享,可在线阅读,更多相关《cxGrid技巧汇总一些常用代码.docx(17页珍藏版)》请在冰豆网上搜索。

cxGrid技巧汇总一些常用代码.docx

cxGrid技巧汇总一些常用代码

cxGrid技巧汇总

=============================================================

在主从TableView中根据主TableView得到对应的从TableView

var

ADetailDC:

TcxGridDataController;

AView:

TcxCustomGridTableView;

begin

withcxGrid1DBTableView1.DataControllerdo

ADetailDC:

=TcxGridDataController(GetDetailDataController(FocusedRecordIndex,0));

AView:

=ADetailDC.GridView;

end;

=============================================================

定位在第一行并显示内置编辑器

cxDBVerticalGrid1.FocusedRow:

=cxDBVerticalGrid1.Rows[0];

cxDBVerticalGrid1.ShowEdit;

=============================================================

隐藏""字符串

该文本存储在scxGridNoDataInfoText资源字符串,可以将该资源字符串的内容设为空

来隐藏该文本。

usescxClasses,cxGridStrs;...

cxSetResourceString(@scxGridNoDataInfoText,'');

//如果""字符串已经显示,需要调用:

.LayoutChanged;

============================================================

删除应用过滤后的行

var

I:

Integer;

begin

withdo

forI:

=0toViewData.RecordCount-1do

begin

ViewData.Records[0].Focused:

=True;

DataController.DataSet.Delete;

end;

=============================================================

根据单元的值设置样式

procedure.StylesGetContentStyle(

Sender:

TcxCustomGridTableView;ARecord:

TcxCustomGridRecord;

AItem:

TcxCustomGridTableItem;outAStyle:

TcxStyle);

begin

ifARecord.Values[AItem.Index]=aSomeValuethen

AStyle:

=;

end;

procedure.StylesGetContentStyle(

Sender:

TcxCustomGridTableView;ARecord:

TcxCustomGridRecord;

AItem:

TcxCustomGridTableItem;outAStyle:

TcxStyle);

var

AColumn:

TcxCustomGridTableItem;

begin

AColumn:

=(SenderasTcxGridDBTableView).GetColumnByFieldName('Email');

ifVarToStr(ARecord.Values[AColumn.Index])=''then

AStyle:

=cxStyleNullEmail;

end;

=============================================================

TcxCustomGridTableView.FindItemByName,TcxGridDBTableView.GetColumnByFieldNameor

TcxGridDBDataController.GetItemByFieldName

withcxGrid1DBBandedTableView1.DataControllerdo

AValue:

=Values[FocusedRecordIndex,GetItemByFieldName('SomeFieldName').Index];

=============================================================

动态生成BandedView

var

AView:

TcxCustomGridView;

begin

AView:

=.CreateView(TcxGridDBBandedTableView);

TcxGridDBBandedTableView(AView).DataController.DataSource:

=;

TcxGridDBBandedTableView(AView).Bands.Add;

withTcxGridDBBandedTableView(AView).Bands.Adddo

begin

Visible:

=False;

FixedKind:

=fkLeft;

end;

TcxGridDBBandedTableView(AView).DataController.CreateAllItems;

.GridView:

=AView;  

=============================================================

当底层数据集为空时显示一条空记录

procedure.Enter(Sender:

TObject);

var

View:

TcxGridDBTableView;

begin

View:

=TcxGridDBTableView((SenderasTcxGrid).FocusedView);

ifView.DataController.DataSet.IsEmptythen

begin

View.DataController.DataSet.Append;

View.Controller.EditingController.ShowEdit;

end;

end;

=============================================================

在当前View插入记录

使用FocusedView属性得到当前焦点View,用View.DataController得到对应的DataController,

之后使用DataController的方法来操作数据:

-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得到底层数据集后,再用数据集的

方法来操作数据。

=============================================================

激活内置编辑控件

1).Controller.EditingController.ShowEdit();

2).Controller.EditingController.StartEditShowingTimer();

3).Controller.EditingItem:

=;

4).Editing:

=True;

隐藏内置编辑控件

.Controller.EditingController.HideEdit(True);

=============================================================

移除一个分组列

.GroupIndex:

=-1;

.Visible:

=True;

=============================================================

保存修改到数据库

procedure.FormClose(Sender:

TObject;varAction:

TCloseAction);

begin

if(.FocusedView<>nil)and(.FocusedView.DataController.EditState<>[])then

.FocusedView.DataController.Post;

end;

=============================================================

设置内置右键菜单

内置右键菜单包括二个菜单:

cxGridStdHeaderMenu,TcxGridStdFooterMenu

usescxGridStdPopupMenu;

procedureTForm1.cxGridPopupMenu1Popup(ASenderMenu:

TComponent;

AHitTest:

TcxCustomGridHitTest;X,Y:

Integer;varAllowPopup:

Boolean);

begin

ifASenderMenuisTcxGridStdHeaderMenuthen

TcxGridStdHeaderMenu(ASenderMenu).OnPopup:

=StdHeaderMenuPopup;

end;

procedureTForm1.StdHeaderMenuPopup(Sender:

TObject);

var

I:

Integer;

begin

withTcxGridStdHeaderMenu(Sender).Itemsdo

forI:

=0toCount-1do

ifItems[I].Caption='GroupByBox'then

begin

Items[I].Enabled:

=False;

System.Break;

end

end;

=============================================================

得到选中记录的值

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);

ifADataSet.BookmarkValid(TBookmark(Bkm))then

begin

ADataSet.Bookmark:

=TBookmark(Bkm);

OutputVal:

=ADataSet.FieldByName(AFieldName).Value;

end;

View.BeginUpdate;

View.DataController.BeginLocate;

try

//makechangeshere…

finally

View.DataController.EndLocate;

View.EndUpdate;

end;

=============================================================

在GridMode禁用内置的右键Footer菜单

usescxGridStdPopupMenu;

procedurecxGridPopupMenuOnPopup(...)

begin

if(ASenderMenuisTcxGridStdFooterMenu)and

.DataController.DataModeController.GridModethen

AllowPopup:

=False;

end;

=============================================================

主从表任何时候只能展开一个组

procedureTForm1.ADetailDataControllerCollapsing(

ADataController:

TcxCustomDataController;ARecordIndex:

Integer;

varAAllow:

Boolean);

var

I:

Integer;

C:

Integer;

begin

AAllow:

=False;

C:

=0;

forI:

=0toADataController.RecordCount-1do

begin

ifADataController.GetDetailExpanding(I)then

Inc(C);

ifC>1then

AAllow:

=True;

end;

end;

procedureTForm1.ADetailDataControllerExpanding(

ADataController:

TcxCustomDataController;ARecordIndex:

Integer;

varAAllow:

Boolean);

begin

ADataController.CollapseDetails;

end;

procedureTForm1.FormCreate(Sender:

TObject);

begin

cxGrid1DBTableView1.DataController.OnDetailExpanding:

=ADetailDataControllerExpanding;

cxGrid1DBTableView1.DataController.OnDetailCollapsing:

=ADetailDataControllerCollapsing;

end;

=============================================================

动态创建层次(Level)和视图(View)

var

Grid:

TcxGrid;

Level:

TcxGridLevel;

View:

TcxGridDBTableView;

begin

//CreatesaGridinstance

Grid:

=TcxGrid.Create(SomeOwner);

Grid.Parent:

=SomeParent;

//CreatesaLevel

Level:

=Grid.Levels.Add;

Level.Name:

='SomeLevelName';

//CreatesaView

View:

=Grid.CreateView(TcxGridDBTableView)asTcxGridDBTableView;

View.Name:

='SomeViewName';

//…andbindsittotheLevel

Level.GridView:

=View;

//HooksuptheViewtothedata

View.DataController.DataSource:

=SomeDataSource;

//…andcreatesallcolumns

View.DataController.CreateAllItems;

end;

=============================================================

获得GroupFooter合计行对应的记录

procedureTForm1.cxGrid1DBTableView1CustomDrawFooterCell(

Sender:

TcxGridTableView;ACanvas:

TcxCanvas;

AViewInfo:

TcxGridColumnHeaderViewInfo;varADone:

Boolean);

var

ALevel,ADataGroupIndex:

Integer;

AGridRecord,AGroupRecord:

TcxCustomGridRecord;

begin

ifAViewInfoisTcxGridRowFooterCellViewInfoand//Rowfooter

(TcxGridDBColumn(AViewInfo.Column).DataBinding.FieldName='Area')then//Areacolumn

begin

AGridRecord:

=TcxGridRowFooterCellViewInfo(AViewInfo).GridRecord;

ALevel:

=TcxGridRowFooterCellViewInfo(AViewInfo).Container.GroupLevel;

ADataGroupIndex:

=Sender.DataController.Groups.DataGroupIndexByRowIndex[AGridRecord.Index];

ifADataGroupIndex<>-1then

begin

AGroupRecord:

=AGridRecord;

whileAGroupRecord.Level<>ALeveldo

AGroupRecord:

=AGroupRecord.ParentRecord;

AViewInfo.Text:

=AGroupRecord.DisplayTexts[0];

end;

end;

end;

=============================================================

访问过滤之后的记录

var

I:

Integer;

begin

Memo1.Lines.Clear;

withcxGrid1DBTableView1.DataControllerdo

forI:

=0toFilteredRecordCount-1do

Memo1.Lines.Add(DisplayTexts[FilteredRecordIndex[I],0]);

end;

======================================================================

获得单元的Font

cxGrid1DBTableView1.ViewInfo.RecordsViewInfo.Items[1].GetCellViewInfoByItem(

cxGrid1DBTableView1Company).EditViewInfo.Font;

======================================================================

根据Level名称找到Level对象

functionGetLevelByName(AGrid:

TcxGrid;ALevelName:

string):

TcxGridLevel;

functionLoopThroughLevels(ALevel:

TcxGridLevel;ALevelName:

string):

TcxGridLevel;

var

I:

Integer;

begin

Result:

=nil;

forI:

=0toALevel.Count-1do

begin

ifALevel[I].Name=ALevelNamethen

begin

Result:

=ALevel[I];

Exit;

end;

ifALevel[I].Count>0then

begin

Result:

=LoopThroughLevels(ALevel[I],ALevelName);

ifResult<>nilthen

Exit;

end;

end;

end;

var

I:

Integer;

begin

Result:

=nil;

forI:

=0toAGrid.Levels.Count-1do

begin

ifAGrid.Levels[I].Name=ALevelNamethen

begin

Result:

=AGrid.Levels[I];

Exit;

end;

ifAGrid.Levels[I].Count>0then

begin

Result:

=LoopThroughLevels(AGrid.Levels[I],ALevelName);

ifResult<>nilthe

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 工程科技 > 冶金矿山地质

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1