获得序号的代码:
procedure TEmpListForm.DBGridEh1DrawColumnCell(Sender: TObject;
const Rect: TRect;
DataCol: Integer;
Column: TColumnEh;
State: TGridDrawState);
function GetRowIndex: Integer;
//获取正在画的是第几行
var
J : Integer;
begin
Result := 1;
with TStringGrid(Sender)do
begin
for J := 1 to VisibleRowCountdo
begin
if CellRect(0, J).Top = Rect.Top then
begin
Result := J;
Break;
end;
end;
end;
end;
var
Id : Integer;
begin
if TDBGridEh(Sender).DataSource.DataSet.IsEmpty then
exit;
with TDBGridEh(Sender)do
begin
if DataCol = 0 then
begin
if DataSource.DataSet.State = DsInsert then
begin
if DataSource.DataSet.Eof then
Id:=DataSource.DataSet.RecordCount-TStringGrid(Sender).Row+GetRowIndex+1 //append 状态
else
Id := FirstRecNo+GetRowIndex;
//insert 状态
end
else
Id := DataSource.DataSet.RecNo;
//浏览或编辑状态
Canvas.TextRect(Rect,
Rect.Left+(Rect.Right-Rect.Left-Canvas.TextWidth(IntToStr(Id))) div 2,
Rect.Top+(Rect.Bottom-Rect.Top-Canvas.TextHeight(IntToStr(Id))) div 2,
IntToSTr(Id));
end;
end;
end;