根据C#中的条件更改行颜色

时间:2014-01-20 18:44:04

标签: c#-4.0

嗨,我对C#很新。但我有这个网页打印出一些结果,我正在尝试改变,我想做的是做一个if声明。如果row [4]小于[2]则该行应为红色。我试图复制一些代码。

//##############Print result##############
foreach ( string[] row in dataResult)
{
//Birn.Util.cellDecimalPrinter(row[0],"#,0.0")
//<tr id="PopUp" onclick="OpenWindow('file.aspx?querystr=row[0]&date=textbox.text','500','300')">
%>
<tr>
<td><%=row[0]%></td>
<td><%=row[1]%></td>
<td><%=row[2]%></td>
<td><%=row[3]%></td>
<td><%=row[4]%></td>
</tr>
<%
}

1 个答案:

答案 0 :(得分:0)

您可以使用CSS更改行的样式。我假设你在第2行和第4行比较的是一个Decimal的字符串表示:如果需要,将Convert.ToDecimal调整为适当的类型:

foreach ( string[] row in dataResult)
{
    string redStyle= "";
    If (Convert.ToDecimal(row[4]) < Convert.ToDecimal(row[2])) {
        // ideally you would have a style defined in a separate CSS file for this
        redStyle = " style=\"background-color: Red; color: White\"";
    }
}
%>
<tr<%=redStyle%>>
<td><%=row[0]%></td>
<td><%=row[1]%></td>
<td><%=row[2]%></td>
<td><%=row[3]%></td>
<td><%=row[4]%></td>
</tr>
<%
}