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:

image

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

image

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.

In CSS:

/*———– Grid ———————————–*/
.GridHeader
{
    color:#C16914;           
    font-weight:bold;
}

.GridHeader a
{
    color:#C16914;           
    font-weight:bold;
}

.GridHeader a:active
{
    color:Black;           
    font-weight:bold;
}

.GridRow
{
    color:#C16914;
}

.GridAlternatingRow
{
    background-color:#f2f2f2;    /* Light gray */
    color:#C16914;                           
}

.GridPager
{
    color:#C16914;               
    font-weight:bold;
}

.GridPager a
{
    color:#C16914;               
    font-weight:normal;
}

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.

In HTML:

<asp:GridView ID="CustomerGridView" runat="server"
    AllowPaging="true" PageSize="3"
    AllowSorting="true"
    AutoGenerateColumns="false">
    <HeaderStyle CssClass="GridHeader" />
    <RowStyle CssClass="GridRow" />
    <AlternatingRowStyle CssClass="GridAlternatingRow" />
    <PagerStyle CssClass="GridPager" />
    <Columns>
        <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" />
    </Columns>
</asp:GridView>

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.

Enjoy!

3 Comments

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

    nice

  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

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