Solved

GridView Render Alternate Row Color Two White Two Gray?

Posted on 2014-01-25
3
393 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
  • 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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
HTML table 7 54
Using MS Code on my Mac 6 45
JavaScript error 1 33
Hidden Field Value 10 34
Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

758 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now