kb_
kb_

Reputation: 630

stringbuilder with if in appendformat

I would like to have a short if in a string builder AppendFormat so that I can add two different colors to a td when the qty are not the same.

Thats my try to do this:

foreach (var item in dataObj.Ord.LineColl)
{
    builder.AppendFormat(
        @"<tr><td align='right'> {0}</td>
          <td> {1}</td>
          <td> {2}</td>
          <td align='right'> {3} {4}</td>" +
            item.OrdQt == item.ShQt? 
            @"<td align='right' bgcolor='#FF000'> {5} {6}</td>" :
            @"<td align='right' bgcolor='#FFFFFFFF'> {5} {6}</td>"
            ,item.LineNumber, item.Product.Code, item.Product.Description,
                    item.OrdQt, item.OrdQt.Code,
                    item.QtyMes, item.OrdQt.Code,
                    item.ShQt, item.OrdQt.Code);
}

The problem is that the following error occurs:

Cannot implicitly convert type 'string' to 'bool'

Upvotes: 1

Views: 255

Answers (1)

Prescott
Prescott

Reputation: 7412

You can set the color to another variable and then include it in your AppendFormat. Note that there are more variables in that than there are in the string, I'm not sure which are right based on your snippet

foreach (var item in dataObj.Ord.LineColl)
{
    var color = (item.OrdQt == item.ShQt) ? '#ff000' : '#ffffff';

    builder.AppendFormat(
        @"<tr><td align='right'>{0}</td>
         <td>{1}</td>
         <td>{2}</td>
         <td align='right'>{3} {4}</td>
         <td align='right' bgcolor='{9}'> {5} {6}</td>", 
            item.LineNumber, item.Product.Code, item.Product.Description,
            item.OrdQt, item.OrdQt.Code, item.QtyMes, item.OrdQt.Code,
            item.ShQt, item.OrdQt.Code, color);
}

Upvotes: 4

Related Questions