Home arrow ASP.NET arrow Page 4 - Changing the Page Size Interactively in a DataGrid Web Control
ASP.NET

Changing the Page Size Interactively in a DataGrid Web Control


Csaba explains the problems that occur when to wish to display many records using the DataGrid Server and the user decides to change the pagesize.

Author Info:
By: Csaba Hatvany
Rating: 4 stars4 stars4 stars4 stars4 stars / 27
March 31, 2003
TABLE OF CONTENTS:
  1. · Changing the Page Size Interactively in a DataGrid Web Control
  2. · The Problem
  3. · The Solution
  4. · The Implementation
  5. · Conclusion

print this article
SEARCH DEVARTICLES

TOOLS YOU CAN USE

advertisement
Changing the Page Size Interactively in a DataGrid Web Control - The Implementation
(Page 4 of 5 )

For convenience we can wrap the work done in a function

Function NewCurrentPageIndex( _
    dg As DataGrid, _
....ntRecordCount As Integer, _
....intNewPageSize As Integer _
) As Integer

The SelectedIndexChanged event of our page size setting DropDownList control has the procedure PageSizeChanged as its handler. This procedure looks like that:

Sub PageSizeChanged(sender As Object, e As EventArgs)
    MyDataGrid.CurrentPageIndex = _
        NewCurrentPageIndex(MyDataGrid, _
                            Session("recCount"), _
                            ddlPageSize.SelectedItem.Text)
    BindGrid
End Sub

Here is the source of the NewCurrentPageIndex function:

Function NewCurrentPageIndex( _
    dg As DataGrid, _
    intRecordCount As Integer, _
    intNewPageSize As Integer _
) As Integer
    ' old page size
    Dim intOldPageSize As Integer
    ' top record index on current page
    Dim intFirstRecordIndex As Integer
    ' new page count
    Dim intNewPageCount As Integer
    Dim intNewCurrentPageIndex as Integer
    ' is given from the DataGrid PageSize property
    intOldPageSize = dg.PageSize
    ' identifies the reference record
    intFirstRecordIndex = dg.CurrentPageIndex * intOldPageSize + 1
    ' set the new page size for the Data grig
    dg.PageSize = intNewPageSize
    ' The actual page count of the DataGrid control
    ' is the "old" page count.
    ' The new page count of the DataGrid control will be set
    ' automatically after we bind the Datagrid to the data source
    ' with new page size set.
    ' We need the new page count already now
    ' to find out the new current page index,
    ' so we must calculate it.
    intNewPageCount = _
    CType(Math.Ceiling(intRecordCount / intNewPageSize),Integer)
    ' get the new current page index
    Dim i as Integer
    For i = 1 to intNewPageCount
        If intFirstRecordIndex >= (i-1)*intNewPageSize +1 And _
            intFirstRecordIndex <= i*intNewPageSize Then
            intNewCurrentPageIndex=i-1
            Exit For
        End If
    Next i
    NewCurrentPageIndex = intNewCurrentPageIndex
End Function

Here (http://www.hatvany-online.net/aspdotnet/articles/changingpagesize.aspx) you can try a live demo. You can find the source code of the sample in the support file.


blog comments powered by Disqus
ASP.NET ARTICLES

- How Caching Means More Ca-ching, Part 2
- How Caching Means More Ca-ching, Part 1
- Reading a Delimited File Using ASP.Net and V...
- What is .Net and Where is ASP.NET?
- An Object Driven Interface with .Net
- Create Your Own Guestbook In ASP.NET
- HTTP File Download Without User Interaction ...
- Dynamically Using Methods in ASP.NET
- Changing the Page Size Interactively in a Da...
- XML Serialization in ASP.NET
- Using Objects in ASP.NET: Part 1/2
- IE Web Controls in VB.NET
- Class Frameworks in VB .NET
- Cryptographic Objects in C#: Part 1
- Sample Chapter: Pure ASP.Net

Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 



© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 4 - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials