Right-click on any row to show a context menu.



We've provided a special plugin that you can use to display a context menu upon click on a row grid with custom actions. In this example, you can right-click any row on the grid and perform any of the following - Add Row, Delete Row, Edit Row.

Please, review the View tab for implementation details.
using Microsoft.AspNetCore.Http;
using System;
using System.Collections.Generic;
using Trirand.Web.Core;

namespace CoreDemo.Models
{
    public class OrdersJqGridModel
    {
        public CoreGrid OrdersGrid { get; set; }

        public OrdersJqGridModel(HttpContext context)
        {
            OrdersGrid = new CoreGrid(context)
            {
                Columns = new List<CoreColumn>()
                {
                    new CoreColumn
                    {
                        // Always set PrimaryKey for Add,Edit,Delete operations
                        // If not set, the first column will be assumed as primary key
                        DataField = "OrderId",
                        PrimaryKey = true,
                        Editable = false,
                        Width = 50,
                        DataType = typeof(int)                        
                    },
                    new CoreColumn
                    {
                        DataField = "CustomerId",
                        Editable = true,
                        Width = 100,
                        DataType = typeof(string),

                    },
                    new CoreColumn
                    {
                        DataField = "OrderDate",
                        Editable = true,
                        Width = 100,
                        DataFormatString = "{0:yyyy/MM/dd}",
                        DataType = typeof(DateTime)
                    },
                    new CoreColumn
                    {
                        DataField = "Freight",
                        Editable = true,
                        Width = 75,
                        DataType = typeof(double)
                    },
                    new CoreColumn
                    {
                        DataField = "ShipName",
                        Editable =  true,
                        Width = 150,
                        DataType = typeof(string)
                    }
                },

                Width = "640",
                Height = "250",
                
                ToolBarSettings = new ToolBarSettings
                {
                    ShowRefreshButton = true
                }
            };  
        }        
    }
}
@model CoreDemo.Models.OrdersJqGridModel
@using Trirand.Web.Core
@using CoreDemo.Models;

<!DOCTYPE html>
<html lang="en-us">
<head>
    <meta charset="utf-8">
    <title>jqGrid for ASP.NET Core - Context Menu</title>
    <!-- jQuery runtime minified -->
    <script src="~/js/jquery-3.2.1.min.js" type="text/javascript"></script>
    <!-- The jqGrid localization file we need, English in this case -->
    <script type="text/javascript" src="~/js/trirand/i18n/grid.locale-en.js"></script>
    <!-- The jqGrid client-side javascript -->
    <script type="text/javascript" src="/js/trirand/coregrid.min.js"></script>
    <!-- The jQuery UI theme that will be used by the grid. -->
    <link rel="stylesheet" type="text/css" href="http://code.jquery.com/ui/1.12.1/themes/redmond/jquery-ui.css" />
    <!-- The jQuery UI theme extension jqGrid needs -->    
    <link rel="stylesheet" type="text/css" href="~/css/trirand/coregrid.css" />
    
    <!-- The 3rd party context menu component needed by the example -->
    <script type="text/javascript" src="~/js/trirand/jquery.jqGrid.ContextMenu.js"></script>
</head>

