Gridview row color

In this blog we will know how to change the grid view row color according to some given conditions.

 

Scenario

 

 I have a Dropdown list with two items that is "Important" and "Very Important" and a textbox for 'short note'. I have saved those to database. I want the data to load on grid view but with colored row. I mean for the data that is  'Important' i want the row to be colored 'blue' and for the data that is  'very important' i want the row to be colored 'red'.

 

 

<%@ 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>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
   
    </div>
    <asp:DropDownList ID="DropDownList1" runat="server">
    </asp:DropDownList><br />
    <asp:Label ID="Label1" runat="server" Text="Short Notes" Width="100px"
        Font-Bold="True"></asp:Label>
    <asp:TextBox ID="TextBox1" runat="server" Height="58px" TextMode="MultiLine"></asp:TextBox>
   
    <br />
    <br />
    <br />
    <asp:Button ID="btn_insert" runat="server" onclick="btn_insert_Click"
        Text="Insert" />
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false"
         onrowdatabound="GridView1_RowDataBound">
         <Columns>
         <asp:BoundField DataField="shortnote" HeaderText="Shortnote" />
         </Columns>
    </asp:GridView>
    </form>
</body>
</html>


using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Drawing;
public partial class _Default : System.Web.UI.Page
{
    string connStr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
    SqlCommand com;
    SqlDataAdapter sqlda;
    DataSet ds;
    string str;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            DropDownList1.Items.Add("Select");
            DropDownList1.Items.Add("Important");
            DropDownList1.Items.Add("very Important");
        }
       

    }
    protected void btn_insert_Click(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection(connStr);
        con.Open();
        str = "insert into Details(shortnote,flag) values('" + TextBox1.Text + "','" + DropDownList1.SelectedItem.Text + "')";
        com = new SqlCommand(str, con);
        com.ExecuteNonQuery();
        con.Close();
        bindgrid();
    }
    void bindgrid()
    {
        SqlConnection con = new SqlConnection(connStr);
        con.Open();
        str = "select * from Details";
        com = new SqlCommand(str, con);
        sqlda = new SqlDataAdapter(com);
        ds = new DataSet();
        sqlda.Fill(ds, "Details");
        GridView1.DataSource = ds;
        GridView1.DataMember = "Details";
        GridView1.DataBind();
        con.Close();
    }

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            if (Convert.ToString(DataBinder.Eval(e.Row.DataItem, "flag")) == "Important")
            {
                e.Row.BackColor = System.Drawing.Color.Blue;
            }
            else
            {
                e.Row.ForeColor = System.Drawing.Color.Red;
            }

        }

    }
}




Ebook Download
View all
Learn
View all