Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
post removed due to security reasons
post removed due to security reasons
post removed due to security reasons
post removed due to security reasons
post removed due to security reasons


What I have tried:

Code removed
Code removed
Code removed
Code removed
Code removed
Code removed
Code removed
Posted
Updated 20-Aug-18 0:21am
v5

use jquery
pass the column index
and use this.text() for evry row and compare the value in the if condition
and apply .css to change the color
https://www.aspsnippets.com/Articles/Dynamically-change-GridView-Row-Background-Color-based-on-condition-in-ASPNet-using-C-and-VBNet.aspx
 
Share this answer
 
v2
Comments
Member 13920580 21-Jul-18 8:47am    
can you please be more specific? I'm new to asp.net
aparnalakshmi 22-Jul-18 15:00pm    
go through the link I provided
Member 13920580 23-Jul-18 5:56am    
I did what it said on that link but still, I can't get it worked, this is the code I used


        protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                string ostatus = e.Row.Cells[8].Text;

                foreach (TableCell cell in e.Row.Cells)
                {
                    if (ostatus == "Accepted")
                    {
                        cell.BackColor = Color.Red;
                    }
                }
            }
        }
try something like this:

C#
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
     if (e.Row.RowType == DataControlRowType.DataRow)
     {
          DataRowView drv = e.Row.DataItem as DataRowView;
          if (drv["ostatus"].ToString().ToLower().Equals("accepted"))
          {
              //to change the Row color
              e.Row.BackColor = System.Drawing.Color.Green;

              //to change the Cell color
              e.Row.Cells[8].BackColor = System.Drawing.Color.Green;
          }
     }
}
 
Share this answer
 
Comments
Member 13920580 23-Jul-18 5:58am    
Changed the GV id to 5 and tried it on the codebehind but it didn't work :(
Vincent Maverick Durano 24-Jul-18 7:39am    
BoundField should work. You just have to tell us what exactly do you meant by “it doesnt work”. Are you getting error? Did you tried debugging the code and stepping into it to figureout what’s going on.
I've used TemplateField instead of BoundField which resolved my problem, it might help someone else so I will put the codes here.


<asp:TemplateField HeaderText="وضعیت">
    <ItemTemplate>
        <span style="background-color: <%# (Eval("ostatus").ToString() == "Accepted") ? "green;" : (Eval("ostatus").ToString() == "Denied") ? "red;" : "blue;" %>">
            <%# Eval("ostatus") %>
        </span>
    </ItemTemplate>
</asp:TemplateField>
 
Share this answer
 
v2
Comments
Vincent Maverick Durano 24-Jul-18 7:39am    
Thanks for sharing the solution. Glad it worked for you.
ya good it will be helpful to others
 
Share this answer
 
Comments
Vincent Maverick Durano 24-Jul-18 7:36am    
Please post reply as comments instead of solution. Thank you.

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