Grouping In Kendo Grid Using ASP.NET WEB API And Entity Framework

Introduction

This article tells about how to perform a grouping in Kendo Grid, using ASP.NET Web API. To explain it, I have created a RESTful GET Service, which is used to load the DataSource of Kendo Grid

Requirements

  • VS 2010 and above
  • SQL Server 2008 and above

Prerequisites

Basic knowledge of ASP.NET WebAPI, jQuery, Kendo UI.

This article flows, as per the following.

  1. Set up the table
  2. Creating an ASP.NET Web API Application.
  3. Creating a Controller.
  4. Testing the REST API.
  5. Creating a HTML page and implementing the grouping in kendo Grid

Set up the table

For this article, I have created one table named EmployeeList, the design of which is shown below.

Figure 1

Employee List Table

 
Figure 2 

Creating an ASP.NET WEB API Application

Create a Web API Application, using an installed Web template in Visual Studio, as shown below. In my case, I named the Application “KGridTemplate"

  
 Figure

 
Figure 4 

Creating model classes

Now, we will create Entity Framework models from the database tables.

Step 1

Right-click the Models folder, select Add -> ADO.NET Entity Data Model or select Add->New Item. In the "Add New Item" Window, select data in the left pane and ADO.NET Entity Data Model from the center pane. Name the new model file (In my case, I made it as Employee) and click Add.

Step 2

In the Entity Data Model wizard, select "EF Designer" from the database and click "Next".

Figure 5

Step 3 

Click New Connection button. The Connection Properties Window will open. 

Figure 6

Step 4

In Connection Properties Window, provide the name of the local Server, where the database was created (in this case, (DESKTOP-585QGBN)). After providing the Server name, select "Employee" from the available databases and click OK.


Figure 7 

Step 5

You can use the default name for the connection to save the Web.Config file. Now, click Next.

 
Figure 8 

Step 6

Select the table to generate the models for EmployeeList table and click Finish.


Figure 9

My database schema is shown in the figure given below.

Figure 10

Creating a Controller

Right click on Controller folder and add a new Web API 2 controller- Empty, as shown in the Figure 11. In my case, I named it as EmployeesController.cs.


Figure 11

Write the code given below in EmployeeController.cs

EmployeeController.cs
  1. [RoutePrefix("api/Employee")]  
  2. public class EmployeeController: ApiController {  
  3.     EmployeeEntities db = new EmployeeEntities();  
  4.     [HttpGet]  
  5.     [AllowAnonymous]  
  6.     [Route("EmployeeList")]  
  7.     public HttpResponseMessage GetEmployeeList() {  
  8.         try {  
  9.             return Request.CreateResponse(HttpStatusCode.OK, db.EmployeeLists, Configuration.Formatters.JsonFormatter);  
  10.         } catch (Exception ex) {  
  11.             return Request.CreateResponse(HttpStatusCode.OK, ex.Message, Configuration.Formatters.JsonFormatter);  
  12.         }  
  13.     }  
  14. }  

Testing the API in Postman

  • API End Point /API/ Employee/ EmployeeList.
  • Type GET.

 

Figure 12 

Now, our API is ready, let's create a Kendo Grid DataSource, using the API.

Creating a HTML page

Create one new HTML page in the Application, where we are going to implement Kendo Grid, using the RESTful Service. In my case, I named it as KendoGrid.html.

Remote DataSource in KendoGrid 

Click here to learn more about remote DataSource in Kendo Grid

