Sometimes it is necessary to change the CSS for a row depending on data. The following example is pulled from
http://asp.net/learn/dataaccess/tutorial11cs.aspx?tabid=63protected void HighlightCheapProducts_RowDataBound(object sender, GridViewRowEventArgs e)
{
// Make sure we are working with a DataRow
if (e.Row.RowType == DataControlRowType.DataRow)
{
// Get the ProductsRow object from the DataItem property...
Northwind.ProductsRow product = (Northwind.ProductsRow)
((System.Data.DataRowView)e.Row.DataItem).Row;
if (!product.IsUnitPriceNull() && product.UnitPrice < 10m)
{
e.Row.CssClass = "AffordablePriceEmphasis";
}
}
}
However, when the GridView is rendered with the adapter, "AffordablePriceEmphasis" is ignored completely. In the source code of GridViewAdapter.cs, in the method GetRowClass, the first line is
string className = "";
I simply replaced it with
string className = row.CssClass;
and now it works as expected. It would be nice to see this changed in the CodePlex source as well. Note that this does not overwrite anything, it simply adds a second class like
<tr class="AffordablePriceEmphasis AspNet-GridView-Alternate">
Cheers!