How to change the gridview row color in asp.net [Resolved]

Posted by Allemahesh under ASP.NET on 7/1/2013 | Points: 10 | Views : 11550 | Status : [Member] [MVP] | Replies : 2
I have a grid-view. I want to change the grid-view row colors from code behind file.




Responses

Posted by: Satyapriyanayak on: 7/1/2013 [Member] [MVP] Silver | Points: 50

Up
0
Down

Resolved
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;
}

}

}
}


If this post helps you mark it as answer
Thanks

Allemahesh, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Jaskumar on: 7/2/2013 [Member] Starter | Points: 25

Up
0
Down
Use This code of Gridview Row bound event

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{

if (e.Row.RowType == DataControlRowType.DataRow)
{
string quantity = e.Row.Cells[5].Text;

foreach (TableCell cell in e.Row.Cells)
{
if (quantity == "Mumbai")
{
e.Row.Cells[5].BackColor = Color.Red;
}

}
}
}

Jas

Allemahesh, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response