由于公司业务的关系,又要写回Delphi程序。以下是为完成客户需求时所写的一些功能需求代码,作为又一次的记录。
IDE版本:Delphi6, 控件版本:DevExpress4.5
先来个D6截图,缅怀一下
CxGrid的强大不容置疑,但也带来的繁多的属性与复杂的操作,加上能找到的中文说明不多,所以很多时候只能通过看例子和自己摸索。
一、代码实现分组
CxGrid可以实现客户端本地分组查询,但很多时候需要用户自己动鼠标拖动实现。如要程序自动实现,很多时候需要靠代码完成。以下为代码:
procedure SetCxGridGroup(cxTableView:TcxGridDBTableView; groupField : string; fields : array of string; SumKinds:array of TcxSummaryKind);
参数介绍:
cxTableView: TcxGridDBTableView对象;
groupField:分组字段名
fields:要实现统计的字段名数组
SumKinds:要统计字段的统计类型
procedure TForm.SetCxGridGroup(cxTableView:TcxGridDBTableView; groupField : string; fields : array of string; SumKinds:array of TcxSummaryKind);
var
i,j : Integer;
csg: TcxDataSummaryGroup;
col: TcxGridDBColumn;
item : TcxGridDBTableSummaryItem;
begin
ifcxTableView.DataController.DataSource.DataSet.Active then
begin
cxTableView.OptionsView.GroupByBox := False; //关闭分组栏
cxTableView.OptionsView.Footer := true; //显示页脚栏
cxTableView.OptionsView.GroupFooters:= gfVisibleWhenExpanded; //显示分组页脚栏
csg := cxTableView.DataController.Summary.SummaryGroups.Add; //增加分组对象
//循环CxGrid列
for i := 0 to cxTableView.ColumnCount - 1 do
begin
if cxTableView.Columns[i].DataBinding.FieldName = groupField then //通过对比字段名,增加分组
begin
//创建分组
col := cxTableView.Columns[i];
col.Caption:= cxTableView.Columns[i].Caption;
col.GroupIndex := 0;
end else
begin
for j := Low(fields) to High(fields) do
begin
if cxTableView.Columns[i].DataBinding.FieldName = fields[j] then //增加分组统计
begin
item := TcxGridDBTableSummaryItem(cxTableView.DataController.Summary.DefaultGroupSummaryItems.Add);
item.Column := cxTableView.Columns[i];
item.Kind := SumKinds[j];
item.Position := spFooter;
Break;
end;//if
end;//for
end;//else
end;//for
cxTableView.DataController.Groups.FullExpand;
end;//if
end;
效果图
二、指定字段实现下拉框
Uses cxDropDownEdit;
...
cxTableView.GetColumnByFieldName('type').PropertiesClass := TcxComboBoxProperties;
TcxComboBoxProperties(cxTableView.GetColumnByFieldName('type').Properties).DropDownListStyle := lsFixedList;
TcxComboBoxProperties(cxTableView.GetColumnByFieldName('type').Properties).Items.Add('A-Type');
三、行复制
var
i, j : Integer;
temp : string; //临时累加数据值
list : TStrings; //累加数据值
edt : TCustomEdit; //临时Edit,用于将数据拷贝到内存中,利用Edit偷个懒
aDS : TDataSet; //CxGrid的DataSet
begin
list := TStringList.Create;
edt := TCustomEdit.Create(Self);//生成临时Edit
edt.Visible := False;
edt.Parent := Self;
try
aDS := cxTableView.DataController.DataSource.DataSet;
for i := 0 to cxTableView.Controller.SelectedRowCount - 1 do
begin
if cxTableView.Controller.SelectedRecords[i].HasCells then //判断是否有单元格,防止拷贝到分组项
begin
temp := '';
aDS.Locate(keyField, cxTableView.Controller.SelectedRows[i].Values[cxTableView.GetColumnByFieldName(keyField).Index],[]);
for j := 0 to aDS.FieldCount - 1 do
temp := temp + aDS.FieldByName(aDS.Fields.Fields[j].FieldName).AsString + #9;
list.Add(temp);
end;
end;
edt.Text := list.Text;
edt.SelectAll;
edt.CopyToClipboard;
finally
if Assigned(edt) then
FreeAndNil(edt);
if Assigned(list) then
FreeAndNil(list);
end;
如果CxGrid不进行分组,也可以这样写
var
i,j : Integer;
temp : string;
list : TStrings;
edt: TCustomEdit;
r :TcxRowInfo;
begin
list := TStringList.Create;
edt := TCustomEdit.Create(Self);
edt.Visible := False;
edt.Parent := Self;
try
for i := 0 to cxTableView.Controller.SelectedRowCount - 1 do
begin
r.RecordIndex :=aCXTV.Controller.SelectedRecords[i].Index;
temp := '';
for j := 0 to aCXTV.ItemCount - 1 do
temp := temp +aCXTV.DataController.GetRowDisplayText(r, j) + #9;
list.Add(temp);
end;
edt.Text := list.Text;
edt.SelectAll;
edt.CopyToClipboard;
finally
if Assigned(edt) then
FreeAndNil(edt);
if Assigned(list) then
FreeAndNil(list);
end;
四、拷贝禁止修改的单元格值
该处有误,为不误导广大市民就删了
五、获取鼠标所选单元格值
开始用了
cxTableView.Controller.FocusedRow.Values[cxTableView.Controller.FocusedColumnIndex];
后来发现以上写法,在有隐藏列的情况下会有列错位的问题,一般会隐藏几列,就会错几位。
经查看
row := cxTV.Controller.FocusedRowIndex;
col := cxTV.Controller. FocusedColumnIndex;
这两个得到的是显示的位置下标输出的值,而无论cxTV.Controller.FocusedRow.Values[col];
还是cxTV.Controller.FocusedRow.DisplayTexts[col];
得到的都是包括隐藏列的值后来有找了一下,发现以下这句才是
cxTableView.Controller.FocusedColumn.Index
唉~!FocusedColumnIndex 这个名字太有误导性了