When the gridview wants to dynamically change the background color of the row and column based on the value of the cell, it can write the judgment in the rowdatabound event. In this event, the value of the cell can be obtained to determine and change the color of the display;
For example, a value column determines the color of the column based on the value of this column. When the value is greater than 0, the background color is blue, and the background color is red.CodeAs follows (*. aspx. CS ):
Protected Void Gridview1_rowdatabound ( Object Sender, gridviewroweventargs E)
{
Datarowview orow;
If (E. Row. rowtype = Datacontrolrowtype. datarow)
{
Orow = (Datarowview) E. Row. dataitem;
// The color of the cell depends on the value. Values:> 0, blue, and Red. <0, Red;
If (Convert. todouble (orow [ " Value " ]. Tostring ()) < 0 )
{
E. Row. backcolor = System. Drawing. color. Red;
}
Else
{
E. Row. backcolor = System. Drawing. color. blue;
}
// Or color the last column of a cell.
If (Convert. todouble (orow [ " Value " ]. Tostring ()) < 0 )
{
E. Row. cells [E. Row. cells. Count - 1 ]. Backcolor = System. Drawing. color. Red;
}
Else
{
E. Row. Celle [E. Row. cells. Count - 1 ]. Backcolor = System. Drawing. color. blue;
}
}
}