Solved

Datagrid paging in Visual Basic

Posted on 2004-04-11
3
918 Views
Last Modified: 2013-12-25
Hello,

I want to know if there is any way to achieve datagrid paging in Visual
Basic... i.e., Next Page and Previous Page to navigate the data contained in
a datagrid.

I have searched the web, and can find plenty of articals about datagrid
paging in VB.NET and ASP.NET, but for the life of me I cannot find anything
relating to datagrid paging in Visual Basic 6.

Any help would be appreciated.

Thanks very much.

Note: The datagrid contains the data that is contained in a ADODC called datSalesReport.
0
Comment
Question by:glennston
3 Comments
 
LVL 8

Accepted Solution

by:
MYLim earned 500 total points
ID: 10804271
Displaying

 large amounts of data, typically thousands of records, can present a number of obstacles. Firstly, navigation can be problematic in that the scrollbar becomes hyper-sensitive, jumping hundreds of records at the slightest touch. Secondly, overheads arise as a result of retrieving and populating forms with vast amounts of data.
The simple solution is to offer a filter on the data such as some chronological selection or an alphabetical filter such as surname grouping. However, such filtering opportunities may not exist, in which case, the only option is to display the data as a fixed number of records at a time, in a similar manner to which web searches return their results. To aid this approach, ADO offers three properties, PageSize, AbsolutePage and PageCount designed to control and manage the retrieval of large recordsets. The PageSize property set the number of records returned at a time and AbsolutePage determines which 'page' is returned, where the page is the group of records of size set by PageSize. Then the number of pages is simply calculated by ADO and accessed through the PageCount property.

Eg., if a recordset contains 500 records, and the PageSize is set to 50, the AbsolutePage can take the values 1 to 10, and the PageCount is 10. The example below, assumes a large recordset, rsExample, containing 1000's of records.

  Private Function DisplayPage(lngMaxRecs as Long, _
    lngPage as Long, rsExample as ADODB.Recordset)

      Dim intRow as Integer
      Dim lngHowManyRecs as Long
      Dim lngMaxPages as Long

      rsExample.PageSize = lngMaxRecs
      rsExample.AbsolutePage = lngPage

      lngHowManyRecs = 0
      lngMaxPages = rsExample.PageCount

      lblPage = "Page " & lngPage & " of " & lngMaxPages

      Do Until rsExample.EOF Or lngHowManyRecs >= lngMaxRecs

          ' intRow included to determine the row number
          ' for use in DataGrids
          intRow = rsExample.AbsolutePosition - _
            (rsExample.PageSize * (rsExample.AbsolutePage - 1))

          'populate form with data here

          rsExample.MoveNext
           
          lngHowManyRecs = lngHowManyRecs + 1

      Loop

  End Function
In the example function here, the page index and page size are passed as parameters to the function, controlling which set of data is displayed. This type of function could be called from a navigation control such as a series of cmdbuttons representing movefirst, moveprevious, movenext and movelast actions. The PageSize property, lngMaxRecs, could be set in a menu option or combobox etc. and the AbsolutePage property, lngPage would be set to 1, decremented, incremented or set to PageCount, respectively with respect to the 4 navigation controls above.
This method represents far greater efficiency over returning the entire recordset or holding a global recordset open and progressed throughout it. By contrast, the method above allows the recordset to be closed and the object killed after the function is executed. One point to note, controls must be in place to ensure a valid AbsolutePage parameter is passed within  the range of 1 to PageCount, ideally in the calling code where the recordset is created.

==============================================================
Try this after the recordeset.open command

rs.PageSize = 100 'the number of records in a "page"
rs.AbsolutePage = 1 ' show the first 100
set datagrid.DataSource = rs

The code should display only 100 records a time in your datagrid, to display the second 100 simply change rs.AbsolutePage to 2 and so on...
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now