Paging in a DataGrid Using DropDownList

Here we have also provided next and previous links to navigate the records. We can know how many records are present in the database using a label control. The controls used in this application are a DataGrid, four link buttons for (first, next, previous and last), two label controls and a DropDownList.

Table Creation

datagrid1.gif

Default.aspx code

<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="Default.aspx.vb"Inherits="Dropdown_Paging_in_Datagrid._Default" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<
html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<
body>
    <form id="form1" runat="server">
    <div>
    <asp:DataGrid id="dg1" runat="server"  AutoGenerateColumns="true"
     AllowPaging="True" AllowCustomPaging="False" 
     PagerStyle-Visible = "False" HeaderStyle-BackColor="Red" 
     HeaderStyle-ForeColor="White" BackColor="#FFCC66" > 
     
     
<PagerStyle Visible="False"></PagerStyle> 
        <AlternatingItemStyle BackColor="#FFFFCC" /> 
<
HeaderStyle BackColor="Red" ForeColor="White"></HeaderStyle>
        </asp:Datagrid>     
     <asp:linkbutton id="Firstbutton" Text="<< 1st Page" CommandArgument="0" 
            runat="server" onClick="NavigateButtonClick" ForeColor="#333300" ToolTip="First"/> 
     <asp:linkbutton id="Prevbutton" Text= "" CommandArgument="prev" runat="server" 
            onClick="NavigateButtonClick" ForeColor="#333300" ToolTip="Previous"/>  &nbsp;&nbsp;          
      <asp:linkbutton id="Nextbutton" Text= "" CommandArgument="next" runat="server" 
            onClick="NavigateButtonClick" ForeColor="#333300" ToolTip="Next"/> 
        <asp:linkbutton id="Lastbutton" Text="Last Page >>" CommandArgument="last" 
            runat="server" onClick="NavigateButtonClick" ForeColor="#333300" ToolTip="Last"/> 
      <br /><br />      
     <asp:Label id="PageCount" runat="server" /><br /> 
     <asp:label id="TotalRecords" runat="server" /> <br /> <br /><br /><br />
      
      Change Pagesize <asp:DropDownList id="d1" runat="server" ForeColor="Maroon"> 
      <asp:ListItem>1</asp:ListItem>  
      <asp:ListItem>2</asp:ListItem>  
      <asp:ListItem>3</asp:ListItem>  
      <asp:ListItem  Selected="True">4</asp:ListItem>  
      <asp:ListItem>5</asp:ListItem>  
      <asp:ListItem>6</asp:ListItem> 
      <asp:ListItem>7</asp:ListItem>  
      <asp:ListItem>8</asp:ListItem>  
      <asp:ListItem>9</asp:ListItem>  
      </asp:DropDownList>  
      <asp:button ID="showpage" text="Choose Pagesize" runat="server" /> 
    </div>
    </form>
</body>
</
html>

Default.aspx.vb

Imports System.Data
Imports System.Data.SqlClient
Partial Public Class _Default
    Inherits System.Web.UI.Page
    Dim strConnString As String = System.Configuration.ConfigurationManager.ConnectionStrings.Item("ConnectionString").ToString()
    Dim con As New SqlConnection(strConnString)
    Dim str As String
    Dim com As SqlCommand
    Dim sqlda As SqlDataAdapter
    Dim ds As DataSet
    Dim r1 As Integer
    Dim r2 As Integer
    Protected Sub Page_Load(ByVal sender As ObjectByVal e As System.EventArgs) HandlesMe.Load
        If Not IsPostBack Then
            Bindgrid()
        End If
    End Sub
    Sub Bindgrid()
        con.Open()
        str = ("SELECT * from employee")        com = New SqlCommand(str, con)
        sqlda = New SqlDataAdapter(com)
        ds = New DataSet
        sqlda.Fill(ds, "employee")
        r2 = ds.Tables("employee").Rows.Count.ToString()
        dg1.PageSize = Convert.ToInt32(d1.SelectedItem.Value)
        If Not Page.IsPostBack Then
            dg1.CurrentPageIndex = 0
        End If
        r1 = r2
        TotalRecords.Text = "Total" & " " & " " & " " & "<b><font color=red>" & r2 & "</font> records found"
        Try
            dg1.DataSource = ds
            dg1.DataBind()
        Catch e As Exception
            dg1.CurrentPageIndex = 0
        End Try

        If dg1.CurrentPageIndex <> 0 Then 
            Call Prev_Buttons()
            Firstbutton.Visible = True
            Prevbutton.Visible = True
        Else
            Firstbutton.Visible = False
            Prevbutton.Visible = False
        End If
 
        If dg1.CurrentPageIndex <> (dg1.PageCount - 1) Then
            Call Next_Buttons()
            Nextbutton.Visible = True
            Lastbutton.Visible = True
        Else
            Nextbutton.Visible = False
            Lastbutton.Visible = False
        End If
        PageCount.Text = "Page " & "<b><font color=red>" & dg1.CurrentPageIndex + 1 & "</font>""of " & "<b><font color=red>" & dg1.PageCount &
"</font>"
        con.Close()

    End Sub
    Sub NavigateButtonClick(ByVal sender As ObjectByVal e As EventArgs) 
        Dim flag As String = sender.CommandArgument
        Select Case flag
            Case "next"
                If (dg1.CurrentPageIndex < (dg1.PageCount - 1)) Then
                    dg1.CurrentPageIndex += 1
                End If
            Case "prev"
                If (dg1.CurrentPageIndex > 0) Then

                    dg1.CurrentPageIndex -= 1
                End If
            Case "last"
                dg1.CurrentPageIndex = (dg1.PageCount - 1)
            Case Else
                dg1.CurrentPageIndex = Convert.ToInt32(flag)
        End Select
        Bindgrid()
    End Sub
 
    Sub Prev_Buttons()
        Dim Prevflag As String
 
        If dg1.CurrentPageIndex + 1 <> 1 And r1 <> -1 Then
            Prevflag = dg1.PageSize
            Prevbutton.Text = ("< Prev " & Prevflag)
            If dg1.CurrentPageIndex + 1 = dg1.PageCount Then
                Firstbutton.Text = ("<< 1st Page")
            End If
        End If
    End Sub
 
    Sub Next_Buttons()
        Dim Nextflag As String
 
        If dg1.CurrentPageIndex + 1 < dg1.PageCount Then
            Nextflag = dg1.PageSize
            Nextbutton.Text = ("Next " & Nextflag & " >")
        End If
        If dg1.CurrentPageIndex + 1 = dg1.PageCount - 1 Then
            Dim Endflag As Integer = r1 - (dg1.PageSize * (dg1.CurrentPageIndex + 1))
            Nextbutton.Text = ("Next " & Endflag & " >")
        End If
    End Sub
 

    Protected Sub showpage_Click(ByVal sender As ObjectByVal e As EventArgs) Handlesshowpage.Click
        dg1.CurrentPageIndex = 0
        Bindgrid()
    End Sub
End Class

Output

datagrid2.gif

When we click the next4 link:

datagrid3.gif

When we change the page size to 6:

datagrid4.gif

When we click the lastpage link:

datagrid5.gif

Up Next
    Ebook Download
    View all
    Learn
    View all