Formatting GridView control with CSS

Using style elements to format the GridView is easy but can result in lower performance. Instead, we can use Cascading Style Sheets to format the GridView control. The following example demonstrates how we can format GridView control using Cascading Style Sheets.

<%@ Page Language="C#" %>
<head runat="server">
<title>Untitled Page</title>
<style type="text/css">

    background-color: #FF6600;
    color: #FFFFFF;
    font-size: 8pt;
    font-weight: bold;

    background-color: #FFFFEE;
    color: #000000;
    font-size: 8pt;

    background-color: #FFFFFF;
    color: #000000;
    font-size: 8pt;
<form id="form1" runat="server">
<asp:gridview id="ItemsGridView" datasourceid="myDataSource"  
AutoGenerateColumns="false"  DataKeyNames="ItemID" runat="server">
	<asp:BoundField DataField="ItemID" HeaderText="Item ID" ReadOnly="true"  
    ItemStyle-Width="100px" ItemStyle-CssClass="itemStyle"/>
	<asp:BoundField DataField="ItemName" HeaderText="Item Name" ReadOnly="true"  			
    ItemStyle-Width="100px"/><asp:BoundField DataField="ClStk" 
    HeaderText="Item closingStock" 	ReadOnly="true"  ItemStyle-Width="100px"/>
<AlternatingRowStyle CssClass="alternateItemStyle" />
<HeaderStyle CssClass="headerStyle" />
<asp:SqlDataSource ID="MyDataSource"  
SelectCommand="SELECT ItemID,ItemName,ItemType,ClStk FROM ItemTable" 

In the above example, the header has been modified using the class .headerStyle. The alternate rows of the GridView control have been modified using the class .alternateItemStyle and the background color has been modified using the class .itemStyle.

screenshot of gridview which is customized using the code given above