Solved

GridView Render Alternate Row Color Two White Two Gray?

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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Need a starter for ETL protocol? 4 57
ASP.net VB.net Errors when sending data to SQL 5 20
Output Caching in IIS 2 29
How to close an asp.net web form using C# ? 4 30
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

777 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