Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding CSS Class on RowDataBound

Tags:

asp.net

I'm trying to append a CSS class to a row on RowDataBound. I'm using the alternating css class property against the GridView, so I'm assuming this is applied on RowDataBound. If you assign a CSS class programatically to the CssClass property of the row within the RowDataBound event then the alternating row style css class is not applied, even if you append the CSS class.

Here's what I've got:

Protected Sub gvGeneral_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvGeneral.RowDataBound
    If e.Row.RowType = DataControlRowType.DataRow Then
      If previousExpenseType <> e.Row.Cells(2).Text And previousExpenseType.Length > 0 Then
        e.Row.CssClass += "bottom-border"
      End If

      previousExpenseType = e.Row.Cells(2).Text
    End If
  End Sub

previousExpenseType is just a String which I'm doing comparisons against. If the expense type changes, then I want to have a border applied to the bottom of the <tr> element.

Any ideas how to get around this? It seems there's an event after RowDataBound that's applying the alternating row style css class to the row.

like image 479
Kezzer Avatar asked May 27 '09 11:05

Kezzer


1 Answers

try this in your RowDataBound event handler...

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    GridView grid = GridView1;
    GridViewRow row = e.Row;
    if (row.RowType == DataControlRowType.DataRow)
    {
        bool isAlternating = row.RowState == DataControlRowState.Alternate;

        List<string> classes = new List<string>();

        /*  Setting the CssClass of a row overwrites any CssClass declared in the 
         *  markup, so lets save the value that is in the markup and add to it.
         */
        if (isAlternating) { classes.Add(grid.AlternatingRowStyle.CssClass); }
        else { classes.Add(grid.RowStyle.CssClass); }

        //
        //logic for adding other css classes to the row...
        //

        //set the CssClass property of the row to the combined css classes value
        row.CssClass = string.Join(" ", classes.ToArray());

        //
        //more row processing...
        //
    }
}
like image 123
bba Avatar answered Sep 28 '22 08:09

bba