Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

GridView Render Alternate Row Color Two White Two Gray?

Posted on 2014-01-25
3
Medium Priority
?
407 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 2000 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

Tech or Treat!

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

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

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

610 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