Deborah's Developer MindScape

         Tips and Techniques for Web and .NET developers.

February 9, 2010

ASP.NET: Styling a GridView

Filed under: ASP.NET,C#,VB.NET @ 6:20 pm

This post provides some tips for styling the GridView control.

The prior post here demonstrates how to use the GridView with your business objects. This post builds upon that example and demonstrates how to style a GridView. Use these techniques any time you want to add some style to your GridView control, regardless of how the GridView was populated.

By default, a sorted and paged DataGrid looks something like this:


In this example, the application color scheme was a shade of orange, so the desired GridView design is as follows:


But you can replace the color with whatever color matches your design.

If you plan to use stylesheets, which I highly recommend, then styling your GridView requires two steps. First, define the desired stylesheet elements. Second, apply the style elements in the GridView tag.

Define the Stylesheet Elements

Define the desired stylesheet elements in the CSS file for your application. In this example, the stylesheet is called SampleStyleSheet.css.


/*———– Grid ———————————–*/

.GridHeader a

.GridHeader a:active


    background-color:#f2f2f2;    /* Light gray */


.GridPager a

In this example where the grid is sortable, the grid header is a hyperlink. So the .GridHeader CSS class is not really used. The .GridHeader a element is used to show the hyperlinks in the column header of the grid. The .GridHeader a:active element defines how the hyperlink should look when it is activated. By setting a different color in the a and a:active styles, the hyperlink changes color while the grid is sorted.

The .GridRow CSS class defines the style for the basic grid row. In this case, the text color is set to the orange color scheme, but you can set it to any color. If you want the standard rows to have a background color, you can set that as well.

The .GridAlternatingRow CSS class defines the style for every other row. In this example, the color remains the same as the normal row, but a light gray background color is set.

The .GridPager CSS class defines the style for the current page because the current page is *not* shown with a hyperlink. In this example, the current page is shown in bold.

The .GridPager a element defines the style for the other page numbers that are shown as hyperlinks.

Define the GridView

Define the GridView control in your ASP.NET page using the styles defined in the prior step.


<asp:GridView ID="CustomerGridView" runat="server"
    AllowPaging="true" PageSize="3"
    <HeaderStyle CssClass="GridHeader" />
    <RowStyle CssClass="GridRow" />
    <AlternatingRowStyle CssClass="GridAlternatingRow" />
    <PagerStyle CssClass="GridPager" />
        <asp:BoundField HeaderText="Last Name"
            DataField="LastName" SortExpression="LastName" />
        <asp:BoundField HeaderText="First Name"
            DataField="FirstName" SortExpression="FirstName" />
        <asp:BoundField HeaderText="Email" 
            DataField="EmailAddress" SortExpression="EmailAddress" />

Notice that the styles are set in the HeaderStyle, RowStyle, AlternatingRowStyle, and PagerStyle elements within the asp:GridView tag.

Set the GridView styles any time you want your GridView to match your ASP.NET application design.



  1.   Archie — March 23, 2011 @ 6:58 am    Reply


  2.   Kairii — August 13, 2011 @ 6:34 am    Reply

    Stay inrfomaivte, San Diego, yeah boy!

  3.   Jodi — August 14, 2011 @ 10:21 am    Reply

    Thank God! Soemone with brains speaks!

RSS feed for comments on this post. TrackBack URI

Leave a comment

© 2023 Deborah's Developer MindScape   Provided by WPMU DEV -The WordPress Experts   Hosted by Microsoft MVPs