views:

191

answers:

2

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

+1  A: 

I can't think of a more elegant way of doing this (at the moment) aside from this:

private DateTime _previousRowDateTime;
private string[] _commentRowClasses = {"commentRow", "altCommentRow"};
private int _commentRowClassesIndex = 0;

private string SetRowColor()
{
  if( _AddDate != _previousRowDateTime ) 
  {
    _commentRowClassesIndex = ( _commentRowClassesIndex + 1 ) % 2;
    _previousRowDateTime = _AddDate;
  }
  return _commentRowClasses[_commentRowClassesIndex];
}
ddc0660
Doesn't even compile. You can't set a DateTime to null and you can't just have _previousRowDate sit there without doing anything to it. None the less I think the use of mod% is what I needed. Thanks. ~ck
Hcabnettek
Sorry I was typing quickly and didn't run it through a compiler. I was more concerned about conveying the concept in the first place.
ddc0660
Man I still can't get this to work. I think the mod is the way to go but this implementation is not correct. it says if they are equal set them to equal, that doesn't seem right to me. What am I missing here? They are not initially equal so the index never gets updated.
Hcabnettek
Sorry once again. I think it's just a matter of checking for inequality instead of equality:"If _AddDate and _previousRowDateTime are not the same, then push the index and record the new previous row date time. If they are equal, just return the array's current index."Yea, I think that should do it.
ddc0660
That makes perfect sense. Thanks for your help.
Hcabnettek
A: 

What your code is saying:

If the last date stored is NOT equal to the "_AddDate" variable
    then SET it to that and return that this is a "commentRow".

If the last date stored IS equal to the "_AddDate" variable
    then simply return "altCommentRow".

So, on 2 consecutive rows with the same date where _AddDate has NOT changed, the first one will get the "commentRow" style and the second one, will get "altCommentRow".

If your goal is to rotate coloring so that all consecutive rows with the same date are one color, then, when a new date is reached, switch to the next color, you could try something like this:

private bool _AltFlag;
private string _PreviousDate;

private string SetRowColor()
{
    if (_AddDate != _PreviousDate)
    {
        _AltFlag = !_AltFlag;
    }

    return _AltFlag ? "altCommentRow" : "commentRow";
}

Essentially, we set up a bool to tell us which of the classes we're currently using. If our current date is not the same as the previous date, then flip the flag and return the new class. If it IS the same, we keep the same flag and return the class.

Jeff Wight
It looks like you've forgotten to add "_PreviousDate = _AddDate;" inside the if(...) statement
Igor Korkhov
yes, setting the date inside the if is needed for this to work. Thanks!
Hcabnettek