<body>
    
    <br />
    <span style="font-family:Tahoma">
        <b>Right-click</b> on any row to show a context menu.
    </span>
    <br />
    <br />

    <div>           
        @Html.Trirand().CoreGrid(Model.OrdersGrid, "ContextMenuGrid")
    </div>
    <br /><br />
    <div>
         @await Component.InvokeAsync("CodeTabs", new { product = "grid", example = "functionalitycontextmenu" })        
    </div> 
    
    <div class="contextMenu" id="myMenu1" style="width:350px;display:none">
        <ul style="width: 250px">
            <li id="add">
                <span class="ui-icon ui-icon-plus" style="float:left"></span>
                <span style="font-family:Tahoma">Add Row</span>
            </li>
            <li id="edit">
                <span class="ui-icon ui-icon-pencil" style="float:left"></span>
                <span style="font-family:Tahoma">Edit Row</span>
            </li>                
            <li id="del">
                <span class="ui-icon ui-icon-trash" style="float:left"></span>
                <span style="font-family:Tahoma; text-wrap: none">Delete Row</span>
            </li>                
        </ul>
    </div>
    
     <script type="text/javascript">
     
         function initGrid()
         {
            jQuery(".jqgrow", "#ContextMenuGrid").contextMenu('myMenu1', {
                    bindings: {
                        'edit': function(t) {
                            editRow();
                        },
                        'add': function(t) {
                            addRow();   
                        },
                        'del': function(t) {
                            delRow();
                        }
                    },
                    onContextMenu : function(event, menu)
                                 {
                                    var rowId = $(event.target).parent("tr").attr("id")
                                    var grid = $("#ContextMenuGrid");
                                    grid.setSelection(rowId);                                    
                                    
                                    return true;                                    
                                           
                                 }
                });             
           }    

           function addRow() {
               var grid = jQuery("#ContextMenuGrid");
               grid.editGridRow("new", grid.getGridParam("ddDialogOptions"));
           }

           function editRow() {
               var grid = jQuery("#ContextMenuGrid");
               var rowKey = grid.getGridParam("selrow");
               if (rowKey) {
                   grid.editGridRow(rowKey, grid.getGridParam("editDialogOptions"));
               }
               else {
                   alert("No rows are selected");
               }
           }

           function delRow() {
               var grid = jQuery("#ContextMenuGrid");
               var rowKey = grid.getGridParam("selrow");
               if (rowKey) {
                   grid.delGridRow(rowKey, grid.getGridParam("delDialogOptions"));
               }
               else {
                   alert("No rows are selected");
               }
           }
       </script>   
    
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.AspNetCore.Mvc;
using CoreDemo.Models;
using Trirand.Web.Core;
using Microsoft.AspNetCore.Mvc.Rendering;
using CoreDemo.Extensions;

