cxGrid显示行号的两种方式和两种代码

第一种方式:直接双击events中的对应OnCustomDrawIndicatorCell事件,使用此代码

//==============================================================================

// 显示cxGrid的行号
//==============================================================================

procedure TFormMainOperation.cxgrddbbandtv1CustomDrawIndicatorCell(
  Sender: TcxGridTableView; ACanvas: TcxCanvas;
  AViewInfo: TcxCustomGridIndicatorItemViewInfo; var ADone: Boolean);
var
  AIndicatorViewInfo: TcxGridIndicatorRowItemViewInfo;
  ATextRect: TRect;
 // AStyle: TcxStyle;
  aCV: TcxCanvas;
begin
  if not (AViewInfo is TcxGridIndicatorRowItemViewInfo) then
    Exit;
  aCV := ACanvas;
  ATextRect := AViewInfo.ContentBounds;
  AIndicatorViewInfo := AViewInfo as TcxGridIndicatorRowItemViewInfo;
  InflateRect(ATextRect, -2, -1);
  if AIndicatorViewInfo.GridRecord.Selected then //这个if段是为了在行号处把把选中的行号跟别的区分开,可不用
    begin
      aCV.Font.Style := Canvas.Font.Style + [fsBold];
      aCV.Font.Name := '黑体';
      aCV.Font.Size := 12;
      aCV.Font.Color := clRed;
    end
  else
    begin
      aCV.Font.Style := Canvas.Font.Style - [fsBold];
      acv.Font.Color := Canvas.Font.Color;
    end;
  Sender.LookAndFeelPainter.DrawHeader(ACanvas, AViewInfo.ContentBounds,
    ATextRect, [], cxBordersAll, cxbsNormal, taCenter, vaCenter,
    False, False, IntToStr(AIndicatorViewInfo.GridRecord.Index + 1),
//    AStyle.Font, AStyle.TextColor, AStyle.Color);
    acv.Font, acv.font.Color, acv.Brush.color);
  ADone := True;

end;


第二种方式:使用封装类,建立储存过程,Form初始化OnCreate事件中调用

1、建立封装类和调用函数

unit UnitDevExpressVCLCustom;

interface


uses
  cxGridTableView, cxGridDBBandedTableView, Winapi.Windows, System.SysUtils, cxClasses,
  System.Classes, Vcl.Graphics, cxGraphics, cxLookAndFeels, cxLookAndFeelPainters;


type
  TMyCxGrid = class(TObject)


    class procedure DrawIndicatorCell(Sender: TcxGridTableView; ACanvas: TcxCanvas;
      AViewInfo: TcxCustomGridIndicatorItemViewInfo; var ADone: Boolean);


  end;


  procedure ShowLineNo(cxGridDBBandedTableView: TcxGridDBBandedTableView);


implementation


{-------------------------------------------------------------------------------
  作者:      曾聪
  日期:      2013.10.07
  过程名:    TMyCxGrid.DrawIndicatorCell
  功能:      显示cxGrid的行号
  参数:      Sender: TcxGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxCustomGridIndicatorItemViewInfo; var ADone: Boolean
  返回值:    无
  说明:
-------------------------------------------------------------------------------}


class procedure TMyCxGrid.DrawIndicatorCell(Sender: TcxGridTableView; ACanvas: TcxCanvas;
  AViewInfo: TcxCustomGridIndicatorItemViewInfo; var ADone: Boolean);
var
  FValue: string;
  FBounds: TRect;
  //method 2
  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, 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;
  Sender.LookAndFeelPainter.DrawHeader(ACanvas, AViewInfo.ContentBounds,
    ATextRect, [], [bBottom, bLeft, bRight], cxbsNormal, taCenter, TcxAlignmentVert.vaCenter,
    False, False, IntToStr(AIndicatorViewInfo.GridRecord.Index + 1),
    AFont, AFontTextColor, AColor);
  ADone := True;


end;

{-------------------------------------------------------------------------------
  作者:      曾聪
  日期:      2013.10.07
  过程名:    ShowLineNo
  功能:      设置cxGrid显示导航列、导航列宽、调用显示行号函数
  参数:      cxGridDBBandedTableView: TcxGridDBBandedTableView
  返回值:    无
  说明:
-------------------------------------------------------------------------------}
procedure ShowLineNo(cxGridDBBandedTableView: TcxGridDBBandedTableView);
begin
  cxGridDBBandedTableView.OptionsView.Indicator := True;
  cxGridDBBandedTableView.OptionsView.IndicatorWidth := 30;
  cxGridDBBandedTableView.OnCustomDrawIndicatorCell := TMyCxGrid.DrawIndicatorCell;
