Example of Reorder in ASP.NET MVC Data Grid Control
This sample demonstrates reordering of the Grid columns. Select column name and index from properties panel to reorder the columns. You can also reorder columns by simply drag and drop in the desired column position.
Properties
@Html.UiGrid(new UiGridOptions
{
Id = "FlatGrid",
Data = ViewData["dataSource"],
AllowReordering = true,
OnActionComplete = "actionComplete",
AllowSorting = true,
AllowFiltering = true,
FilterType = UiGridFilterType.Excel,
EditSettings = new UiEditSettings { AllowAdding = true, AllowEditing = true, AllowDeleting = true },
Toolbar = new List<string> { "Add", "Edit", "Delete", "Update", "Cancel" },
Columns = new List<UiGridColumn>
{
new UiGridColumn { Field = "EmployeeID", HeaderText = "Employee ID", Type = UiColumnType.Number, Width = 150, TextAlign = UiTextAlign.Right, IsPrimaryKey = true, ValidationRules = new UiValidationRules { Required = true } },
new UiGridColumn { Field = "FirstName", HeaderText = "Name", Width = 125, ValidationRules = new UiValidationRules { Required = true, MinLength = 5 } },
new UiGridColumn { Field = "Title", HeaderText = "Title", Width = 190 },
new UiGridColumn { Field = "HireDate", HeaderText = "Hire Date", Type = UiColumnType.Date, Width = 135, TextAlign = UiTextAlign.Right, Format = "M/d/yyyy", EditType = UiEditType.DatePickerEdit }
}
})
@Html.UiGrid(new UiGridOptions
{
Id = "FlatGrid",
Data = ViewData["dataSource"],
AllowReordering = true,
OnActionComplete = "actionComplete",
AllowSorting = true,
AllowFiltering = true,
FilterType = UiGridFilterType.Excel,
EditSettings = new UiEditSettings { AllowAdding = true, AllowEditing = true, AllowDeleting = true },
Toolbar = new List<string> { "Add", "Edit", "Delete", "Update", "Cancel" },
Columns = new List<UiGridColumn>
{
new UiGridColumn { Field = "EmployeeID", HeaderText = "Employee ID", Type = UiColumnType.Number, Width = 150, TextAlign = UiTextAlign.Right, IsPrimaryKey = true, ValidationRules = new UiValidationRules { Required = true } },
new UiGridColumn { Field = "FirstName", HeaderText = "Name", Width = 125, ValidationRules = new UiValidationRules { Required = true, MinLength = 5 } },
new UiGridColumn { Field = "Title", HeaderText = "Title", Width = 190 },
new UiGridColumn { Field = "HireDate", HeaderText = "Hire Date", Type = UiColumnType.Date, Width = 135, TextAlign = UiTextAlign.Right, Format = "M/d/yyyy", EditType = UiEditType.DatePickerEdit }
}
})
<div class="demo-property-panel">
<h3>Properties</h3>
<div class="demo-property-row"><label for="column">Column</label><select id="column">@foreach (var option in (List<KeyValuePair<string, string>>)ViewData["columns"]) { <option value="@option.Value">@option.Key</option> }</select></div>
<div class="demo-property-row"><label for="columnIndex">Column Index</label><select id="columnIndex">@foreach (var option in (List<KeyValuePair<string, string>>)ViewData["index"]) { <option value="@option.Value">@option.Key</option> }</select></div>
</div>
<script>
var reorderGrid = function () { return window.Lumen.get('FlatGrid'); };
// Syncfusion: after a reorder the index box shows where the chosen column is now.
function actionComplete(args) {
if (args.requestType === 'reorder') {
document.getElementById('columnIndex').value = String(reorderGrid().getColumnIndex(document.getElementById('column').value));
}
}
document.getElementById('column').addEventListener('change', function (e) {
document.getElementById('columnIndex').value = String(reorderGrid().getColumnIndex(e.target.value));
});
// Syncfusion: grid.reorderColumns(field, grid.columns[index].field).
document.getElementById('columnIndex').addEventListener('change', function (e) {
var target = reorderGrid().getColumns()[parseInt(e.target.value, 10)];
if (target) { reorderGrid().reorderColumns(document.getElementById('column').value, target.field); }
});
</script>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using Lumen.UI.Mvc;
namespace YourApp.Controllers
{
public class GridController : Controller
{
// The page is Views/Grid/ColumnReorder.cshtml; it draws what ViewData holds.
public ActionResult ColumnReorder()
{
var DataSource = EmployeeView.GetAllRecords();
ViewData["dataSource"] = DataSource;
ViewData["columns"] = new List<KeyValuePair<string, string>>
{
new KeyValuePair<string, string>("Employee ID", "EmployeeID"),
new KeyValuePair<string, string>("Name", "FirstName"),
new KeyValuePair<string, string>("Title", "Title"),
new KeyValuePair<string, string>("Hire Date", "HireDate")
};
ViewData["index"] = new List<KeyValuePair<string, string>>
{
new KeyValuePair<string, string>("1", "0"),
new KeyValuePair<string, string>("2", "1"),
new KeyValuePair<string, string>("3", "2"),
new KeyValuePair<string, string>("4", "3")
};
return View();
}
public class EmployeeView
{
public EmployeeView()
{
}
public EmployeeView(int EmployeeID, string FirstName, string LastName, string Title, DateTime BirthDate, DateTime HireDate, int ReportsTo, string Address, string PostalCode, string Phone, string City, string Country)
{
this.EmployeeID = EmployeeID;
this.FirstName = FirstName;
this.LastName = LastName;
this.Title = Title;
this.BirthDate = BirthDate;
this.HireDate = HireDate;
this.ReportsTo = ReportsTo;
this.Address = Address;
this.PostalCode = PostalCode;
this.Phone = Phone;
this.City = City;
this.Country = Country;
}
public int EmployeeID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Title { get; set; }
public DateTime BirthDate { get; set; }
public DateTime HireDate { get; set; }
public int ReportsTo { get; set; }
public string Address { get; set; }
public string PostalCode { get; set; }
public string Phone { get; set; }
public string City { get; set; }
public string Country { get; set; }
public static List<EmployeeView> GetAllRecords()
{
List<EmployeeView> Emp = new List<EmployeeView>();
Emp.Add(new EmployeeView(1, "Nancy", "Davolio", "Sales Representative", new DateTime(1948, 12, 08), new DateTime(1992, 05, 01), 2, "507 - 20th Ave. E.Apt. 2A ", " 98122", "(206) 555-9857 ", "Seattle ", "USA"));
Emp.Add(new EmployeeView(2, "Andrew", "Fuller", "Vice President, Sales", new DateTime(1952, 02, 19), new DateTime(1992, 08, 14), 4, "908 W. Capital Way", "98401 ", "(206) 555-9482 ", "Kirkland ", "USA"));
Emp.Add(new EmployeeView(3, "Janet", "Leverling", "Sales Representative", new DateTime(1963, 08, 30), new DateTime(1992, 04, 01), 3, " 4110 Old Redmond Rd.", "98052 ", "(206) 555-8122", "Redmond ", "USA "));
Emp.Add(new EmployeeView(4, "Margaret", "Peacock", "Sales Representative", new DateTime(1937, 09, 19), new DateTime(1993, 05, 03), 6, "14 Garrett Hill ", "SW1 8JR ", "(71) 555-4848 ", "London ", "UK "));
Emp.Add(new EmployeeView(5, "Steven", "Buchanan", "Sales Manager", new DateTime(1955, 03, 04), new DateTime(1993, 10, 17), 8, "Coventry HouseMiner Rd. ", "EC2 7JR ", " (206) 555-8122", "Tacoma ", " USA"));
Emp.Add(new EmployeeView(6, "Michael", "Suyama", "Sales Representative", new DateTime(1963, 07, 02), new DateTime(1993, 10, 17), 2, " 7 Houndstooth Rd.", " WG2 7LT", "(71) 555-4444 ", "London ", "UK "));
Emp.Add(new EmployeeView(7, "Robert", "King", "Sales Representative", new DateTime(1960, 05, 29), new DateTime(1994, 01, 02), 7, "Edgeham HollowWinchester Way ", "RG1 9SP ", "(71) 555-5598 ", "London ", " UK"));
Emp.Add(new EmployeeView(8, "Laura", "Callahan", "Inside Sales Coordinator", new DateTime(1958, 01, 09), new DateTime(1994, 03, 05), 9, "722 Moss Bay Blvd. ", "98033 ", " (206) 555-3412", "Seattle ", "USA "));
Emp.Add(new EmployeeView(9, "Anne", "Dodsworth", "Sales Representative", new DateTime(1966, 01, 27), new DateTime(1994, 11, 15), 5, "4726 - 11th Ave. N.E. ", "98105 ", "(71) 555-5598 ", " London", "UK "));
return Emp;
}
}
}
}
@* Install once: Install-Package Lumen.UI.Mvc (or: dotnet add package Lumen.UI.Mvc) *@
@* Views/Web.config: <add namespace="Lumen.UI.Mvc" /> *@
@* Views/Shared/_Layout.cshtml *@
<head>
@Html.UiStyles()
<link rel="stylesheet" href="~/Content/site.css" />
</head>
<body>
@RenderBody()
@Html.UiScripts()
@RenderSection("scripts", required: false)
</body>
@* Grids with a DataUrl: @Html.AntiForgeryToken() on the page and [HttpPost, ValidateJsonAntiForgeryToken] on the action. *@
Reordering can be enabled by setting AllowReordering property as true. Reordering can be done by drag and drop the column header from one index to another index within the Grid.
The location in which the column to be placed, will be indicated by two arrows symbols.
In this demo, you can either reorder columns by drag and drop or by selecting column name and column index to reorder from the dropdown.