KendoGrid.html 
  1. <!DOCTYPE html>  
  2. <html>  
  3.   
  4. <head>  
  5.     <title>Kendo Grid</title>  
  6.     <meta charset="utf-8" />  
  7.     <link rel="stylesheet" href="http://kendo.cdn.telerik.com/2017.1.118/styles/kendo.common.min.css" />  
  8.     <link rel="stylesheet" href="http://kendo.cdn.telerik.com/2017.1.118/styles/kendo.rtl.min.css" />  
  9.     <link rel="stylesheet" href="http://kendo.cdn.telerik.com/2017.1.118/styles/kendo.silver.min.css" />  
  10.     <link rel="stylesheet" href="http://kendo.cdn.telerik.com/2017.1.118/styles/kendo.mobile.all.min.css" />  
  11.     <script src="http://code.jquery.com/jquery-1.12.4.min.js"></script>  
  12.     <script src="http://kendo.cdn.telerik.com/2017.1.118/js/kendo.all.min.js"></script>  
  13. </head>  
  14.   
  15. <body>  
  16.     <div id="example">  
  17.         <div id="grid"></div>  
  18.         <script>  
  19.             $(document).ready(function() {  
  20.                 $("#grid").kendoGrid({  
  21.                     dataSource: {  
  22.                         type: "json",  
  23.                         transport: {  
  24.                             read: "/api/Employee/EmployeeList"  
  25.                         },  
  26.                         schema: {  
  27.                             model: {  
  28.                                 fields: {  
  29.                                     EmployeeID: {  
  30.                                         type: "number"  
  31.                                     },  
  32.                                     FirstName: {  
  33.                                         type: "string"  
  34.                                     },  
  35.                                     LastName: {  
  36.                                         type: "string"  
  37.                                     },  
  38.                                     Company: {  
  39.                                         type: "string"  
  40.                                     }  
  41.                                 }  
  42.                             }  
  43.                         },  
  44.                     },  
  45.                     filterable: true,  
  46.                     sortable: true,  
  47.                     pageable: true,  
  48.                     columns: [{  
  49.                         field: "EmployeeID",  
  50.                         filterable: false  
  51.                     }, {  
  52.                         field: "FirstName",  
  53.                         title: " First Name",  
  54.                     }, {  
  55.                         field: "LastName",  
  56.                         title: "Last Name"  
  57.                     }, {  
  58.                         field: "Company",  
  59.                         title: "Company",  
  60.                     }]  
  61.                 });  
  62.             });  
  63.         </script>  
  64.     </div>  
  65. </body>  
  66.   
  67. </html>  
Result in the Browser

 
Figure 13

Grouping in Kendo Grid

Lets group the record in Grid, which is based on company

KendoGrid.html
  1. <!DOCTYPE html>  
  2. <html>  
  3.   
  4. <head>  
  5.     <title>Kendo Grid</title>  
  6.     <meta charset="utf-8" />  
  7.     <link rel="stylesheet" href="http://kendo.cdn.telerik.com/2017.1.118/styles/kendo.common.min.css" />  
  8.     <link rel="stylesheet" href="http://kendo.cdn.telerik.com/2017.1.118/styles/kendo.rtl.min.css" />  
  9.     <link rel="stylesheet" href="http://kendo.cdn.telerik.com/2017.1.118/styles/kendo.silver.min.css" />  
  10.     <link rel="stylesheet" href="http://kendo.cdn.telerik.com/2017.1.118/styles/kendo.mobile.all.min.css" />  
  11.     <script src="http://code.jquery.com/jquery-1.12.4.min.js"></script>  
  12.     <script src="http://kendo.cdn.telerik.com/2017.1.118/js/kendo.all.min.js"></script>  
  13. </head>  
  14.   
  15. <body>  
  16.     <div id="example">  
  17.         <div id="grid"></div>  
  18.         <script>  
  19.             $(document).ready(function() {  
  20.                 $("#grid").kendoGrid({  
  21.                     dataSource: {  
  22.                         type: "json",  
  23.                         transport: {  
  24.                             read: "/api/Employee/EmployeeList"  
  25.                         },  
  26.                         group: {  
  27.                             field: "Company",  
  28.                             aggregates: [{  
  29.                                 field: "Company",  
  30.                                 aggregate: "count"  
  31.                             }]  
  32.                         },  
  33.                         schema: {  
  34.                             model: {  
  35.                                 fields: {  
  36.                                     EmployeeID: {  
  37.                                         type: "number"  
  38.                                     },  
  39.                                     FirstName: {  
  40.                                         type: "string"  
  41.                                     },  
  42.                                     LastName: {  
  43.                                         type: "string"  
  44.                                     },  
  45.                                     Company: {  
  46.                                         type: "string"  
  47.                                     }  
  48.                                 }  
  49.                             }  
  50.                         },  
  51.                     },  
  52.                     filterable: true,  
  53.                     sortable: true,  
  54.                     pageable: true,  
  55.                     columns: [{  
  56.                         field: "EmployeeID",  
  57.                         filterable: false  
  58.                     }, {  
  59.                         field: "FirstName",  
  60.                         title: " First Name",  
  61.                     }, {  
  62.                         field: "LastName",  
  63.                         title: "Last Name"  
  64.                     }, {  
  65.                         field: "Company",  
  66.                         title: "Company",  
  67.                         groupHeaderTemplate: "Company: #= value # Total: #= count #"  
  68.                     }]  
  69.                 });  
  70.             });  
  71.         </script>  
  72.     </div>  
  73. </body>  
  74.   
  75. </html> 
The group field in DataSource is used to group the record, which is based on the company field with the aggregate - count. The groupHeaderTemplate in the column is used to display the title in each group

Result in the Browser


Figure 14


Figure 15

I hope, you have enjoyed this article. Your valuable feedback, questions or comments about this article are always welcome.

Up Next
    Ebook Download
    View all
    Learn
    View all