Lazarus

Programming => Databases => Topic started by: mpknap on January 31, 2020, 09:50:41 am

Title: Dbgrid row count
Post by: mpknap on January 31, 2020, 09:50:41 am
I can't find on the internet how to count how many rows have dbgrid1 (or sql records loaded into dbgrid)?
later there will be information in label1.caption
Title: Re: Dbgrid row count
Post by: lucamar on January 31, 2020, 11:07:16 am
Try with DBGrid.DataSource.DataSet.RecordCount, but, as the docs say, remember that:

Quote
RecordCount is the number of records in the dataset. This number is not necessarily equal to the number of records returned by a query. For optimization purposes, a TDataset descendent may choose not to fetch all records from the database when the dataset is opened. If this is the case, then the RecordCount will only reflect the number of records that have actually been fetched at the current time, and therefor the value will change as more records are fetched from the database.

Only when Last has been called (and the dataset has been forced to fetch all records returned by the database), will the value of RecordCount be equal to the number of records returned by the query.
Title: Re: Dbgrid row count
Post by: mpknap on February 01, 2020, 09:33:47 am
yes
label1.Caption:=DataSource1.DataSet.RecordCount.ToString;

displays the number of records on the screen in dbgrid.
Just using DBNavigator click manualy on "Last" button
and then in label1 it will show how many are all.

And how to programatically press "Last" button in DBNavigator?

Title: Re: Dbgrid row count
Post by: HeavyUser on February 01, 2020, 10:48:25 am
yes
label1.Caption:=DataSource1.DataSet.RecordCount.ToString;

displays the number of records on the screen in dbgrid.
Just using DBNavigator click manualy on "Last" button
and then in label1 it will show how many are all.

And how to programatically press "Last" button in DBNavigator?
Code: Pascal  [Select][+][-]
  1. DataSource1.DataSet.Last;
  2.  
Title: Re: Dbgrid row count
Post by: mpknap on February 01, 2020, 06:03:25 pm
Code: Pascal  [Select][+][-]
  1.   datasource1.DataSet.Last;
  2.     datasource1.DataSet.First;
  3.     label11.Caption := 'Rec ' + DataSource1.DataSet.RecNo.ToString + '  of ' +
  4.       DataSource1.DataSet.RecordCount.ToString;
  5.      

thx :)
TinyPortal © 2005-2018