第一种方式:直接双击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
在 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
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左右,因为数据记录过多会出现重叠现象(数字和指针图标在一起了)!