Example of Adaptive Layout in ASP.NET MVC Data Grid Control
This sample demonstrates optimal viewing experience and improve usability on small screens.
Properties
@Html.UiGrid(new UiGridOptions
{
Id = "adaptivebrowser",
Data = ViewData["dataSource"],
Height = "100%",
EnableAdaptiveUI = true,
AllowGrouping = false,
ShowColumnMenu = true,
RowRenderingMode = UiRowDirection.Vertical,
AllowPaging = true,
PageSize = 12,
PageCount = 3,
PageSizes = new[] { 12, 24, 48 },
AllowFiltering = true,
AllowSorting = true,
FilterType = UiGridFilterType.Excel,
GroupSettings = new UiGroupSettings { ShowGroupedColumn = true },
EditSettings = new UiEditSettings { AllowAdding = true, AllowEditing = true, AllowDeleting = true, Mode = UiEditMode.Dialog },
Toolbar = new List<string> { "Add", "Edit", "Delete", "Update", "Cancel", "Search", "ColumnChooser", "ExcelExport", "PdfExport" },
Aggregates = new List<UiGridAggregateRow>
{
new UiGridAggregateRow { Columns = new List<UiGridAggregateColumn> { new UiGridAggregateColumn { Field = "Freight", Format = "C2", Type = UiAggregateType.Sum, FooterTemplate = "Sum:${Sum}" } } }
},
Columns = new List<UiGridColumn>
{
new UiGridColumn { Field = "OrderID", HeaderText = "Order ID", Type = UiColumnType.Number, Width = 180, IsPrimaryKey = true, ValidationRules = new UiValidationRules { Required = true } },
new UiGridColumn { Field = "Freight", HeaderText = "Freight", Type = UiColumnType.Number, Width = 180, Format = "C2", EditType = UiEditType.NumericEdit, ValidationRules = new UiValidationRules { Required = true } },
new UiGridColumn { Field = "CustomerID", HeaderText = "Customer ID", Width = 180, ValidationRules = new UiValidationRules { Required = true, MinLength = 3 } },
new UiGridColumn { Field = "ShipCity", HeaderText = "Ship City", Width = 180 }
}
})
<style>
.e-mobile-layout { width: 400px; max-width: 100%; height: 640px; margin: 0 auto; border: 10px solid #333; border-radius: 28px; overflow: hidden; background: #fff; position: relative; }
.e-mobile-content { height: 100%; position: relative; }
</style>
<div class="e-adaptive-demo">
<div class="e-mobile-layout">
<div class="e-mobile-content">
@Html.UiGrid(new UiGridOptions
{
Id = "adaptivebrowser",
Data = ViewData["dataSource"],
Height = "100%",
EnableAdaptiveUI = true,
AllowGrouping = false,
ShowColumnMenu = true,
RowRenderingMode = UiRowDirection.Vertical,
AllowPaging = true,
PageSize = 12,
PageCount = 3,
PageSizes = new[] { 12, 24, 48 },
AllowFiltering = true,
AllowSorting = true,
FilterType = UiGridFilterType.Excel,
GroupSettings = new UiGroupSettings { ShowGroupedColumn = true },
EditSettings = new UiEditSettings { AllowAdding = true, AllowEditing = true, AllowDeleting = true, Mode = UiEditMode.Dialog },
Toolbar = new List<string> { "Add", "Edit", "Delete", "Update", "Cancel", "Search", "ColumnChooser", "ExcelExport", "PdfExport" },
Aggregates = new List<UiGridAggregateRow>
{
new UiGridAggregateRow { Columns = new List<UiGridAggregateColumn> { new UiGridAggregateColumn { Field = "Freight", Format = "C2", Type = UiAggregateType.Sum, FooterTemplate = "Sum:${Sum}" } } }
},
Columns = new List<UiGridColumn>
{
new UiGridColumn { Field = "OrderID", HeaderText = "Order ID", Type = UiColumnType.Number, Width = 180, IsPrimaryKey = true, ValidationRules = new UiValidationRules { Required = true } },
new UiGridColumn { Field = "Freight", HeaderText = "Freight", Type = UiColumnType.Number, Width = 180, Format = "C2", EditType = UiEditType.NumericEdit, ValidationRules = new UiValidationRules { Required = true } },
new UiGridColumn { Field = "CustomerID", HeaderText = "Customer ID", Width = 180, ValidationRules = new UiValidationRules { Required = true, MinLength = 3 } },
new UiGridColumn { Field = "ShipCity", HeaderText = "Ship City", Width = 180 }
}
})
</div>
</div>
</div>
<div class="demo-property-panel">
<h3>Properties</h3>
<div class="demo-property-row"><label for="adaptivecheck">Enable horizontal row mode</label><input type="checkbox" id="adaptivecheck" /></div>
</div>
<script>
// Syncfusion: grid.rowRenderingMode = Horizontal (table rows, and grouping is allowed) or Vertical (a card per row).
document.getElementById('adaptivecheck').addEventListener('change', function (e) {
window.Lumen.get('adaptivebrowser').setSettings({ rowRenderingMode: e.target.checked ? 'horizontal' : 'vertical', grouping: e.target.checked });
});
</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/AdaptiveLayout.cshtml; it draws what ViewData holds.
public ActionResult AdaptiveLayout()
{
var order = OrdersDetails.GetAllRecords();
ViewData["dataSource"] = order;
return View();
}
/// <summary>One order: the rows of Syncfusion's Grid samples that use the orders.</summary>
public class OrdersDetails
{
public OrdersDetails()
{
}
public OrdersDetails(int OrderID, string CustomerId, int EmployeeId, double Freight, bool Verified, DateTime OrderDate, string ShipCity, string ShipName, string ShipCountry, DateTime ShippedDate, string ShipAddress)
{
this.OrderID = OrderID;
this.CustomerID = CustomerId;
this.EmployeeID = EmployeeId;
this.Freight = Freight;
this.ShipCity = ShipCity;
this.Verified = Verified;
this.OrderDate = OrderDate;
this.ShipName = ShipName;
this.ShipCountry = ShipCountry;
this.ShippedDate = ShippedDate;
this.ShipAddress = ShipAddress;
}
public int OrderID { get; set; }
public string CustomerID { get; set; }
public int EmployeeID { get; set; }
public double Freight { get; set; }
public string ShipCity { get; set; }
public bool Verified { get; set; }
public DateTime OrderDate { get; set; }
public string ShipName { get; set; }
public string ShipCountry { get; set; }
public DateTime ShippedDate { get; set; }
public string ShipAddress { get; set; }
public static List<OrdersDetails> GetAllRecords()
{
List<OrdersDetails> order = new List<OrdersDetails>();
int code = 10000;
for (int i = 1; i < 10; i++)
{
order.Add(new OrdersDetails(code + 1, "ALFKI", i + 0, 2.3 * i, false, new DateTime(1991, 05, 15, 10, 40, 00), "Berlin", "Simons bistro", "Denmark", new DateTime(1996, 7, 16, 09, 30, 00), "Kirchgasse 6"));
order.Add(new OrdersDetails(code + 2, "ANATR", i + 2, 3.3 * i, true, new DateTime(1990, 04, 04, 09, 30, 00), "Madrid", "Queen Cozinha", "Brazil", new DateTime(1996, 9, 11, 03, 45, 00), "Avda. Azteca 123"));
order.Add(new OrdersDetails(code + 3, "ANTON", i + 1, 4.3 * i, true, new DateTime(1957, 11, 30, 03, 45, 00), "Cholchester", "Frankenversand", "Germany", new DateTime(1996, 10, 7, 07, 50, 00), "Carrera 52 con Ave. Bolívar #65-98 Llano Largo"));
order.Add(new OrdersDetails(code + 4, "BLONP", i + 3, 5.3 * i, false, new DateTime(1930, 10, 22, 07, 30, 00), "Marseille", "Ernst Handel", "Austria", new DateTime(1996, 12, 30, 06, 12, 00), "Magazinweg 7"));
order.Add(new OrdersDetails(code + 5, "BOLID", i + 4, 6.3 * i, true, new DateTime(1953, 02, 18, 05, 50, 00), "Tsawassen", "Hanari Carnes", "Switzerland", new DateTime(1997, 12, 3, 12, 50, 00), "1029 - 12th Ave. S."));
code += 5;
}
return order;
}
}
}
}
@* 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. *@
The enableAdaptiveUI property is used to render the grid filter, sort, edit, pager and toolbars like column chooser, pdf export, excel export, etc... dialogs adaptively and rowRenderingMode property is used to render the grid row elements in the following directions,
Horizontal- Renders the grid row elements in the horizontal direction.Vertical- Renders the grid row elements in the vertical direction.
In this sample, you can change the row elements rendering direction by using the properties panel checkbox
In this demo, the column menu feature is only supported for the Grid rowRenderingMode mode as Vertical. This feature includes grouping, sorting, autofit, filter, and column chooser feature.