ARTICLE

Custom GridView Paging in VB.NET

Posted by Rohatash Kumar Articles | ASP.NET using VB.NET July 20, 2011
Here, we will see custom GridView Paging in VB.NET.
 
Reader Level:

The GridView control provides you with an easy way to display the number of items on a page without taking much space with the help of paging. For Example you have a record set of 500 items and you want to display 10 items per page using paging.

Drag and drop a GridView control on the form.

ASPX code

<asp:GridView ID="GridView1" runat="server" AllowPaging="True"

 onrowdatabound="GridView1_RowDataBound" PageSize="5">

  <PagerTemplate>

  <asp:Menu ID="Menu1" runat="server" onmenuitemclick="Menu1_MenuItemClick"

   Orientation="Horizontal">

   </asp:Menu>

  &nbsp;<asp:LinkButton ID="btnNext" runat="server" CommandName="Next"

 oncommand="Navigate">&gt;</asp:LinkButton>

 </PagerTemplate>

 </asp:GridView>

GridView control looks like below image.

g1.gif

Figure1

Now double click on the form and add the following code.

Public Class WebForm1

    Inherits System.Web.UI.Page

 

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        If Not Page.IsPostBack Then

 

            binding()

        End If

    End Sub

    Private Sub binding()

        Dim tblData As New DataTable()

        Dim col1 As New DataColumn("Data1")

        tblData.Columns.Add(col1)

 

        For i As Integer = 1 To 1500

            Dim dr As DataRow = tblData.NewRow()

            dr("Data1") = i

            tblData.Rows.Add(dr)

        Next

        GridView1.DataSource = tblData

        GridView1.DataBind()

    End Sub

    Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)

        If e.Row.RowType = DataControlRowType.Pager Then

            Dim pgmenu As Menu = DirectCast(e.Row.FindControl("Menu1"), Menu)

            Dim startno As Integer, endno As Integer

            Dim selectedpgeno As Integer = -1

            If ViewState("selectedpgno") Is Nothing Then

                startno = 1

                endno = startno + 10

            Else

                selectedpgeno = Convert.ToInt32(ViewState("selectedpgno"))

                If (selectedpgeno - 10) <= 0 Then

                    startno = 1

                    endno = selectedpgeno + 9

                Else

                    startno = selectedpgeno - 10

                    endno = selectedpgeno + 9

                End If

            End If

 

            For i As Integer = startno To endno

                pgmenu.Items.Add(New MenuItem(i.ToString(), (i - 1).ToString()))

            Next

        End If

    End Sub

    Protected Sub Menu1_MenuItemClick(ByVal sender As Object, ByVal e As MenuEventArgs)

        ViewState.Add("selectedpgno", e.Item.Text)

        GridView1.PageIndex = Convert.ToInt32(e.Item.Value)

        binding()

    End Sub

    Protected Sub Navigate(ByVal sender As Object, ByVal e As CommandEventArgs)

        If e.CommandName = "Next" Then

            ViewState.Add("selectedpgno", GridView1.PageIndex + 1)

            GridView1.PageIndex = Convert.ToInt32(GridView1.PageIndex + 1)

            binding()

        End If

    End Sub

 

End Class

Now run the application and test it.

Display Paging in gridview

Figure2

Now click on the paging number 2 to show next five record.

Display next record

Figure3

Login to add your contents and source code to this article
share this article :
post comment
 
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor