Setting the default grouping in Kendo Grid

Abi P picture Abi P · Jun 24, 2014 · Viewed 12.9k times · Source

I have a requirement to group a grid by default on a particular column and to not allow the user to remove the grouping on that column. Is this possible?

Answer

numaroth picture numaroth · Dec 19, 2014

You can set an initial grouping when you create the grid. You can keep the users from being able to remove or change the grouping by setting the groupable property to false or simply not including it in the configuration.

Both of the examples below group the grid based on FirstName.

Razor HTML Example:

@(Html.Kendo().Grid(Model.Person)
    .Name("grid")
    .Columns(columns =>
    {
      columns.Bound(model => model.FirstName);
      columns.Bound(item => item.LastName);
    })
    .Groupable(g => g.Enabled(false))
    .DataSource(dataSource => dataSource
        .Server()
        .Group(groups => groups.Add(p => p.FirstName))
)

JavaScript Example:

$("#grid").kendoGrid({
    dataSource: {
        data: [{FirstName: "FirstName1", LastName: "LastName1"},
              {FirstName: "FirstName1", LastName: "LastName2"},
              {FirstName: "FirstName3", LastName: "LastName3"},
              {FirstName: "FirstName1", LastName: "LastName4"}],
        group: { field: "FirstName" } // set grouping for the dataSource
    },
    groupable: false, // this will remove the group bar
    sortable: true,
    columns: ["FirstName","LastName"]
});

Link to a fiddle for the JavaScript example.

Source of the JavaScript example