Server Intellect
 
Home   Asp.Net Tutorials   What's New   Newsletter   More Resources
Tutorial RSS
 
  Categories
Advanced Technologies
AJAX
Internet Browsers
Charts
Controls
Database
Email
Error Handling
File
Graphics
Website Navigation
Network
Performance
User Interface and Themes
Validation
Visual Web Developer
Web Services
XML
Suggest Tutorial


Navigator: Home - Tutorials - User Interface and Themes - How to Style a GridView with CSS in ASP.NET 4.0
How to Style a GridView with CSS in ASP.NET 4.0

ASP.Net 4.0 Tutorials V4
Server Intellect Cloud Hosting

ASP.NET User Interface and Themes Tutorial

This tutorial will demonstrate how to apply CSS styles to a GridView object in ASP.NET 4.0.

Adding the Database
To demonstrate how to apply styles to a gridview we will need to create a simple web site with a database to populate the gridview with data. Then, we will need to add in some CSS to style it. To begin, create a new ASP.NET Empty Web Site. Then, to add the database:
  1. Right click the project in your solution explorer.
  2. Select add ASP.NET folder.
  3. Select App_Data.
  4. Right click the App_Data folder in your solution explorer.
  5. Select add new item...
  6. Select a Sql Server Database.
  7. Click add.
Yes, it is possible to find a good web host. Sometimes it takes a while to find one you are comfortable with. After trying several, we went with Server Intellect and have been very happy thus far. They are by far the most professional, customer service friendly and technically knowledgeable host we've found so far.

Now that we have a database, we need to add a table to it with some sample data. To do this:
  1. Expand the Database.mdf folder in your server/database explorer.
  2. Right click the Tables folder.
  3. Select add new table.
  4. Add the following columns with their respective types to the table:
    Column Name Data Type 
     ID  int 
     Data  nvarchar(50)
  5. Save the table as 'Table1'.
Next, add the following sample data to Table1:
ID  Data 
 1  Temp data 1 
 2  Temp data 2
 3  Temp data 3
 4  Temp data 4

Adding the Default.aspx Page
Next, we need to add a web form with a gridview on it. To do this:
  1. Right click the project in your solution explorer.
  2. Select add new item...
  3. Select a web form.
  4. Name it 'Default.aspx'.
  5. Click add.
  6. Open Default.aspx up to design mode.
  7. Drag and drop a gridview control onto the web form.
  8. Expand the GridView Tasks menu.
  9. From the Choose Data Source dropdownlist select <New data source...>.
  10. Select a SQL Database and click OK.
  11. Use the dropdown list to select Database.mdf and click Next.
  12. Choose to save the connection string and click Next.
  13. Click Next leaving the default select statement.
  14. Finally, you can test your query and ensure that it is correct and click Finish.
Adding the CSS StyleSheet
Next, we need to add a new CSS style sheet to the project to which we will add our CSS classes. To do this:
  1. Right click the project in your solution explorer.
  2. Select add new item...
  3. Select a style sheet.
  4. Name it 'StyleSheet.css'.
  5. Click add.
We moved our web sites to Server Intellect and have found them to be incredibly professional. Their setup is very easy and we were up and running in no time.

Now we can add some CSS that will be applied to our gridview. To do this, open StyleSheet.css up for editing and add in the following:
/* the style for the table headers */
.gridview th
{
    font-sizelarge;
    font-weightbold;
    colorNavy;
    border1px solid black;
    padding8px;
}
/* the style for the normal table cells */
.gridview td
{
    padding8px;
    border1px solid black;
}

The gridview control generates a table in which it displays its data. In order to style the gridview, we must apply a class to it that effects the table cells that we actually want to style. We first start with the table header and change the font size, weight, and color in the 'th' tag. We also add in a simple border and some padding. Next, we style the 'td' tag by adding in some padding and a simple border.

Applying the Style
Next, we need to actually apply the style to the gridview that we added earlier. To do this:
  1. Open up Default.aspx to source mode.
  2. Drag and drop the StyleSheet.css file from your solution explorer into the head tag to create a reference to it.
  3. Set the CssClass property of the gridview to 'gridview'. 
If you're looking for a really good web host, try Server Intellect - we found the setup procedure and their control panel, very easy to adapt to and their IT team is awesome!

Test
To test this and ensure that your styles have been applied, load up the web site and ensure that your table generated by the gridview corresponds to the style you added.

The Default.aspx looks like this:
<head runat="server">
    <link href="StyleSheet.css" rel="stylesheet" type="text/css" />
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
            DataSourceID="SqlDataSource1" CssClass="gridview">
            <Columns>
                <asp:BoundField DataField="ID" HeaderText="ID" SortExpression="ID" />
                <asp:BoundField DataField="Data" HeaderText="Data" SortExpression="Data" />
            </Columns>
        </asp:GridView>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
            ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 
            SelectCommand="SELECT * FROM [Table1]"></asp:SqlDataSource>
    </div>
    </form>
</body>
Download Project Source - Enter your Email to be emailed a link to download the Full Source Project used in this Tutorial!



100% SPAM FREE! We will never sell or rent your email address!



 
  Developer Resources







Server Intellect Rocks