protected void GridViewProfit_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Cells[0].Text = (e.Row.DataItemIndex + 1).ToString(); if (Convert.ToDecimal(e.Row.Cells[3].Text) > 0)
{
e.Row.Cells[3].ForeColor = System.Drawing.Color.Red;
e.Row.Cells[4].ForeColor = System.Drawing.Color.Red;
}
else if (Convert.ToDecimal(e.Row.Cells[3].Text) < 0)
{
e.Row.Cells[3].ForeColor = System.Drawing.Color.Green;
e.Row.Cells[4].ForeColor = System.Drawing.Color.Green;
}
}
}
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Cells[0].Text = (e.Row.DataItemIndex + 1).ToString(); if (Convert.ToDecimal(e.Row.Cells[3].Text) > 0)
{
e.Row.Cells[3].ForeColor = System.Drawing.Color.Red;
e.Row.Cells[4].ForeColor = System.Drawing.Color.Red;
}
else if (Convert.ToDecimal(e.Row.Cells[3].Text) < 0)
{
e.Row.Cells[3].ForeColor = System.Drawing.Color.Green;
e.Row.Cells[4].ForeColor = System.Drawing.Color.Green;
}
}
}
if (Convert.ToDecimal(e.Row.Cells[3].Text) > 0)
{
e.Row.Cells[3].Text="<font color='#FF0000'>e.Row.Cells[3].Text</font>";
e.Row.Cells[4].Text="<font color='#FF0000'>e.Row.Cells[4].Text</font>";
}
<head runat="server">
<title></title>
<style>
.simtd
{
color: #fff;
background-color: Blue;
}
.emptytd
{
color: #FFFF00;
background-color: Green;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<asp:GridView ID="GridView1" runat="server" CssClass="table" OnRowDataBound="GridView1_RowDataBound">
</asp:GridView>
</form>
</body> protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Cells[0].Text = (e.Row.DataItemIndex + 1).ToString(); if (Convert.ToDecimal(e.Row.Cells[0].Text) > 2)
{
//e.Row.CssClass = ""; 定义整行的样式
e.Row.Cells[0].CssClass = "simtd";
}
else
{
e.Row.Cells[0].CssClass = "emptytd";
}
}
}