The best way to change its style based on the value of a column is to think of ways to datarowbound the GridView event. The Datarowbound event is executed immediately after the rows in the GridView are bound to the data. The Datarowbound event uses the GridViewRowEventArgs class as the event variable. You can use the GridViewRowEventArgs property to manipulate rows that have been bound to data through event variables.
Copy Code code as follows:
protected void GridView1_RowDataBound (object sender, GridViewRowEventArgs e)
{
gridviewrow row = E.row;
}
Row returns a GridViewRow object in the TableRow class.
The bound row has several different types. For example: DataRow, Emptydatarow, Footer, Header, Pager and Separator. The RowType property of the GridView can get the row type of the current row. RowType is a set of Datacontrolrow enumerations.
Look at the following code example to detect whether the row listed by the GridView is a standard type of row.
Copy Code code as follows:
protected void GridView1_RowDataBound (object sender, GridViewRowEventArgs e)
{
if (E.row.rowtype = = Datacontrolrowtype.datarow)
{
//do something!
}
}
You can use the Cells property of the row to get its cells, which returns a Tablecellcollection object. The specific cells is then obtained through the tablecellcollection index. The tablecellcollection index returns a Tabelcell object that corresponds to a cell in the row:
Copy Code code as follows:
protected void GridView1_RowDataBound (object sender, GridViewRowEventArgs e)
{
if (E.row.rowtype = = Datacontrolrowtype.datarow)
{
String value = E.row.cells[0]. Text;
}
}