Recent

Author Topic: How to get Current row number of a DBGrid within a filtered Query?  (Read 9869 times)

JSN

  • Jr. Member
  • **
  • Posts: 53
I have found other solutions to obtaining the current selected row number of a DBGrid. But those seem to return the Record Number of the dataset.

If a Query is filtered, how do I obtain the current selected row number of the DBGrid?

Hope this makes sense. :)

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: How to get Current row number of a DBGrid within a filtered Query?
« Reply #1 on: June 01, 2017, 02:58:24 pm »
you ask the dataset eg
Code: [Select]
  showmessage(inttostr(DBGrid1.DataSource.DataSet.RecNo));
oops to hasty didn't read the question through. sorry but this one I'll have to answer it after work.
« Last Edit: June 01, 2017, 02:59:55 pm by taazz »
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

balazsszekely

  • Guest
Re: How to get Current row number of a DBGrid within a filtered Query?
« Reply #2 on: June 01, 2017, 03:58:13 pm »
@JSN
Ideally you should write a class helper or modify DBGrids.pas and publish a new property. Anyway I found a way, all you have to do is replace "Name" with the field you wish to see(I assume multiselect is off, and your form name is Form1):
Code: Pascal  [Select][+][-]
  1. procedure TfMain.DBGridDrawColumnCell(Sender: TObject; const Rect: TRect;
  2.   DataCol: Integer; Column: TColumn; State: TGridDrawState);
  3. var
  4.   Row: Integer;
  5. begin
  6.   if gdSelected in State then
  7.   begin
  8.     Row := Trunc(Rect.Bottom/(Sender as TDBGrid).DefaultRowHeight) - 1;
  9.     Form1.Caption := DBGrid.DataSource.DataSet.FieldByName('NAME').AsString + '  ' + IntToStr(Row);
  10.   end;
  11. end;

 

TinyPortal © 2005-2018