Click here to Skip to main content
16,004,452 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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

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


}
}


Please can you help me.

Thanks.
Posted

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";
        }
    }
}
 
Share this answer
 
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
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
 
Share this answer
 
Comments
Member239258 25-Nov-14 1:36am    
Please can you write the complete code in detail
add below code, and please note its not tested some correction will require
C#
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;
                }
        {

    }
}
 
Share this answer
 
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.

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900