Tuesday, July 8, 2014

GridView Paging



<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Pagging.aspx.cs" Inherits="Pagging" %>

<!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></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:GridView ID="grd" runat="server" AllowPaging="true" PageSize="3"
            onpageindexchanging="grd_PageIndexChanging"
            onpageindexchanged="grd_PageIndexChanged">
   
    </asp:GridView>
    </div>
    </form>
</body>
</html>

----------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

public partial class Pagging : System.Web.UI.Page
{
    DataClassesDataContext _objdbml = new DataClassesDataContext();
    Country _objcountry = new Country();
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {

            var da = from p in _objdbml.Countries
                     select p;
            SqlCommand _objcmd = (SqlCommand)_objdbml.GetCommand(da);
            DataSet ds = new DataSet();
            SqlDataAdapter _dap = new SqlDataAdapter(_objcmd);
            _dap.Fill(ds);


            grd.DataSource = ds;
            grd.DataBind();
        }
        catch (Exception ex)
        {
            Response.Redirect("Default2.aspx");
        }
        finally
        {
        }
    }
    protected void grd_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        try
        {
            grd.PageIndex = Convert.ToInt32(e.NewPageIndex.ToString());
            grd.DataBind();
        }
        catch (Exception ex)
        {
            Response.Redirect("Default2.aspx");
        }

    }
    protected void grd_PageIndexChanged(object sender, EventArgs e)
    {

    }
}