Kᴀτᴢ
Kᴀτᴢ

Reputation: 2176

change background of every second row in gridview if row is visible

I have a gridview and on RowDataBound I use this code to change the background of every second row so the gridview can be read better:

 if (e.Row.RowIndex % 2 != 0)
            {
                e.Row.BackColor = System.Drawing.ColorTranslator.FromHtml("#f2f2f2");
            }

After that I use a dropdown to show only relevant rows of my gridview and hide (visible = false) the rows I didn't want to display. The rownumber doesn't change so the different background doesn't match for every second row now.

Now I'm looking to get the rownumbers of only the visible rows to change the color of every second row. Thanks

Upvotes: 2

Views: 2234

Answers (2)

Martin Parenteau
Martin Parenteau

Reputation: 73791

You can use the CSS technique mentioned in Alternate table row color using CSS?:

.alternatingRows > tbody > tr:nth-child(even)
{
    background-color: #f2f2f2;
}

The style class is applied to the GridView itself:

<asp:GridView ID="GridView1" runat="server" CssClass="alternatingRows" ... >

Upvotes: 1

Akshansh Jain
Akshansh Jain

Reputation: 407

Try AlternatingRowStyle property of the gridView.

https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.alternatingrowstyle(v=vs.110).aspx

A documentation on msdn to get you started.

Upvotes: 4

Related Questions