Inline editing with action icons is another editing mode of jqGrid. Just add a column anywhere you wish and set it EditActionIconsColumn property to true - this will enable automatic Edit/Save/Cancel buttons that will trigger the corresponding actions and events.
You can customize which icons to show (edit, delete) and whether to save on pressing Enter or just clicking the icons by custmozing the column EditActionIconsSettings collection.
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 OrdersJqGridModel
{
public JQGrid OrdersGrid { get; set; }
public OrdersJqGridModel()
{
OrdersGrid = new JQGrid
{
Columns = new List()
{
new JQGridColumn { DataField = "OrderID",
// always set PrimaryKey for Add,Edit,Delete operations
// if not set, the first column will be assumed as primary key
PrimaryKey = true,
Editable = false,
Width = 50 },
new JQGridColumn { DataField = "CustomerID",
Editable = true,
Width = 100 },
new JQGridColumn { DataField = "OrderDate",
Editable = true,
Width = 100,
DataFormatString = "{0:yyyy/MM/dd}" },
new JQGridColumn { DataField = "Freight",
Editable = true,
Width = 75 },
new JQGridColumn { DataField = "ShipName",
Editable = true
}
},
Width = Unit.Pixel(640),
Height = Unit.Percentage(100)
};
OrdersGrid.ToolBarSettings.ShowRefreshButton = true;
}
}
}
<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<JQGridMVCExamples.Models.OrdersJqGridModel>" %>
<%@ 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>Edit Row Inline with Action Icons Example</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>
<% Html.BeginForm("EditRowInlineActionIcons", "grid"); %>
<span style="font-size: 130%">
<%= Html.CheckBox("enter", (bool) ViewData["enterData"], new { onchange = "this.form.submit();" })%> save row on Enter
<%= Html.CheckBox("del", (bool) ViewData["delData"], new { onchange = "this.form.submit();" })%> show Delete button
<%= Html.CheckBox("edit", (bool) ViewData["editData"], new { onchange = "this.form.submit();" })%> show Edit button <br />
<br />
<br />
</span>
<% Html.EndForm(); %>
<div>
<%= Html.Trirand().JQGrid(Model.OrdersGrid, "EditRowInlineGrid") %>
</div>
<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 JQGridMVCExamples.Models;
using Trirand.Web.Mvc;
namespace JQGridMVCExamples.Controllers.Grid
{
public partial class GridController : Controller
{
// This is the default action for the View. Use it to setup your jqGrid Model.
public ActionResult EditRowInlineActionIcons(bool? enter, bool? del, bool? edit)
{
ViewData["enterData"] = enter ?? false;
ViewData["delData"] = del ?? true;
ViewData["editData"] = edit ?? true;
// Get the model (setup) of the grid defined in the /Models folder.
var gridModel = new OrdersJqGridModel();
// This method sets common properties for the grid, different than the default in the Model
EditRowInlineActionIcons_SetUpGrid(gridModel.OrdersGrid);
// 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. We are doing this in the EditRowInlineActionIcons_SetUpGrid method.
public JsonResult EditRowInlineActionIcons_DataRequested()
{
// Get both the grid and data models. For data model, make sure IQueryable is implemented (most linq-2-* cases)
// The data model in our case is an autogenerated linq2sql database based on Northwind.
var gridModel = new OrdersJqGridModel();
var dataModel = GetOrders().AsQueryable();
// This method sets common properties for the grid, different than the default in the Model
EditRowInlineActionIcons_SetUpGrid(gridModel.OrdersGrid);
// 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.OrdersGrid.DataBind(dataModel);
}
// The data gets passed to the controller as strongly typed objects of your data model, Order in our case
// This functionality is called Model Binders in ASP.NET MVC terms
public void EditRowInlineActionIcons_EditRow(Order editedOrder)
{
// Get the grid and database (northwind) models
var gridModel = new OrdersJqGridModel();
var northWindModel = new NorthwindDataContext();
// If we are in "Edit" mode, get the Order from database that matches the edited order
// Check for "Edit" mode this way, we can also be in "Delete" or "Add" mode as well in this method
if (gridModel.OrdersGrid.AjaxCallBackMode == AjaxCallBackMode.EditRow)
{
// Get the data from and find the Order corresponding to the edited row
List gridData = GetOrders();
Order orderToUpdate = gridData.Single(o => o.OrderID == editedOrder.OrderID);
// Update the Order information to match the edited Order data
// In this demo we do not need to update the database since we are using Session
// In your case you may need to actually hit the database
orderToUpdate.OrderDate = editedOrder.OrderDate;
orderToUpdate.CustomerID = editedOrder.CustomerID;
orderToUpdate.Freight = editedOrder.Freight;
orderToUpdate.ShipName = editedOrder.ShipName;
// This will save the changes into the database. We have commented this since this is just an online demo
// northWindModel.SubmitChanges();
}
}
public void EditRowInlineActionIcons_SetUpGrid(JQGrid ordersGrid)
{
ordersGrid.Columns.Insert(0, new JQGridColumn
{
EditActionIconsColumn = true,
EditActionIconsSettings = new EditActionIconsSettings
{
SaveOnEnterKeyPress = (bool?) ViewData["enterData"] ?? false,
ShowEditIcon = (bool?) ViewData["editData"] ?? true,
ShowDeleteIcon = (bool?) ViewData["delData"] ?? true
},
HeaderText = "Edit Actions",
Width = 50
});
ordersGrid.DataUrl = Url.Action("EditRowInlineActionIcons_DataRequested");
ordersGrid.EditUrl = Url.Action("EditRowInlineActionIcons_EditRow");
// setup the dropdown values for the CustomerID editing dropdown
EditRowInlineActionIcons_SetUpCustomerIDColumn(ordersGrid);
}
private void EditRowInlineActionIcons_SetUpCustomerIDColumn(JQGrid ordersGrid)
{
// setup the grid search criteria for the columns
JQGridColumn customersColumn = ordersGrid.Columns.Find(c => c.DataField == "CustomerID");
customersColumn.Editable = true;
customersColumn.EditType = EditType.DropDown;
// Populate the search dropdown only on initial request, in order to optimize performance
if (ordersGrid.AjaxCallBackMode == AjaxCallBackMode.RequestData)
{
var northWindModel = new NorthwindDataContext();
var editList = from customers in northWindModel.Customers
select new SelectListItem
{
Text = customers.CustomerID,
Value = customers.CustomerID
};
customersColumn.EditList = editList.ToList();
}
}
// This is a helper method fetching the data from Session
public List EditRowInlineActionIcons_GetOrders()
{
List orders;
if (Session["Orders"] == null)
{
var northWindModel = new NorthwindDataContext();
orders = (from order in northWindModel.Orders
select order).ToList();
Session["Orders"] = orders;
}
else
{
orders = Session["Orders"] as List;
}
return orders;
}
}
}
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" />