cxGrid显示行号

第一种方法:直接双击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;
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值