Tuesday, 7 August 2012

How to change cell's backcolor of Gridview with conditional


How to change cell's backcolor of Gridview with conditional






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


<!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>Untitled Page</title>
   
   
  
  
    </head>
<body >
    <form id="form1" runat="server">
    <asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>
   
    <asp:GridView ID="grd1" runat ="server" Width="600px" Height ="600px" ShowHeader="true"
    ShowFooter="true" AutoGenerateColumns ="false" AllowPaging="true"
            onpageindexchanging="grd1_PageIndexChanging"
            onrowcreated="grd1_RowCreated" Font-Bold="True" Font-Size="Medium"
            ForeColor="Black"  AllowSorting="true"
        OnRowDataBound  ="grd1_RowDataBound" Font-Names="Verdna" 
            >
    <Columns >
    <asp:TemplateField >
    <HeaderStyle  ForeColor="Blue" Font-Bold ="true"  />
    <HeaderTemplate >
    Sr. No
   
    </HeaderTemplate>
   
    <ItemTemplate>
    <div style="width :100%;">
    <asp:Label ID="srno" runat ="server" Text='<%# Container.DataItemIndex +1 %>' />
    </div>
    </ItemTemplate>
   
   
    </asp:TemplateField>
   
   
    <asp:TemplateField >
     <HeaderStyle  ForeColor="Blue" Font-Bold ="true" />
    <HeaderTemplate>
    Roll
   
    </HeaderTemplate>
    <EditItemTemplate>
  
    <asp:TextBox ID="txtrollno" runat ="server" Text='<%# Bind("Roll") %>'  />
  
    </EditItemTemplate>
    <ItemTemplate>
  
    <asp:Label  ID="lblrollno" runat ="server" Text='<%# Bind("Roll") %>'  />
  
    </ItemTemplate>
   
   
    </asp:TemplateField>
     <asp:TemplateField >
     <HeaderStyle  ForeColor="Blue" Font-Bold ="true" />
        
    <HeaderTemplate>
   Name
   
    </HeaderTemplate>
   
     <EditItemTemplate>
  
    <asp:TextBox ID="txtname" runat ="server" Text='<%# Bind("name") %>'  />
  
    </EditItemTemplate>
    <ItemTemplate>
  
    <asp:Label  ID="lblname" runat ="server" Text='<%# Bind("name") %>'  />
  
    </ItemTemplate>
    </asp:TemplateField>
    </Columns>
    <PagerStyle BackColor="#3366CC" ForeColor="#000099" />
    <HeaderStyle BackColor="#9966FF" />
    </asp:GridView>
    <div>
    <asp:UpdatePanel ID ="upd1" runat ="server" >
    </asp:UpdatePanel>
    </div>
   
    </form>
   
</body>
</html>


C# code:-

Global Declaration

string strcon = "Data Source=FWSI-C25\\SqlExpress;Initial Catalog=Employee;Integrated Security=True";
    SqlConnection con;
  static   DataSet ds;
    SqlDataAdapter da;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            bind();
        }
    }
    public void bind()
    {
        da = new SqlDataAdapter("select * from stu_info", strcon);
        ds = new DataSet();
        da.Fill(ds);
        grd1.DataSource = ds.Tables[0];
        grd1.DataBind();
    }

Gridview rowcreated event

    protected void grd1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
       
           
            if (e.Row.RowType == DataControlRowType.DataRow)
            {

                if (((Label)e.Row.Cells[2].Controls[1]).Text != null)
                {

                    if (((Label)e.Row.Cells[2].Controls[1]).Text.Substring(0, 1).ToLower () == "s")
                    {
                        e.Row.Cells[2].BackColor = System.Drawing.Color.BlueViolet;
                        e.Row.Cells[2].ForeColor = System.Drawing.Color.White;
                        e.Row.Cells[2].Font.Bold = true;
                    }
                    if (((Label)e.Row.Cells[2].Controls[1]).Text.Substring(0, 1).ToLower () == "n")
                    {
                        e.Row.Cells[2].BackColor = System.Drawing.Color.OrangeRed;
                        e.Row.Cells[2].ForeColor = System.Drawing.Color.White;
                        e.Row.Cells[2].Font.Bold = true;
                    }
                    if (((Label)e.Row.Cells[2].Controls[1]).Text.Substring(0, 1).ToLower() == "r")
                    {
                        e.Row.Cells[2].BackColor = System.Drawing.Color.Orange;
                        e.Row.Cells[2].ForeColor = System.Drawing.Color.White;
                        e.Row.Cells[2].Font.Bold = true;
                    }
                    if (((Label)e.Row.Cells[2].Controls[1]).Text.Substring(0, 1).ToLower() == "m")
                    {
                        e.Row.Cells[2].BackColor = System.Drawing.Color.Black ;
                        e.Row.Cells[2].ForeColor = System.Drawing.Color.White;
                        e.Row.Cells[2].Font.Bold = true;
                    }
                    if (((Label)e.Row.Cells[2].Controls[1]).Text.Substring(0, 1).ToLower() == "d")
                    {
                        e.Row.Cells[2].BackColor = System.Drawing.Color.Firebrick;
                        e.Row.Cells[2].ForeColor = System.Drawing.Color.White;
                        e.Row.Cells[2].Font.Bold = true;
                    }
                  
                }


                if (Convert.ToInt32(((Label)e.Row.Cells[1].Controls[1]).Text) % 2 == 0)
                {
                    e.Row.Cells[1].BackColor = System.Drawing.Color.Green;
                    e.Row.Cells[1].ForeColor = System.Drawing.Color.White;
                    e.Row.Cells[1].Font.Bold = true;
                }
                else
                {
                    e.Row.Cells[1].BackColor = System.Drawing.Color.Red;
                    e.Row.Cells[1].ForeColor = System.Drawing.Color.White;
                    e.Row.Cells[1].Font.Bold = true;
                }
                if (Convert.ToInt32(((Label)e.Row.Cells[0].Controls[1]).Text) % 2 == 1)
                {
                    e.Row.Cells[0].BackColor = System.Drawing.Color.Green;
                    e.Row.Cells[0].ForeColor = System.Drawing.Color.White;
                    e.Row.Cells[0].Font.Bold = true;
                }
                else
                {
                    e.Row.Cells[0].BackColor = System.Drawing.Color.Red;
                    e.Row.Cells[0].ForeColor = System.Drawing.Color.White;
                    e.Row.Cells[0].Font.Bold = true;
                }
            }
        }
       
    }





0 comments: