Change Kendo Grid DataSource Based On Selection In DropDownList

Introduction

In this article, I’ll describe how to change the Kendo Grid DataSource, based on the selected item in Kendo DropDownlist, using ASP.NET MVC and Entity framework. The Application is developed, using Entity framework "Database First" approach and ASP.NET MVC.

Prerequisites

Basic knowledge of ASP.NET MVC, Entity framework and Kendo UI framework.

Creating a Table

For the demo purpose, I am going to create two tables in MS SQL database.

The script, given below is used to create the tables with some records.

  1. CREATE TABLE EmployeeA(  
  2.  EmployeeId INT IDENTITY(1,1) CONSTRAINT Pk_Emp_A PRIMARY KEY ,  
  3.  FirstName VARCHAR(20),  
  4.  LastName VARCHAR(20),  
  5.  )  
  6.  GO   
  7.  INSERT INTO EmployeeA VALUES('Ganesh','Karthick')  
  8.  INSERT INTO EmployeeA VALUES('Jai''Gugan')  
  9.  GO  
  10.  CREATE TABLE EmployeeB(  
  11.  EmployeeId INT IDENTITY(1,1) CONSTRAINT Pk_Emp_B PRIMARY KEY ,  
  12.  FirstName VARCHAR(20),  
  13.  LastName VARCHAR(20),  
  14.  )  
  15.  GO   
  16.  INSERT INTO EmployeeB VALUES('Pradeep','Raj')  
  17.  INSERT INTO EmployeeB VALUES('Arun''Karthick')  

Create New MVC Project

Create a new empty ASP.NET MVC Application, as per the figures, given below.

Open Visual Studio -> File -> New Project -> ASP.NET Web Application.
 
 

Select MVC and click OK.



Note -  Please refer to my previous 
article to learn how to configure Kendo UI in ASP.NET MVC Application.

Generate the Model

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 EmployeeDetails, and click Add.

Step 2

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

Step 3

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



Step 4

In the "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.

Step 5

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

Step 6

Select the table to generate the models for EmployeeA and EmployeeB tables and click Finish.

My database schema is shown in the figure, given below-

 

Create a Controller

Create a new empty controller. Right-click the Controllers folder and select Add –> New Empty Controller. In my case, I named it as GridDataSourceController.cs.

Write the code, given below, in the Controller.

  1. public class GridDataSourceController : Controller  
  2.    {  
  3.       private EmployeeEntities db = new EmployeeEntities();  
  4.         public ActionResult CompanyList()  
  5.         {  
  6.             return View();  
  7.         }  
  8.         public ActionResult GetAllEmployeeA([DataSourceRequest]DataSourceRequest request)  
  9.         {  
  10.             try  
  11.             {  
  12.   
  13.                 var employee = db.EmployeeAs.ToList();  
  14.   
  15.                 return Json(employee.ToDataSourceResult(request), JsonRequestBehavior.AllowGet);  
  16.             }  
  17.             catch (Exception ex)  
  18.             {  
  19.                 return Json(ex.Message);  
  20.             }  
  21.   
  22.         }  
  23.         public ActionResult GetAllEmployeeB([DataSourceRequest]DataSourceRequest request)  
  24.         {  
  25.             try  
  26.             {  
  27.   
  28.                 var employee = db.EmployeeBs.ToList();  
  29.   
  30.                 return Json(employee.ToDataSourceResult(request), JsonRequestBehavior.AllowGet);  
  31.             }  
  32.             catch (Exception ex)  
  33.             {  
  34.                 return Json(ex.Message);  
  35.             }  
  36.   
  37.         }  
  38.     }  

From the Controller, shown above, it is obvious that GetAllEmployeeA and GetAllEmployeeB actions will give a complete employees list of company A and company B, respectively. 

Creating a View

Before going through the View, please check out my previous article Kendo UI Grid with ASP.NET MVC, where I have explained how to implement Kendo Grid, using ASP.NET MVC and Entity framework.

