Home > Software design >  How can I show the color of a value in a <td> tag in C# based on condition?
How can I show the color of a value in a <td> tag in C# based on condition?

Time:11-26

I have a service build that sends mail. I create a table and send the values as an e-mail. I want to do it as <td style style='background-color:green> when my string values, for example, "Torque" value is "low". But I didn't know how to use the If condition here. My code is as below.

string aa = "<html> <style> table, th, td { border: 1px solid black; border-collapse: collapse; }</style><body> "  
                 $@"
            <table style='width: 100 %'>
           <tr>
              <th>Engine</th>
             <th>Risk(Torque)</th>
             <th>Risk(Flow)</th>
             <th>Risk(Power)</th>
           </tr>";
        foreach (var item in valuesRodBreak)
        {
            aa = aa   $@"
                  <tr>
                   <td>{item.Engine}</td>
                   <td>{item.Torque}</td>
                   <td>{item.Flow}</td>
                   <td>{item.Power}</td>
                  </tr>
                ";

        }
        aa  = $@"</table></body></html>";

I want to do it as <td style style='background-color:green>{item.Torque} in the tag above, which can be according to the if condition or something else. How can I do this?

CodePudding user response:

Use methods:

private string GetColorStyleAttribute(string value)
{
    switch(value)
    {
        case "Torque": 
            return " style='background-color:green";
        // ...
        default: 
            return "";
    }
}

aa = aa   $@"
      <tr>
       <td{GetColorStyleAttribute(item.Engine)}>{item.Engine}</td>
       <td{GetColorStyleAttribute(item.Torque)}>{item.Torque}</td>
       <td{GetColorStyleAttribute(item.Flow)}>{item.Flow}</td>
       <td{GetColorStyleAttribute(item.Power)}>{item.Power}</td>
      </tr>
    ";
  • Related