user3510330
user3510330

Reputation: 97

Column's value color change based on condition

I use placeholder control for generating dynamic table. Now I need to make color of one's column value based below on condition, but my if condition is not working. How to do it? Here is my code:

for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
 strResults.Append("<tr style='color: black;'>");
 strResults.Append("<td>" + ds.Tables[0].Rows[i]["Description"] + "</td>");
 strResults.Append("<td>" + ds.Tables[0].Rows[i]["Target"] + "</td>");
 strResults.Append("<td>" + ds.Tables[0].Rows[i]["Actual"] + "</td>");
 if(ds.Tables[0].Rows[i]["Achievement"] >94% )
    {
 strResults.Append("<td style='color: green;'>" + ds.Tables[0].Rows[i]["Achievement"] + "</td>");
  }
 strResults.Append("</tr>");
  }

Upvotes: 2

Views: 591

Answers (1)

sujith karivelil
sujith karivelil

Reputation: 29006

You are not allowed to do comparison Like what you are doing. Better you parse the Output in ds.Tables[0].Rows[i]["Achievement"] to double and then compare them with 94 not with 94%; And also parse the double value from the input before comparing it. I suggest to use double.TryParse() for comparison

double Achievement = 0.0;

if (double.TryParse(ds.Tables[0].Rows[i]["Achievement"].ToString().Replace("%",""), out Achievement) 
    && Achievement > 94)
{
    strResults.Append("<td style='color: green;'>" + ds.Tables[0].Rows[i]["Achievement"] + "</td>");
}            
else
{
    strResults.Append("<td>" + ds.Tables[0].Rows[i]["Achievement"] + "</td>");
}
strResults.Append("</tr>");

Upvotes: 1

Related Questions