end;

end.


2、在Form的OnCreate事件中调用:

procedure TFormMainOperation.FormCreate(Sender: TObject);
begin
  ShowLineNo(cxgrddbbandtv1);
end;




以上代码如果需要在右侧仍然显示指针效果:

1、在最后一行end之前增加一行代码:

Sender.LookAndFeelPainter.DrawIndicatorImage(ACanvas, ATextRect, AIndicatorViewInfo.IndicatorKind);

2、DrawIndicatorImage 从这里跟进去(Ctrl+左键单击) 
在 cxLookAndFeelPainters 单元中作如下修改: 
class procedure TcxCustomLookAndFeelPainter.DrawIndicatorImage(ACanvas: TcxCanvas; 
   const R: TRect; AKind: TcxIndicatorKind); 
var 
   X, Y: Integer; 
begin 
   if AKind = ikNone then Exit; 
   with cxIndicatorImages, R do 
   begin 
     X := (Left + Right - Width);               //靠右,在各函数只修改这一个地方 
     Y := (Top + Bottom - Height) div 2;       //居中 
   end; 
   cxIndicatorImages.Draw(ACanvas.Canvas, X, Y, Ord(AKind) - 1); 
end; 
注意,我已注明靠右的那一行, 就是去掉 DIV 2 了, 此为1226版本的修改,13.1.2修改类似也是去掉DIV 2,但是左右宽的代码使用的是变量R.Left等
3、还要改一个地方: 
SKIN控件目录下的dxSkinLookAndFeelPainter单元,找到 
TdxSkinLookAndFeelPainter.DrawIndicatorImage 函数 
的 
OffsetRect(ARect, (Left + Right - cx div 2) , (Top + Bottom - cy) div 2); 
这一行,将 (Left + Right - cx div 2) 改为(Left + Right - cx) 也是去掉 div 2 就是靠右; 
修改后: OffsetRect(ARect, (Left + Right - cx) , (Top + Bottom - cy) div 2); 
使用 
procedure TForm1.cxGrid1DBTableView1CustomDrawIndicatorCell( 
   Sender: TcxGridTableView; ACanvas: TcxCanvas; 
   AViewInfo: TcxCustomGridIndicatorItemViewInfo; var ADone: Boolean); 
begin 
     SetRowNumber(Sender,AviewInfo,ACanvas,ADone); 
end; 
序号列的列宽最好改为40左右,因为数据记录过多会出现重叠现象(数字和指针图标在一起了)!




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
CxGrid是一个非常强大和灵活的表格控件,可以用于显示和编辑数据。它具有许多吸引人和实用的功能,使得它成为选择grid的理想之一。 首先,CxGrid可以提供多种多样的数据显示选项。它可以以表格的形式展示数据,这使得数据更加清晰和易于阅读。此外,CxGrid还可以将数据以树形结构展示,使得用户可以更好地组织和管理数据。另外,CxGrid还支持多级分组和排序,可以根据某一列或者多列进行排序和分组,方便用户快速查找和筛选数据。 其次,CxGrid具有丰富的数据编辑功能。用户可以通过单击编辑单元格来修改数据,也可以直接在编辑框中输入新的值。此外,CxGrid还支持复选框、下拉框、日期选择等不同类型的数据输入和编辑方式。对于需要批量编辑数据的情况,CxGrid还可以启用批量编辑模式,使得用户可以同时修改多行数据。 另外,CxGrid还提供了一些高级功能,如过滤器和表达式编辑器。用户可以根据特定的条件来过滤数据,只显示符合条件的数据。同时,用户还可以使用表达式编辑器自定义计算列,根据特定的表达式对数据进行计算和处理。 此外,CxGrid还允许用户对表格进行自定义布局和外观设置。用户可以自由调整列的顺序和宽度,设置列的对齐方式和格式。同时,CxGrid提供了丰富的皮肤和样式选项,可以让用户根据自己的喜好来美化表格外观。 综上所述,CxGrid具有丰富的功能和灵活的设计,使得它成为选择grid的理想之一。无论是数据展示还是数据编辑,CxGrid都能满足用户的需求,并提供良好的用户体验。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值