17.
怎样设计多表头的cxGrid?
解决:cxGrid可以解决如下的表头:
---------------------------------
| 说明1 | 说明2 |
---------------------------------
| 字段1 | 字段2 | 字段3 | 字段4 |
| 字段5 | 字段6 |
| 字段7 | 字段8 | 字段9
|
实现这个很简单,你可以直接在上面拖动字段名,拖动时会显示箭头的,放入你想显示的位置就OK了。或者在鼠标右击cxGrid1DBBandedTableView
但是cxGrid不能实现如下的多表头形式:
---------------------------------
| 说明1 | 说明2 |
---------------------------------
| 说明3 | 说明4 | 说明5 | 说明6 |
| 字段1 | 字段2 |
| 字段3 | 字段4 | 字段5 |
不知道有谁能实现这样的多表头?
****************************************************************************
18.
在主从表结构时,当点开“+”时怎样将焦点聚在相应主表的记录上?
解决:
var
HitTest: TcxCustomGridHitTest;
procedure
TColumnsShareDemoMainFor
Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
// Note that the Sender parameter is a
Site
// The point belongs to the [+]/[-]
button area
// Move focus to the
record
end;
****************************************************************************
19
CXGrid4如何展开全部节点
解决:GridDBTableView1.DataController.Groups.FullExpand;
****************************************************************************
20.
cxGrid如何动态创建Items的Editor的项?
解决:cxGrid的列有一个属性,它的编辑框可以指定combobox,spinedit等.在设计时,可以为
combobox的items添加项目.请问是否可以动态创建?(run-time时由程序加入)
- var
-
A:TDataSource:
-
B:TcxlookupcomboboxPropert
ies; - begin
- A:=TDataSource.create(self);
- B:=tcxlookupcomboboxpropert
ies.create(self); - A.Dataset:=Dic_ry_xb;//此处指定数据源。
- b.listdource:=a;//此处指明字段的listsource属性。
- b.keyfieldnames:='a';
//此处指明字段的关键字段 - b.listfieldnames:='b';
//此处指明字段的返回值。 - b.listcolumns.items[0].caption:='x;
//此处默认是会建立一个字段,但是显示的表头是name,所以此处让它显示为自己想要的中午显示。 - cxGrid1DBTableView1c1_sex_code.Properties:=b;
//此处指明是那个字段。 - end;
//这个是初始化的代码
****************************************************************************
21.
拷贝文件时有进度显示
解决:
- procedure
TForm1 .mycopyfile(sourcef,targetf:string); - var
-
FromF,
ToF: file; -
NumRead,
NumWritten: Integer; - Buf:
array[1..2048] of Char; - n:integer;
- begin
-
AssignFile(FromF, sourcef); -
Reset(FromF, 1); { Record size = 1 } -
AssignFile(ToF,targetf); { Open output file } -
Rewrite(ToF, 1); { Record size = 1 } -
n:=0; -
repeat -
BlockRead(FromF, Buf, SizeOf(Buf), NumRead); -
form1.label1.caption:=IntToStr(sizeof(buf)*n*100 div FileSize(FromF))+ '100%'; -
application.ProcessMessages; -
//显示进度 -
BlockWrite(ToF, Buf, NumRead, NumWritten); -
inc(n); -
until (NumRead 0)= or (NumWritten <> NumRead); -
form1.Label1.Caption:='100%'; -
CloseFile(FromF); -
CloseFile(ToF); - end;
- procedure
TForm1 .Button1Click(Sender:TObject); - begin
- mycopyfile('e:/components/tv2k-w2k.zip','c:/a.zip');
- end;
****************************************************************************
22. cxGrid 设置斑马线
解决:
在TcxGridDBBandedTableView
****************************************************************************
23 根据记录内容更改字体颜色
解决:
参考范例CustomDrawTableViewDemo,
主要在TcxGridDBBandedTableView
如下代码:
if
begin //标识负数记录
end;
其中colOrderProductCount是“产品订数”列。
还要有一步就是要刷新显示
TcxGridDBBandedTableView
//tvCars.LayoutChanged(False);
TcxGridDBBandedTableView
****************************************************************************
24 用代码展开/收缩主从结构
解决:
注:tvDepartment为主表对应的TableView
****************************************************************************
25
在内置右键菜单的后面增加菜单项
解决:
首先应在Form上加一个cxGridPopupMenu控件
UseBuildInPopupMenus设为True
-
- procedure
TFormItemList TObject); - var
-
AMenu: TComponent; -
FMenuItem, FSubMenuItem: TMenuItem; - begin
-
AMenu := nil; -
if cxGridPopupMenu = -
Exit; -
AMenu := cxGridPopupMenu.BuiltInPopupMenus[0].PopupMenu; //第一个内置右键菜单(表头菜单) -
if Assigned(AMenu) AMenu -
begin -
TPopupMenu(AMenu).AutoHotkeys := maManual; //手动热键 -
-
//------------------------- -
FMenuItem := TMenuItem.Create(Self); -
FMenuItem.Caption := -
FMenuItem.Name := -
TPopupMenu(AMenu).Items.Add(FMenuItem); -
-
//展开所有组 -
FMenuItem := TMenuItem.Create(Self); -
FMenuItem.Name := -
FMenuItem.Caption := -
FMenuItem.OnClick := miExpandAllGroupClick; -
TPopupMenu(AMenu).Items.Add(FMenuItem); -
-
//收缩所有组 -
FMenuItem := TMenuItem.Create(Self); -
FMenuItem.Name := -
FMenuItem.Caption := -
FMenuItem.OnClick := miCollapseAllGroupClick; -
TPopupMenu(AMenu).Items.Add(FMenuItem); -
-
//------------------------- -
FMenuItem := TMenuItem.Create(Self); -
FMenuItem.Caption := -
TPopupMenu(AMenu).Items.Add(FMenuItem); -
-
//过滤面板 -
FMenuItem := TMenuItem.Create(Self); -
FMenuItem.Name := -
FMenuItem.Caption := -
//自动显示 -
FSubMenuItem := TMenuItem.Create(Self); -
FSubMenuItem.Name := -
FSubMenuItem.Caption := -
FSubMenuItem.RadioItem := True; -
FSubMenuItem.GroupIndex := //指定同一组 -
FSubMenuItem.Checked := True; -
FSubMenuItem.OnClick := miFilterPanelClick; -
FMenuItem.Add(FSubMenuItem); //加入二级子菜单 -
//总是显示 -
FSubMenuItem := TMenuItem.Create(Self); -
FSubMenuItem.Name := -
FSubMenuItem.Caption := -
FSubMenuItem.RadioItem := True; -
FSubMenuItem.GroupIndex := -
FSubMenuItem.OnClick := miFilterPanelClick; -
FMenuItem.Add(FSubMenuItem); -
//从不显示 -
FSubMenuItem := TMenuItem.Create(Self); -
FSubMenuItem.Name := -
FSubMenuItem.Caption := -
FSubMenuItem.RadioItem := True; -
FSubMenuItem.GroupIndex := -
FSubMenuItem.OnClick := miFilterPanelClick; -
FMenuItem.Add(FSubMenuItem); -
TPopupMenu(AMenu).Items.Add(FMenuItem); -
-
//自定义过滤 -
FMenuItem := TMenuItem.Create(Self); -
FMenuItem.Name := -
FMenuItem.Caption := -
FMenuItem.OnClick := miCustomFilterClick; -
TPopupMenu(AMenu).Items.Add(FMenuItem); -
-
//过滤管理器 -
FMenuItem := TMenuItem.Create(Self); -
FMenuItem.Name := -
TPopupMenu(AMenu).Images.AddImage(FormMain.ImageListExtend, //添加图标图像 -
FMenuItem.ImageIndex := TPopupMenu(AMenu).Images - //指定图标序号 -
FMenuItem.Caption := -
FMenuItem.OnClick := Self -
TPopupMenu(AMenu).Items.Add(FMenuItem); -
-
//--------------------- -
FMenuItem := TMenuItem.Create(Self); -
FMenuItem.Caption := -
TPopupMenu(AMenu).Items.Add(FMenuItem); -
-
//导出 -
FMenuItem := TMenuItem.Create(Self); -
FMenuItem.Name := -
TPopupMenu(AMenu).Images.AddImage(FormMain.ImageListExtend, -
FMenuItem.ImageIndex := TPopupMenu(AMenu).Images - -
FMenuItem.Caption := -
FMenuItem.OnClick := Self -
TPopupMenu(AMenu).Items.Add(FMenuItem); -
-
//打印 -
FMenuItem := TMenuItem.Create(Self); -
FMenuItem.Name := -
FMenuItem.Caption := -
TPopupMenu(AMenu).Images.AddImage(FormMain.ImageListExtend, -
FMenuItem.ImageIndex := TPopupMenu(AMenu).Images - -
FMenuItem.OnClick := Self -
TPopupMenu(AMenu).Items.Add(FMenuItem); -
end; - end;
-
- procedure
TFormItemList TObject); - var
-
FileName, FileExt, msg: String; - begin
-
if Self -
begin -
msg := '没有导出数据...'; -
Application.MessageBox(PChar(msg), PChar(Application -
MB_OK or MB_IconWarning); -
Exit; -
end; -
-
Self.SaveDialogExport.Filter := (*.xls)|*.xls|XML文件 (*.xml)|*.xml' -
+ '|文本文件 (*.txt)|*.txt|网页文件 (*.html)|*.html' -
Self.SaveDialogExport.Title := -
-
if Self -
Exit; -
-
FileName := Self.SaveDialogExport.FileName; -
FileExt := LowerCase(ExtractFileExt(FileName)); -
if FileExt = -
ExportGrid4ToExcel(FileName, Self.cxGrid1) -
else FileExt = -
ExportGrid4ToXML(FileName, Self.cxGrid1) -
else FileExt = -
ExportGrid4ToText(FileName, Self.cxGrid1) -
else FileExt = -
ExportGrid4ToHTML(FileName, Self.cxGrid1) -
else -
begin -
msg := '不支持的导出文件类型...'; -
Application.MessageBox(PChar(msg), PChar(Application -
MB_OK or MB_IconError); -
Exit; -
end; -
-
msg := '导出完成...'; -
Application.MessageBox(PChar(msg), PChar(Application -
MB_OK or MB_IconInformation); - end;
-
- procedure
TFormItemList TObject); - begin
-
//打印 -
Self.dxComponentPrinter.Preview(True, Self - end;
-
- procedure
TFormItemList TComponent; -
AHitTest: TcxCustomGridHitTest; X, Y: Integer; var AllowPopup: Boolean); - begin
-
if GetHitTypeByHitCode(AHitTest = gvhtColumnHeader //右击列标题时 -
begin -
//if tvResult.DataController.Groups.GroupingItemCount > 0 then -
if tvResult > //有分组时显示 -
begin -
TMenuItem(Self.FindComponent('miLineForGroup')).Visible := True; -
TMenuItem(Self.FindComponent('miExpandAllGroup')).Visible := True; -
TMenuItem(Self.FindComponent('miCollapseAllGroup')).Visible := True; -
end -
else -
begin -
TMenuItem(Self.FindComponent('miLineForGroup')).Visible := False; -
TMenuItem(Self.FindComponent('miExpandAllGroup')).Visible := False; -
TMenuItem(Self.FindComponent('miCollapseAllGroup')).Visible := False; -
end; -
end; - end;
-
- procedure
TFormItemList TObject); - begin
-
//过滤管理器 -
//弹出Filter Builder Dialog对话框 -
tvResult.Filtering.RunCustomizeDialog; - end;
-
- procedure
TFormItemList TObject); - var
-
AHitTest: TcxCustomGridHitTest; - begin
-
//自定义过滤 -
//弹出Custom Filter Dialog对话框 -
AHitTest := cxGridPopupMenu.HitTest; -
if GetHitTypeByHitCode(AHitTest = gvhtColumnHeader //获得右击的列 -
tvResult.Filtering.RunCustomizeDialog(TcxGridColumnHeaderHitTe st(AHitTest).Column); - end;
-
- procedure
TFormItemList TObject); - var
-
mi: TMenuItem; - begin
-
//隐藏/显示过滤面板 -
mi := TMenuItem(Sender); -
mi.Checked := True; -
if mi = -
tvResult.Filtering.Visible := fvAlways -
else mi = -
tvResult.Filtering.Visible := fvNever -
else -
tvResult.Filtering.Visible := fvNonEmpty; - end;
-
- procedure
TFormItemList TObject); - begin
-
//展开所有组 -
tvResult.DataController.Groups.FullExpand; - end;
-
- procedure
TFormItemList TObject); - begin
-
//收缩所有组 -
tvResult.DataController.Groups.FullCollapse; - end;
****************************************************************************
26
根据某列的值设定其它列的可编辑性
解决:
procedure
begin
end;
****************************************************************************
27 保存/恢复Grid布局
解决:
网格左上角的自定义布局按钮:
TableView-?OptionsCustiomize?ColumnsQuickCustomizatio
- //恢复布局
-
IniFileName
:= ExtractFilePath(Application.ExeName) + + Self + - if
FileExists(IniFileName) -
Self.tvResult.RestoreFromIniFile(IniFileName) //从布局文件中恢复 - else
- begin
-
Self.tvResult.BeginUpdate; -
for i := Self - -
Self.tvResult.Items[i].ApplyBestFit; //调整为最佳宽度 -
Self.tvResult.EndUpdate; - end;
-
- //保存布局
-
IniFileName
:= ExtractFilePath(Application.ExeName) + + Self + - if
DirectoryExists(ExtractFileDir(IniFileName)) -
CreateDir(ExtractFileDir(IniFileName)); - Self.tvResult.StoreToIniFile(IniFileName);
//保存为布局文件 -
-
-
实例:
- IniFileName:
string; -
- procedure
TMainFM .FormCreate(Sender:TObject); //窗体创建时读取布局 - var
i: Integer; - begin
-
qyHed.Open; -
IniFileName := ExtractFilePath(Application.ExeName) + '/Layout/'+ .Owner.ClassNamecxGrd + .NamecxGrd + '.ini'; -
if FileExists(IniFileName) then -
Self.cxTbv.RestoreFromIniFile(IniFileName) //从布局文件中恢复 -
else -
begin -
Self.cxTbv.BeginUpdate; -
for i 0:= to Self .cxTbv.ItemCount- 1do -
Self.cxTbv.Items[i].ApplyBestFit; //调整为最佳宽度 -
Self.cxTbv.EndUpdate; -
end; - end;
-
- procedure
TMainFM .NSaveGrdClick(Sender:TObject); //保存布局文件 - begin
-
try -
IniFileName := ExtractFilePath(Application.ExeName) + '/Layout/'+ .Owner.ClassNamecxGrd + .NamecxGrd + '.ini'; -
if not DirectoryExists(ExtractFileDir(IniFileName)) then -
CreateDir(ExtractFileDir(IniFileName)); -
Self.cxTbv.StoreToIniFile(IniFileName); -
except -
end; - end;
****************************************************************************
28保存/恢复带汇总行的布局解决:
.StoreToIniFile('c:/Grid.ini',
.RestoreFromIniFile(,True,False{or
True,optional},[gsoUseSummary]);
****************************************************************************
28
在主从TableView中根据主TableView得到对应的从TableView
解决:
var
begin
end;
==============================================================================
29 定位在第一行并显示内置编辑器
cxDBVerticalGrid1.FocusedRow :=
cxDBVerticalGrid1.Rows[0];
cxDBVerticalGrid1.ShowEdit;
==============================================================================
30 隐藏 "" 字符串
该文本存储在scxGridNoDataInfoText资源字符串,可以将该资源字符串的内容设为空
来隐藏该文本。
uses cxClasses, cxGridStrs;
...
cxSetResourceString(@scxGridNoDataInfoText, '');
//如果"" 字符串已经显示,需要调用:
.LayoutChanged;
============================================================
31 删除应用过滤后的行
var
I: Integer;
begin
with do
for I := 0 to ViewData.RecordCount - 1 do
begin
ViewData.Records[0].Focused := True;
DataController.DataSet.Delete;
end;
=============================================================
32
根据单元的值设置样式
procedure
begin
end;
procedure
var
begin
end;
======================================================================
TcxCustomGridTableView.FindItemByName,
TcxGridDBDataController.GetItemByFieldName
****************************************************************************
33 动态生成BandedView
解决:
var
begin