All grid data
Filtered grid data
Filtered and paged data



Exporting to CSV is supported via the ExportToCSV(string fileName) method. Click on the button to see how it works. There are various settings related to exporting available off the ExportSettings collection.
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

<!DOCTYPE html>
<html lang="en-us">
<head>
    <meta charset="utf-8">
    <title>jqGrid for ASP.NET Core - Export to CSV format</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>    
    <!-- Referece to the javascript client-side code of the jqDatePicker -->
    <script type="text/javascript" src="~/js/trirand/coredatepicker.min.js"></script>    
    <!-- Referece to the javascript client-side code of jqAutoComplete  -->
    <script type="text/javascript" src="~/js/trirand/coreautocomplete.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" />   
    
    <style>
        /*the size of the datepicker items*/
        #ui-datepicker-div { font-size: 12px; } 
        /*the size of the autocomplete seach menu for Shipname*/
        .ui-menu-item { font-size: 12px; text-wrap: none } 
	    /*the image that will be shown in the autocomplete input box while performing the search*/
        .ui-autocomplete-loading 
        {
		    background: white url("/content/images/loading.gif") right center no-repeat;
	    }
	</style> 
</head> 
<body>

<div>           
        @Html.Trirand().CoreGrid(Model.OrdersGrid, "Grid")
        
        <!-- this CoreDatePicker will be used internally by the grid as DatePicker control for searching and editing -->
        <!-- Datepickers can be used standalone too - when you set the DisplayMode to Standalone (it's the default) -->
        @(Html.Trirand().CoreDatePicker(
            new CoreDatePicker 
                { 
                    DisplayMode = DatePickerDisplayMode.ControlEditor,
                    ShowOn = ShowOn.Focus,                    
                    DateFormat = "yyyy/mm/dd",                                        
                    MinDate = new DateTime(1995,1,1),
                    MaxDate = new DateTime(2020,12,21)
                }, "DatePicker")
)
        
        <!-- this CoreAutoComplete control will be used internally by the grid as DatePicker control for searching and editing -->
        <!-- AutoCompleters b can be used standalone too - when you set the DisplayMode to Standalone (it's the default) -->
        @(Html.Trirand().CoreAutoComplete(
            new CoreAutoComplete 
                {
                    DisplayMode = AutoCompleteDisplayMode.ControlEditor,
                    DataUrl = Url.Action("ExcelExport_AutoCompleteShipName","grid")
                }, "AutoComplete")
)
    </div>
    
     <br />
        @{ Html.BeginForm("ExportToCSV", "Grid"); }
            <div style="font-family: Tahoma">
                @Html.RadioButton("exportType", "1", true) All grid data<br />
                @Html.RadioButton("exportType", "2") Filtered grid data<br />
                @Html.RadioButton("exportType", "3") Filtered and paged data<br />
            </div>
            <br />
            <input type="button" 
                    id="Button1" 
                    value="Export to CSV" onclick="this.form.submit();" /> 
        @{ Html.EndForm(); }
            
    <br /><br />
    <div>
         @await Component.InvokeAsync("CodeTabs", new { product = "grid", example = "exportcsv" })       
    </div>    
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.AspNetCore.Mvc;
using CoreDemo.Models;
using Trirand.Web.Core;
using CoreDemo.Extensions;
using Microsoft.AspNetCore.Mvc.Rendering;

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 ExportCSV()
        {
            // Get the model (setup) of the grid defined in the /Models folder.
            var gridModel = new OrdersJqGridModel(HttpContext);
            var ordersGrid = gridModel.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("CSVGrid_DataRequested");

            // customize the default Orders grid model with custom settings
            // NOTE: you need to call this method in the action that fetches the data as well,
            // so that the models match
            SetCSVExportGrid(ordersGrid);

            // Pass the custmomized grid model to the View
            return View(gridModel);
        }

        // This method is called when the grid requests data        
        public JsonResult CSVGrid_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 OrdersJqGridModel(HttpContext);
            var northWindModel = new NorthWind();

            // customize the default Orders grid model with our custom settings
            SetCSVExportGrid(gridModel.OrdersGrid);

            // Save the current grid state in Session
            // We will later need it for CSV Export
            
            CoreGridState gridState = gridModel.OrdersGrid.GetState();
            HttpContext.Session.Set<CoreGridState>("gridState", gridState);

            // 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(northWindModel.Orders);
        }

        public string CSVExport_AutoCompleteShipName(string term)
        {
            var northWindModel = new NorthWind();
            CoreAutoComplete autoComplete = new CoreAutoComplete();

            autoComplete.DataField = "ShipName";
            autoComplete.AutoCompleteMode = AutoCompleteMode.BeginsWith;
            autoComplete.DataSource = from o in northWindModel.Orders
                                      select o;
            return autoComplete.DataBind(HttpContext);
        }

        private void SetCSVExportGrid(CoreGrid ordersGrid)
        {
            // show the search toolbar
            ordersGrid.ToolBarSettings.ShowSearchToolBar = true;
            ordersGrid.ToolBarSettings.ShowSearchButton = true;

            var orderDateColumn = ordersGrid.Columns.Find(c => c.DataField == "OrderDate");
            orderDateColumn.SearchOptions.Add(SearchOperation.IsGreaterOrEqualTo);

            orderDateColumn.DataFormatString = "{0:yyyy/MM/dd}";
            orderDateColumn.SearchType = SearchType.DatePicker;
            orderDateColumn.DataType = typeof(DateTime);
            orderDateColumn.SearchControlID = "DatePicker";
            orderDateColumn.SearchToolBarOperation = SearchOperation.IsEqualTo;

            var shipNameColumn = ordersGrid.Columns.Find(c => c.DataField == "ShipName");
            shipNameColumn.SearchType = SearchType.AutoComplete;
            shipNameColumn.DataType = typeof(string);
            shipNameColumn.SearchControlID = "AutoComplete";
            shipNameColumn.SearchToolBarOperation = SearchOperation.Contains;

            var OrderIdColumns = ordersGrid.Columns.Find(c => c.DataField == "OrderId");
            OrderIdColumns.Searchable = true;
            OrderIdColumns.DataType = typeof(int);
            OrderIdColumns.SearchToolBarOperation = SearchOperation.IsEqualTo;

            SetCSVCustomerIdSearchDropDown(ordersGrid);
            SetCSVFreightSearchDropDown(ordersGrid);
        }

        private void SetCSVCustomerIdSearchDropDown(CoreGrid ordersGrid)
        {
            // setup the grid search criteria for the columns
            CoreColumn customersColumn = ordersGrid.Columns.Find(c => c.DataField == "CustomerId");
            customersColumn.Searchable = true;

            // DataType must be set in order to use searching
            customersColumn.DataType = typeof(string);
            customersColumn.SearchToolBarOperation = SearchOperation.IsEqualTo;
            customersColumn.SearchType = SearchType.DropDown;

            // Populate the search dropdown only on initial request, in order to optimize performance
            if (ordersGrid.AjaxCallBackMode == AjaxCallBackMode.RequestData)
            {
                var northWindModel = new NorthWind();
                var searchList = from customers in northWindModel.Customers
                                 select new SelectListItem
                                 {
                                     Text = customers.CustomerId,
                                     Value = customers.CustomerId
                                 };

                customersColumn.SearchList = searchList.ToList<SelectListItem>();
                customersColumn.SearchList.Insert(0, new SelectListItem { Text = "All", Value = "" });
            }
        }

        private void SetCSVFreightSearchDropDown(CoreGrid ordersGrid)
        {
            // setup the grid search criteria for the columns
            CoreColumn freightColumn = ordersGrid.Columns.Find(c => c.DataField == "Freight");
            freightColumn.Searchable = true;

            // DataType must be set in order to use searching
            freightColumn.DataType = typeof(decimal);
            freightColumn.SearchToolBarOperation = SearchOperation.IsGreaterOrEqualTo;
            freightColumn.SearchType = SearchType.DropDown;

            // Populate the search dropdown only on initial request, in order to optimize performance
            if (ordersGrid.AjaxCallBackMode == AjaxCallBackMode.RequestData)
            {
                List<SelectListItem> searchList = new List<SelectListItem>();
                searchList.Add(new SelectListItem { Text = "> 10", Value = "10" });
                searchList.Add(new SelectListItem { Text = "> 30", Value = "30" });
                searchList.Add(new SelectListItem { Text = "> 50", Value = "50" });
                searchList.Add(new SelectListItem { Text = "> 100", Value = "100" });

                freightColumn.SearchList = searchList.ToList<SelectListItem>();
                freightColumn.SearchList.Insert(0, new SelectListItem { Text = "All", Value = "" });
            }
        }

        public FileStreamResult ExportToCSV(string exportType)
        {
            var gridModel = new OrdersJqGridModel(HttpContext);
            var northWindModel = new NorthWind();
            var grid = gridModel.OrdersGrid;

            // Get the last grid state the we saved before in Session in the DataRequested action
            CoreGridState gridState = HttpContext.Session.Get<CoreGridState>("gridState");

            // Need to set grid options again
            SetExportGrid(grid);

            if (String.IsNullOrEmpty(exportType))
                exportType = "1";

            switch (exportType)
            {
                case "1":
                    return File(
                        grid.ExportToCSV(northWindModel.Orders),
                        "text/csv",
                        "grid.csv");
                case "2":
                    grid.ExportSettings.ExportDataRange = ExportDataRange.Filtered;
                    return File(
                         grid.ExportToCSV(northWindModel.Orders, gridState),
                         "text/csv",
                         "grid.csv");
                default:
                    grid.ExportSettings.ExportDataRange = ExportDataRange.FilteredAndPaged;

                    return File(
                         grid.ExportToCSV(northWindModel.Orders, gridState),
                         "text/csv",
                         "grid.csv");
            }
        }
    }
}
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" />