Board index » delphi » Ascertaining Current Row Number in a DBGrid ?

Ascertaining Current Row Number in a DBGrid ?

OK, here's my first dumb question to this group, no doubt it won't be
my last.

Scenraio: I've hooked up a DBGrid to a database, and retiived with a
TQuery.  I can find out the number of tuples that satisfied the query.
I can't seem to find a way to show the user that he is currently
positioned on row X of Y total retireved.  I've got the solution to
finding out what row of the current grid window, but not the total
retrieved.  Please don't tell me that I've got to keep track of the
scrolling and paging of the grid...

Summary : How do I find out the current row of the data in a DBGrid
when the number of lines in the grid is less than the total number of
tuples retrieved?

Any pointers (no pun intended) ?

Thanks

 

Re:Ascertaining Current Row Number in a DBGrid ?


In <3v6ij6$...@Owl.nstn.ca>, bor...@fox.nstn.ca (Bruce Organ) writes:

Quote
>OK, here's my first dumb question to this group, no doubt it won't be
>my last.

>Scenraio: I've hooked up a DBGrid to a database, and retiived with a
>TQuery.  I can find out the number of tuples that satisfied the query.
>I can't seem to find a way to show the user that he is currently
>positioned on row X of Y total retireved.  I've got the solution to
>finding out what row of the current grid window, but not the total
>retrieved.  Please don't tell me that I've got to keep track of the
>scrolling and paging of the grid...

The solution that I use might be not the best one but it works fine for me.
In the form that contains TQuery, I have:

  TGridForm = class(TForm)
        ...
    m_dsrcRead: TDataSource;  { source that connects to your query, in this case m_qryRead }
    m_dbgrList: TDBGrid;
    m_qryRead: TQuery;
    m_panelStatusBar: TPanel;
    m_panelRecNo: TPanel;   { panel that displays record number }
    m_nTotalRecs : longint;   { total number of records retreived }
    m_lstRecIDs : TStringList; { list to hold records unique keys, it's up to you what to use as a key as you'll see later }

    procedure ReadQryAfterOpen(DataSet: TDataset);
    procedure OnReadSourceDataChange(Sender: TObject; Field: TField);
    procedure UpdateStatusBar;
  end;

Then in the implementation:

Each time your data set refreshes the following procedure is called which fills m_lstRecIDs with new values:

procedure TSQLListForm.ReadQryAfterOpen(DataSet: TDataset);
begin
  m_lstRecIDs.Clear;
  m_lstRecIDs.Sorted := False;
  m_qryRead.DisableControls;
  while not m_qryRead.EOF do
  begin
    m_lstRecIDs.Add( m_qryRead.FieldByName('KeyField1').AsString + m_qryRead.FieldByName('KeyField2').AsString);
    m_qryRead.Next;
  end;
  m_qryRead.First;  { this is necessary to prevent out of range error in the statement that follows }
  m_qryRead.EnableControls;
  m_nTotalRecs := m_lstRecIDs.Count;
  m_qryRead.First;
  m_qryRead.EnableControls;
  UpdateStatusBar;
end;

Each time the user moves to a different record ( not matter how ) the following is called:
procedure TSQLListForm.OnReadSourceDataChange(Sender: TObject; Field: TField);
begin
  UpdateStatusBar;
end;

Then updating the status bar is simple:

procedure TSQLListForm.UpdateStatusBar;
begin
  m_panelRecNo.Caption := Format( 'Record %d of %d',
    [m_lstRecIDs.IndexOf(m_qryRead.FieldByName('KeyField1').AsString + m_qryRead.FieldByName('KeyField2').AsString)+1,
     m_nTotalRecs] );
end;

I see several ways to improve it, but you got the idea.
Julian.

Other Threads