Search...

Wednesday, February 15, 2012

Getting value from a cell of gridview on RowDataBound event 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="Default3.aspx.cs" Inherits="_Default3" %>

<!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>Getting value from a cell of gridview on RowDataBound event 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"
        AutoGenerateColumns="false"
        CssClass="gridView"
        AlternatingRowStyle-CssClass="altRow"
        PagerStyle-CssClass="gridPager"
        AllowPaging="true"
        PageSize="10"
        OnRowDataBound="GridView1_RowDataBound"
        onpageindexchanging="GridView1_PageIndexChanging">
        <Columns>
        <asp:BoundField DataField="ProductId" HeaderText="Product Id" />
        <asp:BoundField DataField="PId" HeaderText="Product Name" />
        <asp:BoundField DataField="ProductDetails" HeaderText="Product Details" />
        <asp:BoundField DataField="UnitPrice" HeaderText="Price" />
        </Columns>
        </asp:GridView>
    </div>
    </form>
</body>
</html>

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


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

public partial class _Default3 : 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)
        {
            Products products = new Products();
            GridViewRow currentRow = e.Row;
            var data = from q in products.getPtoductType() where q.PId == ((Products)(currentRow.DataItem)).PId select q.ProductName;
            foreach (String pName in data)
            {
                e.Row.Cells[1].Text = pName;
            }
            
        }
    }
}

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


using System.Collections.Generic;

public class Products
{
    public Products() { }
    public int PId { get; set; }
    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, PId = 1, ProductDetails = "Processor Intel(R) Cort(TM) i5 CPU, Ram 4GB", UnitPrice = 450000M, Quantity = 5 });
        list.Add(new Products { ProductId = 2, PId = 2, ProductDetails = "Processor Intel(R) Cort(TM) i5 CPU, Ram 4GB", UnitPrice = 550000M, Quantity = 4 });
        list.Add(new Products { ProductId = 3, PId = 3, ProductDetails = "Processor Intel(R) Cort(TM) i5 CPU, Ram 4GB", UnitPrice = 350000M, Quantity = 20 });
        list.Add(new Products { ProductId = 4, PId = 4, ProductDetails = "Processor Intel(R) Cort(TM) i5 CPU, Ram 2GB", UnitPrice = 250000M, Quantity = 30 });
        list.Add(new Products { ProductId = 5, PId = 5, ProductDetails = "Processor Intel(R) Cort(TM) i5 CPU, Ram 4GB", UnitPrice = 300000M, Quantity = 30 });
        list.Add(new Products { ProductId = 6, PId = 1, ProductDetails = "Processor Intel(R) Cort(TM) i5 CPU, Ram 4GB", UnitPrice = 450000M, Quantity = 10 });
        list.Add(new Products { ProductId = 7, PId = 2, ProductDetails = "Processor Intel(R) Cort(TM) i5 CPU, Ram 4GB", UnitPrice = 550000M, Quantity = 30 });
        list.Add(new Products { ProductId = 8, PId = 3, ProductDetails = "Processor Intel(R) Cort(TM) i5 CPU, Ram 2GB", UnitPrice = 350000M, Quantity = 20 });
        list.Add(new Products { ProductId = 9, PId = 4, ProductDetails = "Processor Intel(R) Cort(TM) i5 CPU, Ram 4GB", UnitPrice = 250000M, Quantity = 30 });
        list.Add(new Products { ProductId = 10, PId = 5, ProductDetails = "Processor Intel(R) Cort(TM) i5 CPU, Ram 4GB", UnitPrice = 300000M, Quantity = 60 });
        list.Add(new Products { ProductId = 11, PId = 1, ProductDetails = "Processor Intel(R) Cort(TM) i3 CPU, Ram 2GB", UnitPrice = 350000M, Quantity = 30 });
        list.Add(new Products { ProductId = 12, PId = 2, ProductDetails = "Processor Intel(R) Cort(TM) i3 CPU, Ram 2GB", UnitPrice = 550000M, Quantity = 30 });
        list.Add(new Products { ProductId = 13, PId = 3, ProductDetails = "Processor Intel(R) Cort(TM) i3 CPU, Ram 2GB", UnitPrice = 340000M, Quantity = 20 });
        list.Add(new Products { ProductId = 14, PId = 4, ProductDetails = "Processor Intel(R) Cort(TM) i3 CPU, Ram 2GB", UnitPrice = 350000M, Quantity = 30 });
        list.Add(new Products { ProductId = 15, PId = 5, ProductDetails = "Processor Intel(R) Cort(TM) i3 CPU, Ram 2GB", UnitPrice = 400000M, Quantity = 30 });
        list.Add(new Products { ProductId = 16, PId = 1, ProductDetails = "Processor Intel(RR) Cort(TM2) i7 CPU, Ram 8GB", UnitPrice = 650000M, Quantity = 30 });
        list.Add(new Products { ProductId = 17, PId = 2, ProductDetails = "Processor Intel(RR) Cort(TM2) i7 CPU, Ram 8GB", UnitPrice = 750000M, Quantity = 1 });
        list.Add(new Products { ProductId = 18, PId = 3, ProductDetails = "Processor Intel(RR) Cort(TM2) i7 CPU, Ram 8GB", UnitPrice = 450000M, Quantity = 30 });
        list.Add(new Products { ProductId = 19, PId = 4, ProductDetails = "Processor Intel(RR) Cort(TM2) i7 CPU, Ram 8GB", UnitPrice = 450000M, Quantity = 30 });
        list.Add(new Products { ProductId = 20, PId = 5, ProductDetails = "Processor Intel(RR) Cort(TM2) i7 CPU, Ram 8GB", UnitPrice = 370000M, Quantity = 5 });
        return list;
    }

    public List<Products> getPtoductType()
    {
        List<Products> list = new List<Products>();
        list.Add(new Products { PId = 1, ProductName = "Dell Laptop" });
        list.Add(new Products { PId = 2, ProductName = "HP Laptop" });
        list.Add(new Products { PId = 3, ProductName = "HCL Laptop" });
        list.Add(new Products { PId = 4, ProductName = "Linovo Laptop" });
        list.Add(new Products { PId = 5, ProductName = "Compac Laptop" });
        return list;
    }
}

No comments: