User1561948904 posted
I was trying to change the colour of a row in a gridview depending on data contained within using MyGridView_RowDataBound, and found a nice little change to the css adapters to allow this. Thought I should share.
Add the following code before "return className.Trim();" in the function GetRowClass(GridView gridView, GridViewRow row) in GridViewAdapter.cs. (I keep this in ~/App-Code/Adapters/)
if ((row.RowState == DataControlRowState.Normal))
{
className += row.CssClass;
}
In your gridview css file add the colors you need EG:
.MyGridViewCSSselectorClass
.AspNet-GridView
table tbody
tr.AspNet-GridView-Red
td
{
background: Red;
}
.MyGridViewCSSselectorClass
.AspNet-GridView
table tbody
tr.AspNet-GridView-Green
td
{
background: Green;
}
In your web page create a RowDataBound function. To over ride the alternate row style change the row state to normal on every row you wish to change, EG:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
try
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
foreach (Row in the gridview)
{
e.Row.RowState = DataControlRowState.Normal;
if (somethingIsTrue)
e.Row.CssClass = "AspNet-GridView-Red";
else if (somethingElseIsTrue)
e.Row.CssClass = "AspNet-GridView-Green";
}
}
}
catch (Exception ex)
{
throw ex;
}
}
hope this makes sense!