GridView Render Alternate Row Color Two White Two Gray?

I need a GridView to display data with alternating row-pair row colors as:
 1 & 2 = white
 3 & 4 = gray
 5 & 6 = white
 etc...

I'm using the Render(HtmlTextWriter) method to produce alt row colors.

Note:
 1 - the rows/columns are rendered dynamically with a dataset
 2- the Gridview has AutoGenerateColumns="true"

[aspx]
      <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="true" >
        <Columns>
        </Columns>
    </asp:GridView>

[.cs]
       protected override void Render(HtmlTextWriter writer)
        {
            if (GridView1.Rows.Count != 0)
            {
                Table gridTable = (Table)GridView1.Controls[0];
                string lastValue = string.Empty;
                string currentValue = string.Empty;

                foreach (GridViewRow row in GridView1.Rows)
                {
                    int rowIndex = gridTable.Rows.GetRowIndex(row);

                    if (rowIndex % 2 != 0) //alt row colors, need 2-white 2-gray
                    {
                        row.BackColor = System.Drawing.Color.Gainsboro;
                    }
                }
            }
            base.Render(writer);
        }
WorknHardrAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
Robert SchuttConnect With a Mentor Software EngineerCommented:
If you want to skip a header row from counting, use:
if ((rowIndex - 1) % 4 > 1)

Open in new window

0
 
Robert SchuttSoftware EngineerCommented:
Try:
if (rowIndex % 4 > 1)

Open in new window

0
 
WorknHardrAuthor Commented:
Excellent! thx :)
0
All Courses

From novice to tech pro — start learning today.