Click here to Skip to main content
14,095,911 members
Rate this:
 
Please Sign up or sign in to vote.
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
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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
   
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;
                    }
                }
            }
        }
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

try something like this:

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;
          }
     }
}
   
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.
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 4

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>
   
v2
Comments
Vincent Maverick Durano 24-Jul-18 7:39am
   
Thanks for sharing the solution. Glad it worked for you.
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 5

ya good it will be helpful to others
   
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)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Cookies | Terms of Service
Web06 | 2.8.190518.1 | Last Updated 20 Aug 2018
Copyright © CodeProject, 1999-2019
All Rights Reserved.
Layout: fixed | fluid

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