Search...

Wednesday, February 15, 2012

GridView paging example in Asp.Net




*********************************************************************************

/*Add Stylesheet in your application names StyleSheet.css*/

body {}
.gridView{width:100%;background:#ffffff;border:1px solid #476db6; border-collapse:separate;}
.gridView th{border:0px;background:#a7b7fe;color:#000000;text-align:center;font-size:18px;}
.gridView td{border:0px;padding-left:10px;}
.gridView tr:hover{cursor:pointer; background:#ffff00;}
.gridView tr:last-child:hover{ background:#000;background:#a7b7fe;}
.altRow{background:#d1daff;}
.gridPager{background:#a7b7fe;color:#000000;}
.gridPager table{margin:0px auto;}
.gridPager table td{width:20px;}
.gridPager table span{cursor:pointer;font-weight:bold;color:#1a1ac9;}
.gridPager table a{cursor:pointer;text-decoration:none;color:#000000;}
.gridPager table a:hover{text-decoration:underline;}


*********************************************************************************

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

<!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>GridView paging example in Asp.Net</title>
    <link href="StyleSheet.css" rel="stylesheet" type="text/css" />
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <%="You are viewing page "+(GridView1.PageIndex+1)+" of "+GridView1.PageCount+" ,row count "+GridView1.Rows.Count %>
        <asp:GridView ID="GridView1"
        runat="server"
        CssClass="gridView"
        AlternatingRowStyle-CssClass="altRow"
        PagerStyle-CssClass="gridPager"
        AllowPaging="true"
        PageSize="5" onpageindexchanging="GridView1_PageIndexChanging">
        </asp:GridView>
    </div>
    </form>
</body>
</html>

********************************************************************************

using System;

public partial class _Default1 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        GridView1.CellSpacing = -1;
        if (!IsPostBack)
        {
            BindGrid();
        }
    }

    private void BindGrid()
    { 
        Products products=new Products();
        GridView1.DataSource = products.getProducts();
        GridView1.DataBind();
    }
    protected void GridView1_PageIndexChanging(object sender, System.Web.UI.WebControls.GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        BindGrid();
    }
}

********************************************************************************


using System.Collections.Generic;

public class Products
{
    public Products() { }
    public int ProductId { get; set; }
    public string ProductName { get; set; }
    public string ProductDetails { get; set; }
    public decimal UnitPrice { get; set; }
    public int Quantity { get; set; }

    public List<Products> getProducts()
    {
        List<Products> list = new List<Products>();
        list.Add(new Products { ProductId = 1, ProductName = "Dell Laptop", ProductDetails = "Processor Intel(R) Cort(TM) i5 CPU, Ram 4GB", UnitPrice = 450000M, Quantity = 5 });
        list.Add(new Products { ProductId = 2, ProductName = "HP Laptop", ProductDetails = "Processor Intel(R) Cort(TM) i5 CPU, Ram 4GB", UnitPrice = 550000M, Quantity = 4 });
        list.Add(new Products { ProductId = 3, ProductName = "HCL Laptop", ProductDetails = "Processor Intel(R) Cort(TM) i5 CPU, Ram 4GB", UnitPrice = 350000M, Quantity = 20 });
        list.Add(new Products { ProductId = 4, ProductName = "Linovo Laptop", ProductDetails = "Processor Intel(R) Cort(TM) i5 CPU, Ram 2GB", UnitPrice = 250000M, Quantity = 30 });
        list.Add(new Products { ProductId = 5, ProductName = "Compac Laptop", ProductDetails = "Processor Intel(R) Cort(TM) i5 CPU, Ram 4GB", UnitPrice = 300000M, Quantity = 30 });
        list.Add(new Products { ProductId = 6, ProductName = "Dell Laptop", ProductDetails = "Processor Intel(R) Cort(TM) i5 CPU, Ram 4GB", UnitPrice = 450000M, Quantity = 10 });
        list.Add(new Products { ProductId = 7, ProductName = "HP Laptop", ProductDetails = "Processor Intel(R) Cort(TM) i5 CPU, Ram 4GB", UnitPrice = 550000M, Quantity = 30 });
        list.Add(new Products { ProductId = 8, ProductName = "HCL Laptop", ProductDetails = "Processor Intel(R) Cort(TM) i5 CPU, Ram 2GB", UnitPrice = 350000M, Quantity = 20 });
        list.Add(new Products { ProductId = 9, ProductName = "Linovo Laptop", ProductDetails = "Processor Intel(R) Cort(TM) i5 CPU, Ram 4GB", UnitPrice = 250000M, Quantity = 30 });
        list.Add(new Products { ProductId = 10, ProductName = "Compac Laptop", ProductDetails = "Processor Intel(R) Cort(TM) i5 CPU, Ram 4GB", UnitPrice = 300000M, Quantity = 60 });
        list.Add(new Products { ProductId = 11, ProductName = "Dell Laptop", ProductDetails = "Processor Intel(R) Cort(TM) i3 CPU, Ram 2GB", UnitPrice = 350000M, Quantity = 30 });
        list.Add(new Products { ProductId = 12, ProductName = "HP Laptop", ProductDetails = "Processor Intel(R) Cort(TM) i3 CPU, Ram 2GB", UnitPrice = 550000M, Quantity = 30 });
        list.Add(new Products { ProductId = 13, ProductName = "HCL Laptop", ProductDetails = "Processor Intel(R) Cort(TM) i3 CPU, Ram 2GB", UnitPrice = 340000M, Quantity = 20 });
        list.Add(new Products { ProductId = 14, ProductName = "Linovo Laptop", ProductDetails = "Processor Intel(R) Cort(TM) i3 CPU, Ram 2GB", UnitPrice = 350000M, Quantity = 30 });
        list.Add(new Products { ProductId = 15, ProductName = "Compac Laptop", ProductDetails = "Processor Intel(R) Cort(TM) i3 CPU, Ram 2GB", UnitPrice = 400000M, Quantity = 30 });
        list.Add(new Products { ProductId = 16, ProductName = "Dell Laptop", ProductDetails = "Processor Intel(RR) Cort(TM2) i7 CPU, Ram 8GB", UnitPrice = 650000M, Quantity = 30 });
        list.Add(new Products { ProductId = 17, ProductName = "HP Laptop", ProductDetails = "Processor Intel(RR) Cort(TM2) i7 CPU, Ram 8GB", UnitPrice = 750000M, Quantity = 1 });
        list.Add(new Products { ProductId = 18, ProductName = "HCL Laptop", ProductDetails = "Processor Intel(RR) Cort(TM2) i7 CPU, Ram 8GB", UnitPrice = 450000M, Quantity = 30 });
        list.Add(new Products { ProductId = 19, ProductName = "Linovo Laptop", ProductDetails = "Processor Intel(RR) Cort(TM2) i7 CPU, Ram 8GB", UnitPrice = 450000M, Quantity = 30 });
        list.Add(new Products { ProductId = 20, ProductName = "Compac Laptop", ProductDetails = "Processor Intel(RR) Cort(TM2) i7 CPU, Ram 8GB", UnitPrice = 370000M, Quantity = 5 });
        return list;
    }

}

No comments: