Custom GridView Paging in VB.NET

Here, we will see custom GridView Paging in VB.NET
  • 1292

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 ObjectByVal e As System.EventArgsHandlesMe.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 ObjectByVal e AsGridViewRowEventArgs)

        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 ObjectByVal 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 ObjectByVal 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.

g2.gif 

Figure2

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

g3.gif
 

Figure3

Categories

More Articles

© 2013 dotNetheaven. All rights reserved.