In this article I will explain with an example, how to make the WebGrid Scrollable i.e. Freezed or Fixed WebGrid Header Row and add a vertical Scrollbar for scrolling the Data rows in ASP.Net MVC Razor.
The WebGrid Header will be freezed (fixed) and Data rows will be made Scrollable using the ASPSnippets Scrollable Table jQuery Plugin.
 
 
Scrollable Table jQuery Plugin
This article makes use of the Scrollable Table jQuery Plugin which can be downloaded using the link below.
 
 
Database
Here I am making use of Microsoft’s Northwind Database. The download and install instructions are provided in the following article.
 
 
Entity Framework Model
Once the Entity Framework is configured and connected to the database table, the Model will look as shown below.
Note: For beginners in ASP.Net MVC and Entity Framework, please refer my article ASP.Net MVC: Simple Entity Framework Tutorial with example. It covers all the information needed for connecting and configuring Entity Framework.
 
MVC Scrollable WebGrid: Freeze (Fixed) Header of WebGrid in ASP.Net MVC
 
 
Controller
The Entity Framework is now configured and hence now we can create a Controller and write code to fetch the records from the Customers Table of the Northwind Database.
Inside the Index Action method, the Customer records are fetched using Entity Framework and returned to the View.
public class HomeController : Controller
{
    // GET: Home
    public ActionResult Index()
    {
        NorthwindEntities entities = new NorthwindEntities();
        return View(entities.Customers.ToList());
    }
}
 
 
View
Inside the View, in the very first line the Customer Entity is declared as IEnumerable which specifies that the Model will be available as a Collection.
For displaying the records, the WebGrid is rendered using GetHtml function which renders the WebGrid using Model.
Note: For more details on using WebGrid, please refer WebGrid Step By Step Tutorial with example in ASP.Net MVC.
 
Freezing WebGrid Header
Inside the jQuery document ready event handler, the Scrollable Table jQuery Plugin is applied to the WebGrid.
Note: For more information on the Scrollable Table Plugin, please refer my article Make Scrollable Table in HTML with Fixed Header using jQuery.
 
@model IEnumerable<WebGrid_Freeze_Header_MVC.Customer>
 
@{
    Layout = null;
    WebGrid webGrid = new WebGrid(source: Model, canPage: false, canSort: false);
}
 
<!DOCTYPE html>
 
<html>
<head>
    <meta name="viewport" content="width=device-width"/>
    <title>Index</title>
    <style type="text/css">
        body
        {
            font-family: Arial;
            font-size: 10pt;
        }
        .Grid
        {
            border: 1px solid #ccc;
            border-collapse: collapse;
        }
        .Grid th
        {
            background-color: #F7F7F7;
            font-weight: bold;
        }
        .Grid th, .Grid td
        {
            padding: 5px;
            border: 1px solid #ccc;
        }
        .Grid, .Grid table td
        {
            border: 0px solid #ccc;
        }
        .Grid th a, .Grid th a:visited
        {
            color: #333;
        }
    </style>
</head>
<body>
    @webGrid.GetHtml(
        htmlAttributes: new { @id = "WebGrid", @class = "Grid" },
        columns: webGrid.Columns(
                 webGrid.Column("CustomerID", "Customer Id"),
                 webGrid.Column("ContactName", "Customer Name"),
                 webGrid.Column("City", "City"),
                 webGrid.Column("Country", "Country")))
 
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
    <script type="text/javascript" src="~/Scripts/ScrollableTablePlugin_1.0_min.js"></script>
    <script type="text/javascript">
    $(function () {
        $('#WebGrid').Scrollable({
            ScrollHeight: 300
        });
    });
    </script>
</body>
</html>
 
 
Screenshot
MVC Scrollable WebGrid: Freeze (Fixed) Header of WebGrid in ASP.Net MVC
 
 
Browser Compatibility

The above code has been tested in the following browsers.

Internet Explorer  FireFox  Chrome  Safari  Opera 

* All browser logos displayed above are property of their respective owners.

 
 
Downloads