my get record number function, bug for help

  I have wrote my own GetRowNum function to get the record number of the
DBGrid. It works when I invoke it in the keydown handler of DBGrid. But
when I call it in my DrawDataCell event handler of DBGrid, it cause a
infinite loop. Can anyone help me to debug it? I paste my code as follow.

In my form, there are 2 label components, 1 DBGrid, 1 Table, and 1 datasource.
As running, if you press F1 in the DBGrid's cell it can report position in
DBGrid. Now I need to invoke it in my DrawDataCell event handler, I put it
in the handler. When running, it enter a infinite loop to draw data cell.
I found that if I remove the code line "DisableControls" of my function,
the loop will be finite though the result is not what I want.

Here is my code. Have anyone help me to debug?
----------------------------------------------------------------
unit Rowno;

interface

uses
  SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
  Forms, Dialogs, Grids, DBGrids, DB, DBTables, Menus, StdCtrls, Buttons;

type
  TForm1 = class(TForm)
    DataSource1: TDataSource;
    DBGrid1: TDBGrid;
    Table1: TTable;
    Label1: TLabel;
    Label2: TLabel;
    procedure DBGrid1DrawDataCell(Sender: TObject; const Rect: TRect;
      Field: TField; State: TGridDrawState);
    procedure FormCreate(Sender: TObject);
    procedure DBGrid1KeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    function  GetRowNum(DataSet: TDataSet): Longint;
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  i: Integer;
  Row,Col: Longint;
  flag : array[0..120,0..15] of Boolean;

implementation

{$R *.DFM}
function  TForm1.GetRowNum(DataSet: TDataSet):Longint;
begin
  Result:=0;
  with Dataset do
  begin
    if State=dsInactive then
      ShowMessage('Cannot perform this operation on a closed dataset');
    if State=dsBrowse then
    begin
      DisableControls; { remove this line can abvoid infinite loop}
      repeat
        Prior;
        Result:=Result+1;
      until BOF;
      Result:=Result-1;
    end;
    MoveBy(Result);
    EnableControls;
  end;
end;

procedure TForm1.DBGrid1DrawDataCell(Sender: TObject; const Rect: TRect;
  Field: TField; State: TGridDrawState);
  var
    RowHeight,i,j: Integer;
begin
     {Row:=GetRowNum(DataSource1.DataSet);} {commend this line, just use F1 test
                                            GetRowNum(), GetRowNum seems ok}
     Col:=Field.Index;
       with (Sender as TDBGrid).Canvas do
         if not flag[Row,Col] then
           begin
             Brush.Color:=clWhite;
             FillRect(Rect);
             TextOut(Rect.Left,Rect.Top,Field.AsString);
           end
         else
           begin
             Brush.Color:=clRed;
             FillRect(Rect);
             TextOut(Rect.Left,Rect.Top,Field.AsString);
           end;
     if gdFocused in State then
       begin
         with (Sender as TDBGrid).Canvas do
         begin
           Brush.Color:=clBlue;
           FillRect(Rect);
           TextOut(Rect.Left,Rect.Top,Field.AsString);
         end;
       end;
end;

procedure TForm1.FormCreate(Sender: TObject);
var
 i,j : integer;
begin
for i:= 0 to 120 do
  for j:= 0 to 15 do
    flag[i,j]:=False;
end;

procedure TForm1.DBGrid1KeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
var
  RowHeight: Integer;
begin
  if (key=VK_F1) then
  begin
  Row:=GetRowNum(DataSource1.DataSet);
  Col:=TDBGrid(Sender).SelectedField.Index;
  label1.caption:='Col No: '+inttostr(col);
  label2.caption:='Row No: '+inttostr(row);
  flag[Row,Col]:= not flag[Row,Col];
  DBGrid1.invalidate;
  end;
end;

initialization
end.
--
Ingram, I. K. Tsaur
Email: iiidns.iii.org.tw
       adc.iii.org.tw