CXGRID动态创建字段的过程

cxGrid实现多表头,在网上也有不少文章。但是总感觉不用着不顺手。

结合DBGridEH的多表头实现方法,为了保持两者的兼容(还是个人习惯作祟吧)。主要代码如下:

****一、function AddGridColumn(aGrid: TcxGridTableView; aCaption, aField: string; aWidth: integer = 40;
aSumKind: string = ‘’; aVisible: Boolean = True; Edt: Boolean = True; Mov: Boolean = True): TcxGridDBColumn;
var
tmpPos: integer;
BandCap: string;
BandView: TcxGridDBBandedTableView;
Band: TcxGridBand;
BandCol: TcxGridDBBandedColumn;
function FindBandedByCaption(aCaption: string): TcxGridBand;
var
I: Integer;
Find: Boolean;
begin
Find := False;
Result := nil;
for I := BandView.Bands.Count - 1 downto 0 do//从最后一个找起,因为同一个表头的字段往往会排列在一起。
begin
Result := BandView.Bands.Items[I];
if Result.Caption = aCaption then
begin Find := True; Exit; end;
end;
if not Find then//如果没有找到,就创建新的
begin
Result := BandView.Bands.Add;
Result.Caption := aCaption;
end;
end;
begin
Result := TcxGridDBColumn(aGrid.CreateColumn);
{AddCol(Result, aCaption, aField, aWidth, aSumKind, aVisible);
Result.Options.Editing := Edt;
Result.Options.Moving := Mov; }
AddGridColumn(Result, aCaption, aField, aWidth, aSumKind, aVisible);
//处理多表头问题
if aGrid is TcxGridDBBandedTableView then
begin
BandView := TcxGridDBBandedTableView(aGrid);
tmpPos := Pos(’|’, aCaption);//借鉴DBGridEh的格式
if tmpPos > 0 then
begin
BandCap := Copy(aCaption, 1, tmpPos - 1);
Delete(aCaption, 1, tmpPos);
end
else
BandCap := aCaption;
Band := FindBandedByCaption(BandCap);
BandCol := TcxGridDBBandedColumn(Result);
BandCol.Position.BandIndex := Band.Position.ColIndex;
Result.Caption := aCaption;

end;
end;****
二、下面是动态创建字段的过程。(不需要多表头时,可以直接使用这个过程)

function AddGridColumn(aCol: TcxGridDBColumn; aCaption, aField: string; aWidth: integer = 40; aSumKind: string = ‘’; aVisible: Boolean = True; Edt: Boolean = True; Mov: Boolean = True): TcxGridDBColumn;
begin
aCol.HeaderGlyphAlignmentHorz := taCenter;
aCol.HeaderAlignmentHorz := taCenter;
aCol.DataBinding.FieldName := aField;
aCol.Caption := aCaption;
aCol.Width := aWidth;
aCol.Visible := aVisible;
//这里要考虑数据集是否打开,否则不起作用
if aCol.DataBinding.Field is TNumericField then
TNumericField(aCol.DataBinding.Field).DisplayFormat := ‘#0.00’;
if aSumKind = ‘求和’ then
begin
aCol.Summary.FooterKind := skSum;
aCol.Summary.GroupFooterKind := skSum;
aCol.Summary.GroupKind := skSum;
aCol.Summary.FooterFormat := ‘#0.00’;
end
else if (aSumKind = ‘计数’) then
begin
aCol.Summary.FooterKind := skCount;
aCol.Summary.GroupFooterKind := skCount;
aCol.Summary.GroupKind := skCount;
end
else if aSumKind = ‘合计’ then
begin
aCol.Summary.FooterKind := skCount;
aCol.Summary.GroupFooterKind := skCount;
aCol.Summary.GroupKind := skCount;
aCol.Summary.FooterFormat := ‘合计’; //实现显示‘合计’效果----简洁有效
aCol.Summary.GroupFooterFormat := ‘合计’;
aCol.Summary.GroupFormat := ‘合计’;
//aCol.Summary.Item.OnGetDisplayText := THeJiClass.GetDisplayText;
end
else if (aSumKind = ‘平均值’) then
begin
aCol.Summary.FooterKind := skAverage;
aCol.Summary.GroupFooterKind := skAverage;
aCol.Summary.GroupKind := skAverage;
aCol.Summary.FooterFormat := ‘#0.00’;
end;
Result := aCol;
Result.Options.Editing := Edt;
Result.Options.Moving := Mov;
end;

————————————————
版权声明:本文为CSDN博主「ghs79」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/ghs79/article/details/78950436

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值