Tuesday, November 5, 2024

ASP.NET: Control the Number of Alternate Rows in GridView

The GridView control (as well as the DataGrid) has a way of defining the style of every other row of the grid.

My favorite way of doing it, is to set a class on the row and then by stylesheet set the background like so:

<asp:gridview AlternatingRowStyle-CssClass="alt" />

And then add this to the stylesheet.

.alt{

   background-color: #F1F1F1;

}

This approach works very well for me on both the GridView and the old DataGrid, but there is a very important thing that is missing – the ability to have e.g. three rows be alternate and three regular throughout the grid. The alternate rows are always every other row and you can’t do anything about it without overriding the GridView and provide the functionality yourself.

Implementation

Download the AlternateGridView.cs below and add it to the App_Code folder of your website. Then put the following tags unto the page or user control where you want the grid. The AlternateGridView class is only 47 lines of code including XML comments. You can still use the same style to color the background as shown above.

<%@ Register Namespace="CustomControls" TagPrefix="cc" %>

<cc:AlternateGridView runat="server" ID="grid" AlternateRows="3" />

Download

AlternateGridView.zip (0,56 KB)

Comments

Tag:

Add to Del.icio.us | Digg | Reddit | Furl

Bookmark Murdok:

Mads Kristensen currently works as a Senior Developer at Traceworks located
in Copenhagen, Denmark. Mads graduated from Copenhagen Technical Academy with a multimedia degree in
2003, but has been a professional developer since 2000. His main focus is on ASP.NET but is responsible for Winforms, Windows- and
web services in his daily work as well. A true .NET developer with great passion for the simple solution.

Home

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles