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