Saturday 26 July 2014

dynamically change GridView cell color based on cell value in ASP.NET


 dynamically change GridView cell color based on cell value in ASP.NET


Description:-

In this example we explain that how to change the color of the gridview cell based on some condition. Or dynamically change Gridview cell color based on cell value condition in asp.net.or how to dynamically change the Background color of ASP.Net GridView Row based on some conditions


Sometime we have requirement like display data in grid in different color so end user can easily understand. Suppose we have one table student with coloumn name age. If age<= 15 then we have to cell color="yellow". If age>=15 then we have to set gridview cell color ="Red".to achieve this type of functionality we have to use RowDataBound event of the Gridview.

So to change the gridview row background dynamically based on some condition you have to write the below code.

Restrict the size of File when Uploaded How to Restrict the size of File when uploaded by user

Dynamically Read/Write File in asp.Net How to Read Write File in Asp.Net using C#

Default2.aspx:-

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

<!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 id="Head1" runat="server">
    <title>GridView formatting or dynamically change GridView cell color based on cell value
        in ASP.NET</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="grd_student" runat="server" Width="700px" AutoGenerateColumns="False"
            OnRowDataBound="grd_student_RowDataBound" BackColor="#DEBA84" BorderColor="#DEBA84"
            BorderStyle="None" BorderWidth="1px" CellPadding="3" CellSpacing="2">
            <FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" />
            <HeaderStyle BackColor="#A55129" Font-Bold="True" Font-Names="cambria" ForeColor="White"
                Height="25px" />
            <PagerStyle ForeColor="#8C4510" HorizontalAlign="Center" />
            <RowStyle Font-Names="Calibri" Height="25px" BackColor="#FFF7E7" ForeColor="#8C4510" />
            <Columns>
                <asp:BoundField DataField="stud_id" HeaderText="Student ID" />
                <asp:BoundField DataField="stud_name" HeaderText="Student Name" />
                <asp:BoundField DataField="stud_age" HeaderText="Student Age" />
            </Columns>
            <SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="White" />
            <SortedAscendingCellStyle BackColor="#FFF1D4" />
            <SortedAscendingHeaderStyle BackColor="#B95C30" />
            <SortedDescendingCellStyle BackColor="#F1E5CE" />
            <SortedDescendingHeaderStyle BackColor="#93451F" />
        </asp:GridView>
    </div>
    </form>
</body>
</html>


Default2.aspx.cs:-

using System;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

public partial class Default2 : System.Web.UI.Page
{

    SqlConnection conn = new SqlConnection("Data Source=kiritdemo;Initial Catalog=Demo;Integrated Security=True");
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Populate_studGrid();
        }
    }
    protected void Populate_studGrid()
    {
        DataSet ds = new DataSet();
        string cmdstr = "select * from student";
        SqlCommand cmd = new SqlCommand(cmdstr, conn);
        SqlDataAdapter adp = new SqlDataAdapter(cmd);
        adp.Fill(ds);
        grd_student.DataSource = ds;
        grd_student.DataBind();
    }

    protected void grd_student_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            int age = int.Parse(e.Row.Cells[2].Text); //it cell represent a student age

            if (age <= 14)
                e.Row.Cells[2].BackColor = System.Drawing.Color.Black;
            else if (age <= 30)
                e.Row.Cells[2].BackColor = System.Drawing.Color.Yellow;
            else
                e.Row.Cells[2].BackColor = System.Drawing.Color.Pink;
        }
    }
}



0 comments:

Post a Comment