Solved

GridView Render Alternate Row Color Two White Two Gray?

Posted on 2014-01-25
3
403 Views
Last Modified: 2014-01-25
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);
        }
0
Comment
Question by:WorknHardr
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 35

Expert Comment

by:Robert Schutt
ID: 39809058
Try:
if (rowIndex % 4 > 1)

Open in new window

0
 
LVL 35

Accepted Solution

by:
Robert Schutt earned 500 total points
ID: 39809074
If you want to skip a header row from counting, use:
if ((rowIndex - 1) % 4 > 1)

Open in new window

0
 

Author Closing Comment

by:WorknHardr
ID: 39809094
Excellent! thx :)
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question