Click here to Skip to main content
14,355,239 members
Rate this:
Please Sign up or sign in to vote.
See more:
Hi friends,

I want to change the background color of gridview row based on database column value.

In database i have field as Result_Status
here, i have two types of Status as PASS and FAIL

If Pass the row back color should be Green
and if FAIL the row back color should be red.

We need to write code in Gridview Row Databound as

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


}
}


Please can you help me.

Thanks.
Posted
Rate this:
Please Sign up or sign in to vote.

Solution 1

you can apply the css class to a gridview row like following

protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        if(Result_Status == "pass")
        {
             e.Row.CssClass = "green";
        }
        else
        {
             e.Row.CssClass = "red";
        }
    }
}
   
v2
Comments
Member239258 25-Nov-14 1:36am
   
Please can you write the complete code in detail
PRAKASH9 25-Nov-14 1:39am
   
please see my updated solution
Member239258 25-Nov-14 1:43am
   
Sir, it shows error in Database column as

The name 'Result_status' does not exist in the current context

Please help.
PRAKASH9 25-Nov-14 1:47am
   
please paste your gridview binding code here.
Member239258 25-Nov-14 1:56am
   
No Sir, Same error....

The name 'Result_status' does not exist in the current context
Member239258 25-Nov-14 3:30am
   
Sir, This code is working..but i need help here...

IF Fail, it shows red color, if it pass it must show Green color and if it is Null it should show white color.

This is my code, it works good. but i also need green color if it show PASS.

if (e.Row.RowType == DataControlRowType.DataRow)
{
if ((string)(DataBinder.Eval(e.Row.DataItem, "Result_Status")) == "Fail")
{

e.Row.ForeColor = System.Drawing.Color.Red;


}


else
{
e.Row.ForeColor = System.Drawing.Color.White;
}


}


Please help thanks.
PRAKASH9 25-Nov-14 3:34am
   
if (e.Row.RowType == DataControlRowType.DataRow)
{
if ((string)(DataBinder.Eval(e.Row.DataItem, "Result_Status")) == "Fail")
{

e.Row.ForeColor = System.Drawing.Color.Red;


}
else if ((string)(DataBinder.Eval(e.Row.DataItem, "Result_Status")) == "PASS")
{

e.Row.ForeColor = System.Drawing.Color.Green;


}

else
{
e.Row.ForeColor = System.Drawing.Color.White;
}


}
Rate this:
Please Sign up or sign in to vote.

Solution 2

inside condition write

string statusVal=HttpUtility.HtmlDecode(e.Row.Cells[ColumnIndex].Text).Trim() ;

and columnIndex will be your columns index


then put if condition
if(statusVal=="PASS")
 e.Row.Cells[ColumnIndex].BackColor = ColorName;

ColorName is required color object
   
Comments
Member239258 25-Nov-14 1:36am
   
Please can you write the complete code in detail
Rate this:
Please Sign up or sign in to vote.

Solution 3

add below code, and please note its not tested some correction will require
protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
    String statusVal = HttpUtility.HtmlDecode(e.Row.Cells[ColumnIndex].Text).Trim();
    if(statusVal =="Pass")
        {
            foreach (TableCell cell in e.Row.Cells) {
                cell.BackColor = Color.Yellow;
                }
        {
    if(statusVal =="Fail")
        {
            foreach (TableCell cell in e.Row.Cells) {
                cell.BackColor = Color.Red;
                }
        {

    }
}
   
Comments
Member239258 25-Nov-14 2:03am
   
Sir, No color is changed.
Please help.
[no name] 25-Nov-14 3:23am
   
ColumnIndex will be the index number of your gridview column(zero based)
and please debug and check case(upper/lower) in if comparison
Member239258 25-Nov-14 3:29am
   
Sir, This code is working..but i need help here...

IF Fail, it shows red color, if it pass it must show Green color and if it is Null it should show white color.

This is my code, it works good. but i also need green color if it show PASS.

if (e.Row.RowType == DataControlRowType.DataRow)
{
if ((string)(DataBinder.Eval(e.Row.DataItem, "Result_Status")) == "Fail")
{

e.Row.ForeColor = System.Drawing.Color.Red;


}


else
{
e.Row.ForeColor = System.Drawing.Color.White;
}


}


Please help thanks.
[no name] 25-Nov-14 3:41am
   
why don't you try my code above and add one more if condition for null value and give while color, and currently I don't have Visual studio machine, so cant test what you have written...

All the best :)
Member239258 25-Nov-14 4:14am
   
ok Thanks, I did it.
[no name] 25-Nov-14 4:23am
   
Grt, if this discussion helped you, please mark it as useful solution.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100