Monday, July 20, 2015

Asp.net: Enable pagination (Paging) in Gridview control

In this article I will explain How to enable the pagination (paging) in Gridview control in Asp.net.

Description:

Gridview control has the in-built paging functionality. To enable it we have to set the AllowPaging property to true to enable pagination and handle the OnPageIndexChanging event. When we set the AllowPaging property to true by default Numeric paging mode will work. We can customize the paging mode by setting the property PagerSettings-Mode of Gridview. We have four options NextPrevious, NextPreviousFirstLast, Numeric and NumericFirstLast. Don’t forget to assign the Pagesize.

Implementation:

HTML Markup of Webform:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" AllowPaging="true" PageSize="5" OnPageIndexChanging="GridView1_PageIndexChanging">
            <Columns>
                <asp:BoundField DataField="SudentName" HeaderText="Name" />
                 <asp:BoundField DataField="Fee" HeaderText="Fee" />
                 <asp:BoundField DataField="StudentClass" HeaderText="Class" />
                <asp:BoundField DataField="StudentRollNo" HeaderText="Roll Number" />
            </Columns>
        </asp:GridView>

Example of Gridview Markup with PagerSettings-Mode:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" AllowPaging="true" PagerSettings-Mode="NextPrevious" PagerSettings-PreviousPageText="Previous" PagerSettings-NextPageText="Next" PagerSettings-FirstPageText="First" PagerSettings-LastPageText="Last" PageSize="5" OnPageIndexChanging="GridView1_PageIndexChanging">
            <Columns>
                <asp:BoundField DataField="SudentName" HeaderText="Name" />
                 <asp:BoundField DataField="Fee" HeaderText="Fee" />
                 <asp:BoundField DataField="StudentClass" HeaderText="Class" />
                <asp:BoundField DataField="StudentRollNo" HeaderText="Roll Number" />
            </Columns>

        </asp:GridView>

Add the namespace
C#:
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
VB:
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration

Write the code to bind the Gridview from database
C#:
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["connection"].ToString());
    protected void Page_Load(object sender, EventArgs e)
    {
        if(!IsPostBack)
        {
            BindGridview();
        }
    }
    public void BindGridview()
    {
        try
        {
        SqlDataAdapter adp = new SqlDataAdapter("Select * from dbo.Student_Detail", con);
        DataTable dt = new DataTable();
        adp.Fill(dt);
        GridView1.DataSource = dt;
        GridView1.DataBind();
         }
        catch(Exception ex)
        {

        }
    }

VB:
Private con As New SqlConnection(ConfigurationManager.ConnectionStrings("connection").ToString())
    Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
        If Not IsPostBack Then
            BindGridview()
        End If
    End Sub
    Public Sub BindGridview()
        Try
            Dim adp As New SqlDataAdapter("Select * from dbo.Student_Detail", con)
            Dim dt As New DataTable()
            adp.Fill(dt)
            GridView1.DataSource = dt
            GridView1.DataBind()
        Catch ex As Exception
        End Try
    End Sub

After that write the below given code on PageIndexChanging event of Gridview.
C#:
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        BindGridview();
    }

VB:
Protected Sub GridView1_PageIndexChanging(sender As Object, e As GridViewPageEventArgs) Handles GridView1.PageIndexChanging
        GridView1.PageIndex = e.NewPageIndex
        BindGridview()
    End Sub

Now build, run the project. 

Result:
Asp.net: Enable pagination (Paging) in Gridview control
  In this article we have learn How to enable the pagination (paging) in Gridview control in Asp.net using (C#, VB). I hope you enjoyed this article. 

No comments:

Post a Comment