Search...

Friday, February 17, 2012

AutoNumber column or auto number in GridView 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="Default.aspx.cs" Inherits="_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>AutoNumber column or auto number in GridView</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" onrowdatabound="GridView1_RowDataBound">
        <Columns>
        <asp:TemplateField HeaderText="Sno.">
        <ItemTemplate>
        <%# Container.DataItemIndex+1%>
        </ItemTemplate>
        </asp:TemplateField>
        </Columns>
        </asp:GridView>
    </div>
    </form>
</body>
</html>

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

/*You can achieve this by code behind also */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.UI.WebControls;

public partial class _Default : 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();
    }

    protected void GridView1_RowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            GridViewRow currentRow = e.Row;
            currentRow.Cells[0].Text = (currentRow.RowIndex + 1).ToString();
        }
    }
}

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


using System.Collections.Generic;

public class Products
{
    public Products() { }
    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 { ProductName = "Dell Laptop", ProductDetails = "Processor Intel(R) Cort(TM) i5 CPU, Ram 4GB", UnitPrice = 450000M, Quantity = 5 });
        list.Add(new Products { ProductName = "HP Laptop", ProductDetails = "Processor Intel(R) Cort(TM) i5 CPU, Ram 4GB", UnitPrice = 550000M, Quantity = 4 });
        list.Add(new Products { ProductName = "HCL Laptop", ProductDetails = "Processor Intel(R) Cort(TM) i5 CPU, Ram 4GB", UnitPrice = 350000M, Quantity = 20 });
        list.Add(new Products { ProductName = "Linovo Laptop", ProductDetails = "Processor Intel(R) Cort(TM) i5 CPU, Ram 2GB", UnitPrice = 250000M, Quantity = 30 });
        list.Add(new Products { ProductName = "Compac Laptop", ProductDetails = "Processor Intel(R) Cort(TM) i5 CPU, Ram 4GB", UnitPrice = 300000M, Quantity = 30 });
        list.Add(new Products { ProductName = "Dell Laptop", ProductDetails = "Processor Intel(R) Cort(TM) i5 CPU, Ram 4GB", UnitPrice = 450000M, Quantity = 10 });
        list.Add(new Products { ProductName = "HP Laptop", ProductDetails = "Processor Intel(R) Cort(TM) i5 CPU, Ram 4GB", UnitPrice = 550000M, Quantity = 30 });
        list.Add(new Products { ProductName = "HCL Laptop", ProductDetails = "Processor Intel(R) Cort(TM) i5 CPU, Ram 2GB", UnitPrice = 350000M, Quantity = 20 });
        list.Add(new Products { ProductName = "Linovo Laptop", ProductDetails = "Processor Intel(R) Cort(TM) i5 CPU, Ram 4GB", UnitPrice = 250000M, Quantity = 30 });
        list.Add(new Products { ProductName = "Compac Laptop", ProductDetails = "Processor Intel(R) Cort(TM) i5 CPU, Ram 4GB", UnitPrice = 300000M, Quantity = 60 });
        list.Add(new Products { ProductName = "Dell Laptop", ProductDetails = "Processor Intel(R) Cort(TM) i3 CPU, Ram 2GB", UnitPrice = 350000M, Quantity = 30 });
        list.Add(new Products { ProductName = "HP Laptop", ProductDetails = "Processor Intel(R) Cort(TM) i3 CPU, Ram 2GB", UnitPrice = 550000M, Quantity = 30 });
        list.Add(new Products { ProductName = "HCL Laptop", ProductDetails = "Processor Intel(R) Cort(TM) i3 CPU, Ram 2GB", UnitPrice = 340000M, Quantity = 20 });
        list.Add(new Products { ProductName = "Linovo Laptop", ProductDetails = "Processor Intel(R) Cort(TM) i3 CPU, Ram 2GB", UnitPrice = 350000M, Quantity = 30 });
        list.Add(new Products { ProductName = "Compac Laptop", ProductDetails = "Processor Intel(R) Cort(TM) i3 CPU, Ram 2GB", UnitPrice = 400000M, Quantity = 30 });
        list.Add(new Products { ProductName = "Dell Laptop", ProductDetails = "Processor Intel(RR) Cort(TM2) i7 CPU, Ram 8GB", UnitPrice = 650000M, Quantity = 30 });
        list.Add(new Products { ProductName = "HP Laptop", ProductDetails = "Processor Intel(RR) Cort(TM2) i7 CPU, Ram 8GB", UnitPrice = 750000M, Quantity = 1 });
        list.Add(new Products { ProductName = "HCL Laptop", ProductDetails = "Processor Intel(RR) Cort(TM2) i7 CPU, Ram 8GB", UnitPrice = 450000M, Quantity = 30 });
        list.Add(new Products { ProductName = "Linovo Laptop", ProductDetails = "Processor Intel(RR) Cort(TM2) i7 CPU, Ram 8GB", UnitPrice = 450000M, Quantity = 30 });
        list.Add(new Products { ProductName = "Compac Laptop", ProductDetails = "Processor Intel(RR) Cort(TM2) i7 CPU, Ram 8GB", UnitPrice = 370000M, Quantity = 5 });
        return list;
    }

}

No comments: