Delphi CxGrid 汇总(3) 转
使用FocusedView属性得到当前焦点View,用View.DataController得到对应的Data
之后使用Data
-
-
-
-
-
-
示例:
var
…
另外一种方法是使用View.DataController.DataSource.DataSet得到底层数据集后,再用数据集的方法来操作数据。
****************************************************************************
36 激活内置编辑控件
解决:
****************************************************************************
37 隐藏内置编辑控件
解决:
.Controller.EditingController.HideEdit(True);
****************************************************************************
38
移除一个分组列
****************************************************************************
39 保存修改到数据库
解决:
procedure
begin
if
and
end;
****************************************************************************
40 设置内置右键菜单
解决:
内置右键菜单包括二个菜单:cxGridStdHeaderMenu,
- uses
cxGridStdPopupMenu; -
- procedure
.cxGridPopupMenu1Popup(ASenderMenu:TForm1 TComponent; -
AHitTest: TcxCustomGridHitTest; X, Y: Integer; var AllowPopup: Boolean); - begin
-
if isASenderMenu thenTcxGridStdHeaderMenu -
TcxGridStdHeaderMenu(ASenderMenu).OnPopup := StdHeaderMenuPopup; - end;
-
- procedure
.StdHeaderMenuPopup(Sender:TForm1 TObject); - var
-
I: Integer; - begin
-
with doTcxGridStdHeaderMenu(Sender).Items -
for 0I := to 1Count - do -
if 'GroupItems[I].Caption = By Box' then -
begin -
Items[I].Enabled := False; -
System.Break; -
end - end;
****************************************************************************
41
得到选中记录的值
解决:
- 1)
.DataController.DataModeController.GridModeView = 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)
.DataController.DataModeController.GridModeView = True时 -
Bkm := View.DataController.GetSelectedBookmark(ASelectedRecordIndex); -
if .BookmarkValid(TBookmark(Bkm))ADataSet 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
procedure
begin
end;
****************************************************************************
43
主从表任何时候只能展开一个组
解决:
- procedure
.ADetailDataControllerColTForm1 lapsin( ADataController: TcxCustomDataController; -
-
ARecordIndex:
Integer; var AAllow: Boolean); - var
-
I: Integer; -
C: Integer; - begin
-
AAllow := False; -
C := 0; -
for 0I := to .RecordCountADataController 1- do -
begin -
if .GetDetailExpanding(I)ADataController then -
Inc(C); -
if 1C > then -
AAllow := True; -
end; - end;
-
- procedure
.ADetailDataControllerExpTForm1 anding( -
ADataController: TcxCustomDataController; ARecordIndex: Integer; -
var AAllow: Boolean); - begin
-
ADataController.CollapseDetails; - end;
-
- procedure
.FormCreate(Sender:TForm1 TObject); - begin
.DataController.OnDetailExpanding:=ADetailDataControllerExpcxGrid1DBTableView1 anding; .DataController.OnDetailCollapsing:=ADetailDataControllerColcxGrid1DBTableView1 lapsing; - 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) asTcxGridDBTableView; -
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
- procedure
.cxGrid1DBTableView1CustoTForm1 mDrawFooterCell( -
Sender: TcxGridTableView; ACanvas: TcxCanvas; -
AViewInfo: TcxGridColumnHeaderViewI nfo; var ADone: Boolean); - var
-
ALevel, ADataGroupIndex: Integer; -
AGridRecord, AGroupRecord: TcxCustomGridRecord; - begin
-
if isAViewInfo andTcxGridRowFooterCellView Info //Row footer -
(TcxGridDBColumn(AViewInfo.Column).DataBinding.FieldName 'Area')= then //Area column -
begin -
AGridRecord:= TcxGridRowFooterCellView Info(AViewInfo).GridRecord; -
ALevel:= TcxGridRowFooterCellView Info(AViewInfo).Container.GroupLevel; - ADataGroupIndex:=Sender.DataController.Groups.DataGroupIndexByRowIndex
[AGridRecord .Index]; -
if 1ADataGroupIndex <> - then -
begin -
AGroupRecord := AGridRecord; -
while .LevelAGroupRecord do<> ALevel -
AGroupRecord := AGroupRecord.ParentRecord; -
AViewInfo.Text .DisplayTexts[0];:= AGroupRecord -
end; -
end; - end;
****************************************************************************
46 访问过滤之后的记录
解决:
var
begin
end;
****************************************************************************
47 获得单元的Font
解决:
cxGrid1DBTableView1.ViewInfo.RecordsViewInfo.Items[1].GetCellViewInfoByItem(
****************************************************************************
48
根据Level名称找到Level对象
解决:
- function
string):GetLevelByName(AGrid: TcxGrid; ALevelName: TcxGridLevel; -
-
function string):LoopThroughLevels(ALevel: TcxGridLevel; ALevelName: TcxGridLevel; -
var -
I: Integer; -
begin -
Result := nil; -
for 0I := to .CountALevel 1- do -
begin -
if thenALevel[I].Name = ALevelName -
begin -
Result := ALevel[I]; -
Exit; -
end; -
if 0ALevel[I].Count > then -
begin -
Result := LoopThroughLevels(ALevel[I], ALevelName); -
if nilResult <> then -
Exit; -
end; -
end; -
end; -
- var
-
I: Integer; - begin
-
Result := nil; -
for 0I := to .Levels.CountAGrid 1- do -
begin -
if .Levels[I].NameAGrid then= ALevelName -
begin -
Result := AGrid.Levels[I]; -
Exit; -
end; -
if .Levels[I].CountAGrid 0> then -
begin -
Result := LoopThroughLevels(AGrid.Levels[I], ALevelName); -
if nilResult <> then -
Exit; -
end; -
end; - end;
****************************************************************************
uses
procedure
begin
end;
浙公网安备 33010602011771号