ASP.NET: Styling a GridView
Tue, Feb 9 2010 16:20
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 -----------------------------------*/
background-color:#f2f2f2; /* Light gray */
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"
<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" />
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.