第一种方法:直接双击events中的对应OnCustomDrawIndicatorCell事件
代码参考第二种
第二种方法:使用封装类,建立储存过程,事件中调用
dsDataExport.DataSet:=CDS_DataExport3;
CDS_DataExport3.Close; //添加字段前要关闭数据集
AddDummyField(CDS_DataExport3,TIntegerField,'Findrow',10); //动态添加字段
CDS_DataExport3.Open;
CxGridCreateColumns(cxGrid1DBBandedTableView1, CDS_DataExport3); //查询并创建列
cxGrid1DBBandedTableView1.OptionsView.Indicator := True; //显示序号
cxGrid1DBBandedTableView1.OptionsView.IndicatorWidth := 40; //序号宽度
cxGrid1DBBandedTableView1.OnCustomDrawIndicatorCell := DrawIndicatorCell;//过程
procedure TfrmDataExport.DrawIndicatorCell(Sender: TcxGridTableView;
ACanvas: TcxCanvas; AViewInfo: TcxCustomGridIndicatorItemViewInfo;
var ADone: Boolean);
var
FValue: string;
FBounds: TRect;
AIndicatorViewInfo: TcxGridIndicatorRowItemViewInfo;
ATextRect: TRect;
AFont: TFont;
AFontTextColor, AColor: TColor;
begin
AFont := ACanvas.Font;
AColor := clBtnFace;
AFontTextColor := clWindowText;
if (AViewInfo is TcxGridIndicatorHeaderItemViewInfo) then //设置显示行号那一列的标题名称为“序号”
begin
ATextRect := AViewInfo.Bounds;
InflateRect(ATextRect, -1, -1);
Sender.LookAndFeelPainter.DrawHeader(ACanvas, AViewInfo.Bounds,
ATextRect, [], cxBordersAll, cxbsNormal, taCenter, vaCenter, //TcxAlignmentVert.vaCenter,
False, False, '序号', AFont, AFontTextColor, AColor);
ADone := True;
end;
if not (AViewInfo is TcxGridIndicatorRowItemViewInfo) then
Exit;
ATextRect := AViewInfo.ContentBounds;
AIndicatorViewInfo := AViewInfo as TcxGridIndicatorRowItemViewInfo;
InflateRect(ATextRect, -1, -1);
// if AIndicatorViewInfo.GridRecord.Selected then //这个if段是为了在行号处把把选中的行号跟别的区分开,可不用
// begin
// AFont.Style := ACanvas.Font.Style + [fsBold];
// AFont.Name := '黑体';
// AFont.Size := 12;
// AFont.Color := clRed;
// end
// else
// begin
// AFont.Style := ACanvas.Font.Style - [fsBold];
// end;
if AIndicatorViewInfo.GridRecord.Selected then //这个if段是为了在行号处把把选中的行号跟别的区分开,可不用
begin
ACanvas.Font.Style := ACanvas.Font.Style + [fsBold];
ACanvas.Font.Color := clRed;
end
else
begin
ACanvas.Font.Style := ACanvas.Font.Style - [fsBold];
ACanvas.Font.Color := ACanvas.Font.Color;
end;
Sender.LookAndFeelPainter.DrawHeader(ACanvas, AViewInfo.ContentBounds,
ATextRect, [], [bBottom, bLeft, bRight], cxbsNormal, taCenter, vaCenter, //TcxAlignmentVert.vaCenter,
False, False, IntToStr(AIndicatorViewInfo.GridRecord.Index + 1),
AFont, AFontTextColor, AColor);
ADone := True;
end;