Hello All, I need to alternate row colors in a grid, but not on every other row. I have a variable _AddDate that I can check on the GridRowBound event. If it hasn't changed, I want to apply one css class and if it has I want to apply a different class. The code I have does almost exactly what I want but I am setting the class on the row when the value changes and each concurrent row that should be the same class is having the incorrect class applied. Its definitely something wrong with my method. Can anyone point me in the right direction? Also is there a name for these types of functions. I have to do things like this from time to time and they can be tricky to figure out the correct algorithm. Here is what I have.
private void GridRowBound(object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.CssClass = SetRowColor();
}
}
private DateTime _dateToSwitch;
private string SetRowColor()
{
var tmpDate = _AddDate;
var doSwitch = (tmpDate == _dateToSwitch);
if (!doSwitch)
{
_dateToSwitch = tmpDate;
return "commentRow";
}
return "altCommentRow";
}
I have another function that correctly sets _AddDate to the appropriate value so it is always current when it is evaluated.
Any help is appreciated. Happy Friday!
Cheers, ~ck in San Diego