开发者

C# help to set a Row Css class of a Grid View

开发者 https://www.devze.com 2022-12-16 18:41 出处:网络
I need to al开发者_如何转开发ternate 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

I need to al开发者_如何转开发ternate 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


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];
}


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号