You can freeze the first N number of columns in a grid - so that when the user scrolls the grid horizontally, the first N columns stay frozen. Just set the Frozen property of the respective columns to true and this is all you need. In order to see a horizontal scrollbar, you need to set Width of the grid to a smaller number than the combined Width properties of all columns in the grid.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Trirand.Web.Mvc;
using System.Web.UI.WebControls;
namespace JQGridMVCExamples.Models
{
public class CustomersJqGridModel
{
public JQGrid CustomersGrid { get; set; }
public CustomersJqGridModel()
{
CustomersGrid = new JQGrid
{
Columns = new List()
{
new JQGridColumn { DataField = "CustomerID",
PrimaryKey = true,
Frozen = true,
Width = 60 },
new JQGridColumn { DataField = "CompanyName",
Width = 150,
Frozen = true},
new JQGridColumn { DataField = "ContactName", Width = 150 },
new JQGridColumn { DataField = "ContactTitle", Width = 150 },
new JQGridColumn { DataField = "Address", Width = 150 },
new JQGridColumn { DataField = "ContactName", Width = 150 },
new JQGridColumn { DataField = "City", Width = 150 },
new JQGridColumn { DataField = "Region", Width = 150 },
new JQGridColumn { DataField = "PostalCode", Width = 150 },
new JQGridColumn { DataField = "Country", Width = 150 },
new JQGridColumn { DataField = "Phone", Width = 150 },
new JQGridColumn { DataField = "Fax", Width = 150 }
},
Width = Unit.Pixel(650),
Height = Unit.Percentage(100)
};
}
}
}
<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<JQGridMVCExamples.Models.CustomersJqGridModel>" %>
<%@ Import Namespace="Trirand.Web.Mvc" %>
<%@ Import Namespace="JQGridMVCExamples.Models" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>jqSuite for ASP.NET MVC - jqGrid Column Freezing</title>
<script type="text/javascript" src="http://ajax.microsoft.com/ajax/jquery/jquery-1.7.1.min.js"></script>
<script type="text/javascript" src="http://www.trirand.net/aspnetmvc/Scripts/trirand/i18n/grid.locale-en.js"></script>
<script type="text/javascript" src="http://www.trirand.net/aspnetmvc/Scripts/trirand/jquery.jqGrid.min.js"></script>
<link rel="stylesheet" type="text/css" href="http://ajax.aspnetcdn.com/ajax/jquery.ui/1.8.16/themes/redmond/jquery-ui.css" />
<link rel="stylesheet" type="text/css" href="http://www.trirand.net/aspnetmvc/Content/themes/ui.jqgrid.css" />
</head>
<body>
<div>
<%= Html.Trirand().JQGrid(Model.CustomersGrid, "FrozenGrid") %>
</div>
<br /><br />
<% Html.BeginForm("FunctionalityColumnsFreezing", "Grid"); %>
<div style="font-size: 130%; font-family: Tahoma">
<%= Html.RadioButton("columns", "1", ViewData["first"] as bool?) %> Freeze first column <br />
<%= Html.RadioButton("columns", "2", ViewData["second"] as bool?) %> Freeze first two columns<br />
<%= Html.RadioButton("columns", "3", ViewData["third"] as bool?) %> Freeze first three columns<br />
</div>
<br />
<input type="button"
id="Button1"
value="Freeze columns" onclick="this.form.submit();" />
<% Html.EndForm(); %>
<br /><br />
<div>
<% Html.RenderPartial("CodeTabs"); %>
</div>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using JQGridMVCExamples.Models;
using System.Web.UI.WebControls;
namespace JQGridMVCExamples.Controllers.Grid
{
public partial class GridController : Controller
{
// This is the default action for the View. Use it to setup your grid Model.
public ActionResult FunctionalityColumnsFreezing(int? columns)
{
// Get the model (setup) of the grid defined in the /Models folder.
var gridModel = new CustomersJqGridModel();
var grid = gridModel.CustomersGrid;
if (columns == null)
columns = 2;
if (columns == 1)
{
grid.Columns[0].Frozen = true;
grid.Columns[1].Frozen = false;
grid.Columns[2].Frozen = false;
ViewData["first"] = true;
}
if (columns == 2)
{
grid.Columns[0].Frozen = true;
grid.Columns[1].Frozen = true;
grid.Columns[2].Frozen = false;
ViewData["second"] = true;
}
if (columns == 3)
{
grid.Columns[0].Frozen = true;
grid.Columns[1].Frozen = true;
grid.Columns[2].Frozen = true;
ViewData["third"] = true;
}
// Setting the DataUrl to an action (method) in the controller is required.
// This action will return the data needed by the grid
grid.DataUrl = Url.Action("FunctionalityColumnsFreezing_DataRequested");
// Pass the custmomized grid model to the View
return View(gridModel);
}
// This method is called when the grid requests data. You can choose any method to call
// by setting the JQGrid.DataUrl property
public JsonResult FunctionalityColumnsFreezing_DataRequested()
{
// Get both the grid Model and the data Model
// The data model in our case is an autogenerated linq2sql database based on Northwind.
var gridModel = new CustomersJqGridModel();
var northWindModel = new NorthwindDataContext();
// return the result of the DataBind method, passing the datasource as a parameter
// jqGrid for ASP.NET MVC automatically takes care of paging, sorting, filtering/searching, etc
return gridModel.CustomersGrid.DataBind(northWindModel.Customers);
}
}
}
Switch theme:
Theming is based on the very popular jQuery
ThemeRoller standard. This is the same theming mechanism used by jQuery UI and is now a de-facto standard for jQuery based components.
The benefits of using ThemeRoller are huge. We can offer a big set of ready to use themes created by professional designers, including Windows-like themes (Redmond), Apple-like theme (Cupertino), etc.
In addition to that any jQuery UI controls on the same page will pick the same theme.
Last, but not least, you can always roll your own ThemeRoller theme, using the superb
Theme Editor
To use a theme, simply reference 2 Css files in your Html <head> section - the ThemeRoller theme you wish to use, and the jqGrid own ThemeRoller Css file. For example (Redmond theme):
<link rel="stylesheet" type="text/css" media="screen" href="/themes/redmond/jquery-ui-1.8.2.custom.css" />
<link rel="stylesheet" type="text/css" media="screen" href="/themes/ui.jqgrid.css" />