需要在DBGrid增加一列作行号。
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, DB, ADODB, Grids, DBGrids;
type
TForm1 = class(TForm)
DataSource1: TDataSource;
DBGrid1: TDBGrid;
ADOConnection1: TADOConnection;
ADOTable1: TADOTable;
procedure DataSource1StateChange(Sender: TObject);
procedure DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
DataCol: Integer; Column: TColumn; State: TGridDrawState);
private
{ Private declarations }
FirstRecNo: integer;
public
{ Public declarations }
end;
TDataSetEx= class(TDataSet);
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.DataSource1StateChange(Sender: TObject);
begin
with TDataSource(Sender) do
begin
FirstRecNo := DataSet.RecordCount - TDataSetEx(DataSet).ActiveRecord;
end;
end;
procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
DataCol: Integer; Column: TColumn; State: TGridDrawState);
var
i: integer;
begin
Inherited;
with TDBGrid(sender) do
begin
if (DataCol =0) then
begin
if DataSource.State <> DsInsert then
i:= DataSource.DataSet.RecNo
else
i:= FirstRecNo + Rect.Top div 19;
Canvas.TextRect(rect,Rect.Left+2, Rect.Top+2, inttostr(i));
end;
end;
end;
end.