namespace CoreDemo.Controllers.Grid
{
    public partial class GridController : Controller
    {
        // This is the default action for the View. Use it to setup your grid Model.
        public ActionResult FunctionalityContextMenu()
        {
            // Get the model (setup) of the grid defined in the /Models folder.
            var gridModel = new OrdersJqGridModel(HttpContext);
            // This method sets common properties for the grid, different than the default in the Model
            FunctionalityContextMenu_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 CoreGrid.DataUrl property
        public JsonResult FunctionalityContextMenu_DataRequested()
        {
            // Get both the grid Model
            // The data model in our case is an autogenerated linq2sql database based on Northwind.
            var gridModel = new OrdersJqGridModel(HttpContext);
            FunctionalityContextMenu_SetupGrid(gridModel.OrdersGrid);

            // return the result of the DataBind method, passing the datasource as a parameter
            // jqGrid for ASP.NET Core automatically takes care of paging, sorting, filtering/searching, etc
            return gridModel.OrdersGrid.DataBind(FunctionalityContextMenu_GetOrders().AsQueryable());
        }

        public void FunctionalityContextMenu_EditRow()
        {
            // Get the grid and database (northwind) models
            var gridModel = new OrdersJqGridModel(HttpContext);
            var northWindModel = new NorthWind();

            // Get the edit data using the CoreGrid GetEditData() method
            var editData = gridModel.OrdersGrid.GetEditData();

            // If we are in "Edit" mode
            if (gridModel.OrdersGrid.AjaxCallBackMode == AjaxCallBackMode.EditRow)
            {
                // Get the data from and find the Order corresponding to the edited row
                List<Order> gridData = FunctionalityContextMenu_GetOrders();
                Order order = gridData.Single<Order>(o => o.OrderId == Convert.ToInt16(editData.RowData["OrderId"]));

                // update the Order information
                order.OrderDate = Convert.ToDateTime(editData.RowData["OrderDate"]);
                order.CustomerId = editData.RowData["CustomerId"];
                order.Freight = Convert.ToDecimal(editData.RowData["Freight"]);
                order.ShipName = editData.RowData["ShipName"];

                // In this demo we do not need to update the database since we are using Session
                // However you will need to persist that to the database most probably in your scenario
                // *****************************
                // * Database update code here *
                // *****************************
            }
            if (gridModel.OrdersGrid.AjaxCallBackMode == AjaxCallBackMode.AddRow)
            {
                List<Order> gridData = FunctionalityContextMenu_GetOrders();

                // since we are adding a new Order, create a new istance
                Order order = new Order();
                // set the new Order information
                order.OrderId = gridData.Max<Order>(o => o.OrderId) + 1;
                order.OrderDate = Convert.ToDateTime(editData.RowData["OrderDate"]);
                order.CustomerId = editData.RowData["CustomerId"];
                order.Freight = Convert.ToDecimal(editData.RowData["Freight"]);
                order.ShipName = editData.RowData["ShipName"];

                // add the new order to the beginning of the list
                // In this demo we do not need to update the database since we are using Session
                // However you will need to persist that to the database most probably in your scenario
                gridData.Insert(0, order);
            }
            if (gridModel.OrdersGrid.AjaxCallBackMode == AjaxCallBackMode.DeleteRow)
            {
                List<Order> gridData = FunctionalityContextMenu_GetOrders();

                // locate the order that needs to be deleted
                Order order = gridData.Single<Order>(o => o.OrderId == Convert.ToInt16(editData.RowData["OrderId"]));

                // delete the record
                // In this demo we do not need to update the database since we are using Session
                // However you will need to persist that to the database most probably in your scenario
                gridData.Remove(order);
            }
        }

        public void FunctionalityContextMenu_SetupGrid(CoreGrid ordersGrid)
        {            
            // Setting the DataUrl to an action (method) in the controller is required.
            // This action will return the data needed by the grid
            ordersGrid.DataUrl = Url.Action("FunctionalityContextMenu_DataRequested");
            ordersGrid.EditUrl = Url.Action("FunctionalityContextMenu_EditRow");
            ordersGrid.ToolBarSettings.ShowEditButton = true;
            ordersGrid.ToolBarSettings.ShowAddButton = true;
            ordersGrid.ToolBarSettings.ShowDeleteButton = true;
            ordersGrid.EditDialogSettings.CloseAfterEditing = true;
            ordersGrid.AddDialogSettings.CloseAfterAdding = true;
            
            // hook the context menus here
            ordersGrid.ClientSideEvents.GridInitialized = "initGrid";

            // setup the dropdown values for the CustomerId editing dropdown
            FunctionalityContextMenu_SetUpCustomerIdEditDropDown(ordersGrid);
        }

        private void FunctionalityContextMenu_SetUpCustomerIdEditDropDown(CoreGrid ordersGrid)
        {
            // setup the grid search criteria for the columns
            CoreColumn 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 NorthWind();
                var editList = from customers in northWindModel.Customers
                               select new SelectListItem
                               {
                                   Text = customers.CustomerId,
                                   Value = customers.CustomerId
                               };

                customersColumn.EditList = editList.ToList<SelectListItem>();
            }
        }

        public List<Order> FunctionalityContextMenu_GetOrders()
        {
            List<Order> orders;

            if (!HttpContext.Session.Exists<List<Order>>("Orders"))
            {
                var northWindModel = new NorthWind();

                orders = (from order in northWindModel.Orders
                          select order).ToList<Order>();

                HttpContext.Session.Set<List<Order>>("Orders", orders);
            }
            else
            {
                orders = HttpContext.Session.Get<List<Order>>("Orders");
            }

            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/coregrid.css" />