Grouping in ASP.Net Core Grid Component
15 Feb 202410 minutes to read
The Grid has options to group records by dragging and dropping the column header to the group drop area. When grouping is applied, grid records are organized into a hierarchical structure to facilitate easier expansion and collapse of records.
To enable grouping in the grid, set the allowGrouping
as true. Grouping options can be configured through the e-grid-groupsettings
tag helper.
<ejs-grid id="Grid" dataSource="@ViewBag.DataSource" allowGrouping="true" >
<e-grid-columns>
<e-grid-column field="OrderID" headerText="Order ID" textAlign="Right" width="120"></e-grid-column>
<e-grid-column field="CustomerID" headerText="Customer ID" width="150"></e-grid-column>
<e-grid-column field="ShipCity" headerText="Ship City" width="150"></e-grid-column>
<e-grid-column field="ShipName" headerText="Ship Name" width="150"></e-grid-column>
</e-grid-columns>
</ejs-grid>
public IActionResult Index()
{
var Order = OrderDetails.GetAllRecords();
ViewBag.DataSource = Order;
return View();
}
NOTE
- You can group and ungroup columns by using the groupColumn and ungroupColumn methods.
* To disable grouping for a particular column, set theallowGrouping
to false in e-grid-column tag helper.
Initial group
To apply group at initial rendering, set the column field name in the columns
property of e-grid-groupsettings tag helper.
<ejs-grid id="Grid" dataSource="@ViewBag.DataSource" allowGrouping="true" >
<e-grid-groupsettings columns="@(new string[] {"OrderDate"})"></e-grid-groupsettings>
<e-grid-columns>
<e-grid-column field="OrderID" headerText="Order ID" textAlign="Right" width="120"></e-grid-column>
<e-grid-column field="CustomerID" headerText="Customer ID" width="150"></e-grid-column>
<e-grid-column field="OrderDate" headerText="Order Date" format="yMd" width="150"></e-grid-column>
<e-grid-column field="Freight" headerText="Freight" format="C2" width="150"></e-grid-column>
</e-grid-columns>
</ejs-grid>
public IActionResult Index()
{
var Order = OrderDetails.GetAllRecords();
ViewBag.DataSource = Order;
return View();
}
Hide drop area
To avoid ungrouping or further grouping of a column after initial column grouping, define the showDropArea
of e-grid-groupsettings as false.
<ejs-grid id="Grid" dataSource="@ViewBag.DataSource" allowGrouping="true">
<e-grid-groupsettings showDropArea="false" columns="@(new string[] {"Freight"})"></e-grid-groupsettings>
<e-grid-columns>
<e-grid-column field="OrderID" headerText="Order ID" textAlign="Right" width="120"></e-grid-column>
<e-grid-column field="CustomerID" headerText="Customer ID" width="150"></e-grid-column>
<e-grid-column field="OrderDate" headerText="Order Date" format="yMd" width="150"></e-grid-column>
<e-grid-column field="Freight" headerText="Freight" format="C2" width="150"></e-grid-column>
</e-grid-columns>
</ejs-grid>
public IActionResult Index()
{
var Order = OrderDetails.GetAllRecords();
ViewBag.DataSource = Order;
return View();
}
Group with paging
On grouping columns with paging feature, the aggregated information and total items are displayed based on the current page. The grid does not consider aggregated information and total items from other pages. To get additional details (aggregated information and total items) from other pages, set the disablePageWiseAggregates
property of e-grid-groupsettings to false.
NOTE
If remote data is bound to grid dataSource, two requests will be sent when performing grouping action; one for getting the grouped data and another for getting aggregate details and total items count.
Group by format
By default, a column will be grouped by the data or value present for the particular row. To group the numeric or datetime column based on the mentioned format, you have to enable the enableGroupByFormat
property of the corresponding grid columns.
<ejs-grid id="Grid" dataSource="@ViewBag.DataSource" allowGrouping="true" >
<e-grid-groupsettings showDropArea="false" columns="@(new string[] { "OrderDate","Freight"})"></e-grid-groupsettings>
<e-grid-columns>
<e-grid-column field="OrderID" headerText="Order ID" textAlign="Right" width="120"></e-grid-column>
<e-grid-column field="CustomerID" headerText="Customer ID" width="150"></e-grid-column>
<e-grid-column field="OrderDate" headerText="Order Date" format="yMd" enableGroupByFormat="true" width="150"></e-grid-column>
<e-grid-column field="Freight" headerText="Freight" format="C2" enableGroupByFormat="true" width="150"></e-grid-column>
</e-grid-columns>
</ejs-grid>
public IActionResult Index()
{
var Order = OrderDetails.GetAllRecords();
ViewBag.DataSource = Order;
return View();
}
Grouping events
During the group action, the grid component triggers two events. The actionBegin
event triggers before the group action starts and the actionComplete
event triggers after the group action is completed. Using these events you can perform any action.
<ejs-grid id="Grid" dataSource="@ViewBag.DataSource" allowGrouping="true" actionBegin="actionHandler" actionComplete="actionHandler">
<e-grid-columns>
<e-grid-column field="OrderID" headerText="Order ID" textAlign="Right" width="120"></e-grid-column>
<e-grid-column field="CustomerID" headerText="Customer ID" width="150"></e-grid-column>
<e-grid-column field="OrderDate" headerText="Order Date" format="yMd" width="150"></e-grid-column>
<e-grid-column field="Freight" headerText="Freight" format="C2" width="150"></e-grid-column>
</e-grid-columns>
</ejs-grid>
<script>
function actionHandler(args) {
alert(args.requestType + ' ' + args.type); //Custom Action
}
</script>
public IActionResult Index()
{
var Order = OrderDetails.GetAllRecords();
ViewBag.DataSource = Order;
return View();
}
NOTE
The
args.requestType
is based on the current action name. For example, when grouping, theargs.requestType
value will be ‘grouping’.
Collapse by external button
You can collapse the selected group from an external button by invoking the expandCollapseRows method.
<ejs-button id="collapse" content="Collapse" isPrimary="true"></ejs-button>
<ejs-grid id="Grid" dataSource="@ViewBag.DataSource" allowGrouping="true" >
<e-grid-groupsettings columns="@(new string[] {"Freight"})"></e-grid-groupsettings>
<e-grid-columns>
<e-grid-column field="OrderID" headerText="Order ID" textAlign="Right" width="120"></e-grid-column>
<e-grid-column field="CustomerID" headerText="Customer ID" width="150"></e-grid-column>
<e-grid-column field="OrderDate" headerText="Order Date" format="yMd" width="150"></e-grid-column>
<e-grid-column field="Freight" headerText="Freight" format="C2" width="150"></e-grid-column>
</e-grid-columns>
</ejs-grid>
<script>
document.getElementById('collapse').addEventListener('click', () => {
var grid = document.getElementById('Grid').ej2_instances[0];
if (grid.getSelectedRowIndexes().length) {
var currentTr = grid.getRows()[grid.getSelectedRowIndexes()[0]];
while (currentTr.classList && currentTr.classList.length) {
currentTr = currentTr.previousSibling;
}
let collapseElement = currentTr.querySelector('.e-recordplusexpand');
grid.groupModule.expandCollapseRows(collapseElement); //Pass the collapse row element.
}
});
</script>
public IActionResult Index()
{
var Order = OrderDetails.GetAllRecords();
ViewBag.DataSource = Order;
return View();
}
Sort grouped columns in descending order during initial grouping
By default, grouped columns are sorted in ascending order. To sort grouped columns in descending order during initial grouping, you can set the field
and direction
in the columns property of e-grid-sortsettings
tag helper.
The CustomerID
column will be sorted in descending order when the grid is initially grouped, as shown in the following example.
@{
List<object> cols = new List<object>();
cols.Add(new { field = "CustomerID", direction = "Descending" });
}
<ejs-grid id="Grid" dataSource="@ViewBag.DataSource" allowGrouping="true" allowSorting="true">
<e-grid-sortsettings columns="cols"></e-grid-sortsettings>
<e-grid-groupsettings columns="@(new string[] {"CustomerID"})"></e-grid-groupsettings>
<e-grid-columns>
<e-grid-column field="OrderID" headerText="Order ID" textAlign="Right" width="120"></e-grid-column>
<e-grid-column field="CustomerID" headerText="Customer ID" width="150"></e-grid-column>
<e-grid-column field="ShipCity" headerText="Ship City" width="150"></e-grid-column>
<e-grid-column field="ShipName" headerText="Ship Name" width="150"></e-grid-column>
</e-grid-columns>
</ejs-grid>
public IActionResult Index()
{
var Order = OrderDetails.GetAllRecords();
ViewBag.DataSource = Order;
return View();
}
Limitations
- Grouping is not compatible with the following features:
- Autofill