Search code examples
c#asp.netgridviewrowdataboundalternating

Setting gridview row background colour by alternating by row data type


I have a GridView where I am listing values for client companies. Take for example the grid listed the company name and a value. I want to give a light gray background color to all Company 1's rows, then white for Company 2's rows and then back to light grey for Company 3's rows and alternating like so.


Company 1 12

Company 1 15

Company 1 18


Company 2 25

Company 2 78

Company 2 109


Company 3 66

Company 3 1


Can this be done simply in the _RowDataBound event, and if so how?


Solution

  • I managed to use this question

    alternate color gridview rows based on change of cell value asp.net

    to create my own solution in c#. So basically, create two global variables. Then within the _RowDatabound do the below

    int customerCompanyID = Convert.ToInt32(dr["IRPCustomerID"]);
    
     if (customerCompanyID != this._trafficSourceGridCurrentCompanyID)
                {
                    if (this._trafficSourceGridGroupingCssClass == 
    "BackGround2")
                    {
                        this._trafficSourceGridGroupingCssClass = "BackGround1";
                    }
                    else
                    {
                        this._trafficSourceGridGroupingCssClass = "BackGround2";
                    }
    
                    this._trafficSourceGridCurrentCompanyID = customerCompanyID;                    
                }
    
                e.Row.CssClass = _trafficSourceGridGroupingCssClass;