Right click on View-> GridDataSource folder and add new empty View. Write the given code in View.

  1. @model KendoMvcApp.Models.EmployeeA  
  2. @{  
  3.     ViewBag.Title = "CompanyList";  
  4.     Layout = "~/Views/Shared/_Layout.cshtml";  
  5. }  
  6.   
  7. <br />  
  8. <br />  
  9. <div class="container">  
  10.     <div class="row">  
  11.         <div class="col-sm-3">  
  12.             <h4> List of Companies </h4>  
  13.             @(Html.Kendo().DropDownList()  
  14.           .Name("Company")  
  15.           .DataTextField("Text")  
  16.           .DataValueField("Value")  
  17.           .Events(e=>e.Change("onCompanyChange"))  
  18.           .BindTo(new List<KendoMvcApp.Models.CompanyList>() {  
  19.               new KendoMvcApp.Models.CompanyList() {  
  20.                   Text = "Company A",  
  21.                   Value = "1"  
  22.               },  
  23.               new KendoMvcApp.Models.CompanyList() {  
  24.                   Text = "Company B",  
  25.                   Value = "2"  
  26.               }  
  27.                 
  28.           })  
  29.           .Value("1")  
  30.           .HtmlAttributes(new { style = "width: 100%" })  
  31.             )  
  32.         </div>  
  33.         <div class="col-sm-9">  
  34.             <h4 id="EmpTitle"> Employees of Company A </h4>  
  35.   
  36.             @(Html.Kendo().Grid<KendoMvcApp.Models.EmployeeA>()  
  37.     .Name("EmpGrid")  
  38.     .Selectable()  
  39.     .Columns(columns =>  
  40.     {  
  41.           
  42.         columns.Bound(c => c.FirstName);  
  43.         columns.Bound(c => c.LastName);  
  44.   
  45.     })  
  46.     .DataSource(dataSource => dataSource  
  47.         .Ajax()  
  48.         .Read(read => read.Action("GetAllEmployeeA""GridDataSource"))  
  49.         )  
  50.             )  
  51.         </div>  
  52.     </div>  
  53. </div>  
  54.   
  55. <script type="text/javascript">  
  56.     function onCompanyChange()  
  57.     {  
  58.         var Company = $("#Company").val();  
  59.         var grid = $("#EmpGrid").data("kendoGrid");  
  60.         if(Company=='2')  
  61.         {  
  62.             $("#EmpTitle").text('Employees of Company B ')   
  63.         grid.dataSource.transport.options.read.url = "/GridDataSource/GetAllEmployeeB"  
  64.         grid.dataSource.read()  
  65.         }  
  66.         else  
  67.         {  
  68.             $("#EmpTitle").text('Employees of Company A ')  
  69.             grid.dataSource.transport.options.read.url = "/GridDataSource/GetAllEmployeeA"  
  70.             grid.dataSource.read()  
  71.         }  
  72.           
  73.     }  
  74.     var EmployeeBDataSource = new kendo.data.DataSource({  
  75.         schema: {  
  76.             model: {  
  77.                 id: "EmployeeId",  
  78.                 fields: {  
  79.                       
  80.                     FirstName: { type: "string" },  
  81.                     LastName: { type: "string" }  
  82.                 }  
  83.             }  
  84.         },  
  85.         batch: true,  
  86.         transport: {  
  87.             read: {  
  88.                 url: "/GridDataSource/GetAllEmployeeB",  
  89.                 dataType: "json"  
  90.             },  
  91.   
  92.              
  93.         }  
  94.     })  
  95. </script>  

From the code, shown above, you can notice that Kendo DropDownList is implemented with the change event. In the change event function definition, we are going to change Kendo Grid datasource, based on the company selection. If company A is selected, the employee of company A will be populated in the Kendo Grid, else, if the company B is selected, the employee of the company B will be populated in Kendo Grid.

Result

Initially the employees of company A will be populated in the Grid.

  

Selecting company B from the dropdownlist of the employees of company B will be populated in the Grid.

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

Source Code


Similar Articles