Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 588
  • Last Modified:

how to use next and prev page in dbgrid?

I want to make a touch-screen program.
I want to use a next and prev button to control dbgrid,
when I click next button,dbgrid will display next 10 record,
when I click prev button,dbgrid will display prev 10 record.
and I don't want the vertical scroll bar displayed in dbgrid.
Can you tell me how to do that?
  • 8
  • 4
1 Solution
nhdcb2Author Commented:
Edited text of question
To move 10 records forward, use :


To move 10 records backward, use :


Hiding vertical scroll bar :

 Try this API : ShowScrollBar(DBGrid1.Handle, sb_Vert, False)

or try this :

In order to remove the vertical scrollbar from a TDBGrid component,
you must override its Paint method.  Inside the Paint method you
must call the SetScrollRange API procedure to set the min and max
scroll values to zero (this disables the scrollbar), and then call
the inherited Paint.  The code below is a unit containing a new
component called TNoVertScrollDBGrid that does this.  You can copy
the code into a file called NEWGRID.PAS, and add it to the component
library as a custom component.

unit Newgrid;


  WinTypes, WinProcs, Classes, DBGrids;

  TNoVertScrollDBGrid = class(TDBGrid)
    procedure Paint; override;

procedure Register;


procedure TNoVertScrollDBGrid.Paint;
  SetScrollRange(Self.Handle, SB_VERT, 0, 0, False);
  inherited Paint;

procedure Register;
  RegisterComponents('Data Controls', [TNoVertScrollDBGrid]);


        TI (Borland support pages)

Regards, Zif.
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

heck maybe this API might work too :

In a component descending from TDBGrid do something like :
                                   SetScrollRange(Handle, SB_VERT, 0, 0, False);
                             to hide the vertical scrollbar ;
                                   SetScrollRange(Handle, SB_VERT, 0, 4, False);
                             to show it.

Regards, Zif.
nhdcb2Author Commented:
Thank you for your help!
I know how to disappear the vertical scroll bar,
but I don't know how to get the last record that is displayed
in the dbgrid.
so I can't make my next and prev button,
I hope you can help me again!
Thanks a lot!
? nhdcb2, can you explain this problem more?

In my first comment I explained you how to move to the next 10 or  previous 10 records. So this should work (let you browse to the next/previous records). Use Datamodule1.Table1.First and Datamodule1.Table1.Last for browsing directly to the first and last record of the table. The MoveBy(+10) instruction also stops at the last record if there are not more than 10 records to the end of the table.

So, please explain your problem more, because I don't understand your question.

oops, wasn't meant as an answer, so if it doesn't help you with your problem, just reject it and explain the problem. Zif.
nhdcb2Author Commented:
I am sorry for my little explaination!
Because My touch-screen is 17",it must display two dbgrids
in one screen,and that two dbgrids use the same dataset.
When I click the next or prev,The first dbgrid move 10,
and the second dbgrid move next 10.
If I use moveby, that two dbgrids display the same record.
That is not my want.
So I must know the last record that is displayed in the first dbgrid to transfer to the second dbgrid.
Can you tell me how can I do that?
mmmm, not so easy to get it I guess. You find a way to detect how many rows are in the dbgrid.
Let brainstorm a little bit :

 So, if we know how many rows or on the grid.
 And we know what the first record is in the grid
 then we also know the last record.

 We could let the user start with Ttable1.First and if this record moves, we always know which one is the first. If the dbgrid is fixed, we know the amount of rows. (Maybe if we know dimensions of grid and dimension of one row, we know the amount of rows) -> We know last record displayed!

nhdcb2Author Commented:
    Thanks for your help.
    But I think your answered doesn't work. I know the first grid'first and last record,but how can I control my
second grid.Now suppose let every dbgrid display 10 records.
but how can I make the second dbgrid move and does't affect
the first dbgrid?

Hi nhdcb2,

That's in fact really easy. Just take 2 tables, and assign them to the same database table. then take two datasources and two dbgrids. Then the two dbgrids WON'T be synchronised. Browsing through them will be independent from the other one. If you want them to be synchronised then just use table2.gotocurrent(table1).

If I understand you right you want the first grid to display rows 1-10 (for example) and the second grid to display rows 11-20.  When you hit the next button, you want the first grid to display rows 11-20 and the second grid to show rows 21-30.  

(Or it might make more sense to press the next button and have grid 1 show rows 21-30 and grid 2 show 31-40.  It doesn't really matter what you're increment is.)

If this is correct, use 2 tables and datasources (like ZiF said) but when you first start the app (or make the tables active, or whatever) do a table2.moveby(10) to get the offset initialized.

Then when the next button is pressed, do a
Table1.Moveby( Table2.Moveby(10) ).  

Moveby returns an integer that represents how many rows were actually traversed.  If you're looking at the fourth row from the end in the second grid and you press the next button, grid 2 will only move 4 rows.  With the above call, grid 1 will also move only 4 rows.

The call for the prev button would be
Table2.moveby( Table1.moveby(-10) )

This will keep your grids moving together but always offset by 10.

Hope this helps!

Featured Post

[Webinar On Demand] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

  • 8
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now