.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Code Snippets >> ASP.NET Controls >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

Paging in a datagrid using dropdownlist

Posted By:satyapriyanayak       Posted Date: July 02, 2013    Points: 40    Category: ASP.NET Controls    URL: http://www.dotnetspark.com  

How to do paging in a datagrid using a dropdownlist
 

Here we will know how to do paging in a datagrid using a dropdownlist. Here also we have provided next 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.


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"/>     
     
     
      <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 Object, ByVal e As System.EventArgs) Handles Me.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 Object, ByVal 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 Object, ByVal e As EventArgs) Handles showpage.Click
        dg1.CurrentPageIndex = 0
        Bindgrid()
    End Sub
End Class





     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend