Multi Column Header In Kendo Grid Using ASP.NET WEB API And Entity Framework

Introduction

This article tells about how to implement the multi column header 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.

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 REST API.
  5. Creating HTML page and implementing the multi column header 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 3

 
Figure 4

Creating model classes

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

Step 1

Right-click Models folder, select Add -> ADO.NET Entity Data Model or select Add->New Item. In 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 a Employee) and click Add.

Step 2

In 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 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.     {  
  4.         EmployeeEntities db = new EmployeeEntities();  
  5.         [HttpGet]  
  6.         [AllowAnonymous]  
  7.         [Route("EmployeeList")]  
  8.         public HttpResponseMessage GetEmployeeList()  
  9.         {  
  10.             try  
  11.             {   
  12.             return Request.CreateResponse(HttpStatusCode.OK, db.EmployeeLists, Configuration.Formatters.JsonFormatter);  
  13.             }  
  14.             catch(Exception ex)  
  15.             {  
  16.                 return Request.CreateResponse(HttpStatusCode.OK, ex.Message, Configuration.Formatters.JsonFormatter);  
  17.             }  
  18.   
  19.         }  
  20.     }   

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 API.

Creating 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.     <script src="Scripts/GridViewModel.js"></script>  
  14. </head>  
  15.   
  16. <body>  
  17.     <div id="example">  
  18.         <div id="grid"></div>  
  19.          
  20.     </div>  
  21. </body>  
  22.   
  23. </html>    
GridViewModel.js 
  1. $(document).ready(function () {  
  2.       
  3.     $("#grid").kendoGrid({  
  4.         dataSource: {  
  5.             type: "json",  
  6.             transport: {  
  7.                 read: "/api/Employee/EmployeeList"  
  8.             },  
  9.             schema: {  
  10.                 model: {  
  11.                     fields: {  
  12.                         EmployeeID: {  
  13.                             type: "number"  
  14.                         },  
  15.                         EmployeeName: {  
  16.                             type: "string"  
  17.                         },  
  18.                         Designation: {  
  19.                             type: "string"  
  20.                         },  
  21.                         Company: {  
  22.                             type: "string"  
  23.                         }  
  24.                     }  
  25.                 }  
  26.             },  
  27.         },  
  28.         filterable: true,  
  29.         sortable: true,  
  30.         pageable: true,  
  31.         columns: [{  
  32.             field: "EmployeeID",  
  33.             filterable: false  
  34.         }, {  
  35.             field: "EmployeeName",  
  36.             title: "EmployeeName",  
  37.             filterable: { multi: true, search: true }  
  38.         }, {  
  39.             title: "Other Details",  
  40.             columns: [{  
  41.                 field: "Designation",  
  42.                 title: "Designation"  
  43.             }, {  
  44.                 field: "Company",  
  45.                 title: "Company",  
  46.             }]  
  47.         }]  
  48.     });  
  49.   
  50. });   
We can observe that the Multi Column header is achieved simply by making the sub columns with the common title. From the code given above, it is obvious that we have created a title - other details with the column fields Designation and Company
 
Result in Browser
 

Figure 13
 
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