2

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

Kᴀτᴢ
  • 2,146
  • 6
  • 29
  • 57

2 Answers2

4

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.

Akshansh Jain
  • 297
  • 1
  • 5
  • 19
  • thanks but the result is still the same, I only hide the row (`visible=false`) and not delete them – Kᴀτᴢ Sep 14 '16 at 12:23
  • Well then, try to change the AlternatingRowStyle property from your codeback file. Where you assign the row's visiblity to false just after that set the alternatingrowstyle property of the gridview to whatever you want to. Now I have worked in C# so in C# you can do it like this `dataGridView1.AlternatingRowsDefaultCellStyle.BackColor = Color.DarkGray;` – Akshansh Jain Sep 14 '16 at 13:59
  • More information here - https://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.alternatingrowsdefaultcellstyle(v=vs.110).aspx – Akshansh Jain Sep 14 '16 at 14:04
  • the answer of ConnorsFan works for me. +1 anyway. Thanks – Kᴀτᴢ Sep 15 '16 at 07:50
1

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" ... >
Community
  • 1
  • 1
ConnorsFan
  • 70,558
  • 13
  • 122
  • 146