Working With Telerik Grid In MVC

Telerik is a Bulgarian company offering software tools for web, mobile, desktop application development, tools, and subscription services for cross-platform application development. Here, in this article, I will explain how to use Telerik Grid in ASP. NET MVC.
 
To use the Telerik UI, here, I have created a new MVC project. Look at my Solution Explorer.

 
I have added an entity data model to connect with the database as follows.



Here is my connection string and context.



Here is my table in ORM window.

 

In case anyone needs the table structure and data, here is my table script:
  1. USE [CodeX]  
  2. GO  
  3. /****** Object:  Table [dbo].[tbl_employee]    Script Date: 23/07/2016 1:29:43 AM ******/  
  4. SET ANSI_NULLS ON  
  5. GO  
  6. SET QUOTED_IDENTIFIER ON  
  7. GO  
  8. CREATE TABLE [dbo].[tbl_employee](  
  9.     [id] [int] NOT NULL,  
  10.     [firstName] [nvarchar](50) NULL,  
  11.     [LastName] [nvarchar](50) NULL,  
  12.     [EmployeeId] [int] NULL,  
  13.     [Email] [nvarchar](50) NULL,  
  14.     [MobileNo] [nvarchar](10) NULL,  
  15.     [State] [nvarchar](30) NULL,  
  16.     [Country] [nvarchar](50) NULL,  
  17.  CONSTRAINT [PK_tbl_employee] PRIMARY KEY CLUSTERED   
  18. (  
  19.     [id] ASC  
  20. )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]  
  21. ) ON [PRIMARY]  
  22.   
  23. GO  
  24. INSERT [dbo].[tbl_employee] ([id], [firstName], [LastName], [EmployeeId], [Email], [MobileNo], [State], [Country]) VALUES (1, N'Debendra', N'Dash', 10090, N'debendra@gmail.com', N'8123371257', N'Odisha', N'India')  
  25. INSERT [dbo].[tbl_employee] ([id], [firstName], [LastName], [EmployeeId], [Email], [MobileNo], [State], [Country]) VALUES (2, N'Sridhar', N'Dash', 10090, N'sridhar1991@gmail.com', N'808900099', N'karnataka', N'India')  
  26. INSERT [dbo].[tbl_employee] ([id], [firstName], [LastName], [EmployeeId], [Email], [MobileNo], [State], [Country]) VALUES (11, N'Manas', N'sing', 56, N'manas@gmail.com', N'8090705545', N'UP', N'India')  
  27. INSERT [dbo].[tbl_employee] ([id], [firstName], [LastName], [EmployeeId], [Email], [MobileNo], [State], [Country]) VALUES (40, N'Tushar', N'Jena', 78, N'Tusar@gmail.com', N'676789990', N'MP', N'India')  
  28. INSERT [dbo].[tbl_employee] ([id], [firstName], [LastName], [EmployeeId], [Email], [MobileNo], [State], [Country]) VALUES (67, N'Rubi', N'singh', 90, N'Rubi32@gmail.com', N'8018070999', N'Ap', N'India')  
  29. INSERT [dbo].[tbl_employee] ([id], [firstName], [LastName], [EmployeeId], [Email], [MobileNo], [State], [Country]) VALUES (99, N'Sujit', N'Tiwari', 80, N'sujit@gmail.com', N'7894290976', N'UP', N'India')  
  30. INSERT [dbo].[tbl_employee] ([id], [firstName], [LastName], [EmployeeId], [Email], [MobileNo], [State], [Country]) VALUES (120, N'Kabita', N'Kumari', 129, N'kabita@yahoo.com', N'7894290765', N'MP', N'India')  
  31. INSERT [dbo].[tbl_employee] ([id], [firstName], [LastName], [EmployeeId], [Email], [MobileNo], [State], [Country]) VALUES (123, N'Dinesh', N'Kumar', 234, N'Dinesh123@gmail.com', N'8012345678', N'Bihar', N'India')  
  32. INSERT [dbo].[tbl_employee] ([id], [firstName], [LastName], [EmployeeId], [Email], [MobileNo], [State], [Country]) VALUES (150, N'Sachin', N'Tendulkar', 2345, N'sachin@gmail.com', N'8909900008', N'Assam', N'India')  
  33. INSERT [dbo].[tbl_employee] ([id], [firstName], [LastName], [EmployeeId], [Email], [MobileNo], [State], [Country]) VALUES (190, N'Sourav', N'Ganguli', 234, N'sourav@gmail.com', N'7894270987', N'WB', N'India')  
  34. INSERT [dbo].[tbl_employee] ([id], [firstName], [LastName], [EmployeeId], [Email], [MobileNo], [State], [Country]) VALUES (191, N'Pankaj', N'sahu', 188, N'pankaj.sahu@gmail.com', N'7894270987', N'WB', N'India')  
  35. INSERT [dbo].[tbl_employee] ([id], [firstName], [LastName], [EmployeeId], [Email], [MobileNo], [State], [Country]) VALUES (192, N'Kisan', N'agrawal', 177, N'contact@gmail.com', N'7894270989', N'AP', N'India')  
  36. INSERT [dbo].[tbl_employee] ([id], [firstName], [LastName], [EmployeeId], [Email], [MobileNo], [State], [Country]) VALUES (193, N'Ravi', N'kumar', 199, N'Ravi@gmail.com', N'7894270989', N'ODISHA', N'India')  
  37. INSERT [dbo].[tbl_employee] ([id], [firstName], [LastName], [EmployeeId], [Email], [MobileNo], [State], [Country]) VALUES (194, N'Kumar', N'dora', 100, N'doea@gmail.com', N'3494270987', N'JHARKHAND', N'India')  
  38. INSERT [dbo].[tbl_employee] ([id], [firstName], [LastName], [EmployeeId], [Email], [MobileNo], [State], [Country]) VALUES (195, N'Prakash', N'jha', 30, N'jha12345@yahoo.com', N'7894270987', N'BIHAR', N'India')  
  39. INSERT [dbo].[tbl_employee] ([id], [firstName], [LastName], [EmployeeId], [Email], [MobileNo], [State], [Country]) VALUES (196, N'Jeetu', N'nayak', 34, N'jeetu@yahoo.com', N'7894270987', N'PUNJAB', N'India')  
  40. INSERT [dbo].[tbl_employee] ([id], [firstName], [LastName], [EmployeeId], [Email], [MobileNo], [State], [Country]) VALUES (197, N'Rani', N'sahu', 35, N'Rani@gmail.com', N'7894270987', N'GOA', N'India')  
  41. INSERT [dbo].[tbl_employee] ([id], [firstName], [LastName], [EmployeeId], [Email], [MobileNo], [State], [Country]) VALUES (198, N'Bharat', N'das', 67, N'bharat234@gmail.com', N'7894279900', N'GOA', N'India')  
  42. INSERT [dbo].[tbl_employee] ([id], [firstName], [LastName], [EmployeeId], [Email], [MobileNo], [State], [Country]) VALUES (199, N'OM', N'prakash', 68, N'OM@gmail.com', N'8090907090', N'AP', N'India')  
  43. INSERT [dbo].[tbl_employee] ([id], [firstName], [LastName], [EmployeeId], [Email], [MobileNo], [State], [Country]) VALUES (200, N'Sujata', N'sahu', 29, N'sahu23@gmail.com', N'709087690', N'KARNATAKA', N'India')  
Now, after connecting with the database, I will go to NuGet Package Manager and download the following "TelerikMvcExtensions" dll in my project.

 

It will add the following dll and scripts into my project, as follow.



Now, I will add an Employee model, as shown below.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5.   
  6. namespace Tellerik_grid.Models  
  7. {  
  8.     public class employee  
  9.     {  
  10.         public int Id { getset; }  
  11.         public string FirstName { getset; }  
  12.         public string LastName { getset; }  
  13.         public string Email { getset; }  
  14.         public int EmployeeId { getset; }  
  15.         public string MobileNo { getset; }  
  16.   
  17.         public string State { getset; }  
  18.         public string Country { getset; }  
  19.          
  20.          
  21.     }  
  22. }   
Now, I will go to Home Controller and add an action method in it.
  1. public class HomeController : Controller  
  2. {  
  3.     public ActionResult Index()  
  4.     {  
  5.         return View();  
  6.     }  
  7.     public ActionResult GetData()  
  8.     {  
  9.         List<employee> li = new List<employee>();  
  10.         CodeXEntities CX = new CodeXEntities();  
  11.         var det = CX.tbl_employee;  
  12.         foreach(var x in det)  
  13.         {  
  14.             employee emp = new employee();  
  15.             emp.Id = x.id;  
  16.             emp.FirstName = x.firstName;  
  17.             emp.LastName = x.LastName;  
  18.             emp.MobileNo = x.MobileNo;  
  19.             emp.State = x.State;  
  20.             emp.Country = x.Country;  
  21.             emp.Email = x.Email;  
  22.             emp.EmployeeId =Convert.ToInt32(x.EmployeeId);  
  23.             li.Add(emp);  
  24.   
  25.         }  
  26.               
  27.         return View(li);  
  28.   
  29.     }  
This action method simply retrieves the data from my table and returns to the View. Now, this is how I will add a GetData View here.

 
 
Now, here is my View where I have bound the controller data in Telerik Grid.
  1. @model IEnumerable<Tellerik_grid.Models.employee>  
  2. @using Telerik.Web.Mvc.UI;  
  3.   
  4. @{  
  5.     ViewBag.Title = "GetData";  
  6. }  
  7.   
  8. </br>  
  9. <div>  
  10.     @{  
  11.   
  12.         Html.Telerik().Grid(Model)  
  13.             .Name("Grid")  
  14.              .DataKeys(keys => keys.Add(c => c.Id))  
  15.             .Footer(true)  
  16.             .Sortable()  
  17.             .Filterable()  
  18.   
  19.             .Scrollable(scr => scr.Height(330))  
  20.               
  21.             .Columns(x =>  
  22.             {  
  23.                 x.Bound(m => m.Id).Width(120).HeaderTemplate("UserId").Title("User ID").Groupable(false);  
  24.                 x.Bound(m => m.FirstName).Width(120).Groupable(false).Filterable(true).Groupable(true);  
  25.                 x.Bound(m => m.LastName).Width(120).Groupable(true).Filterable(true).Groupable(true);  
  26.                 x.Bound(m => m.MobileNo).Width(120).HeaderTemplate("UserId").Filterable(true);  
  27.                 x.Bound(m => m.Email).Width(120).Groupable(false).Filterable(true);  
  28.                 x.Bound(m => m.State).Width(120).Groupable(true).Filterable(true);  
  29.                 x.Bound(m => m.Country).Width(120).Groupable(false).Filterable(true);  
  30.             })  
  31.   
  32.           .Pageable(x => x.PageSize(10))  
  33.   
  34.             .Render();  
  35.   
  36.     @(Html.Telerik().StyleSheetRegistrar().DefaultGroup(group => group.Add("telerik.common.css").Add("telerik.windows7.css").Add("telerik.hay.min.css").Combined(true).Compress(true)))  
  37.     @(Html.Telerik().ScriptRegistrar())  
  38.     }  
  39.   
  40. </div>  
Now, save the project and run it.This will give the following output.



As we have defined filterable, it will filter the data, as shown below:


Now, if you want to filter, it will filter it.


NOTE: The Telerik Grid contains various properties, like - 
  • HeaderTemplate: This is the string which will appear in the header of the Grid.
  • Name: We can say it's the Id of the Telerik Grid.
  • Footer: This is a boolean property. True indicates that we need a footer.
  • Filterable: By using this property, the filter option in the Grid is enabled.
  • Pageable: Used for enabling the paging where you have to give the page size.
Defining CSS for the Telerik Grid
  • @(Html.Telerik().StyleSheetRegistrar().DefaultGroup(group => group.Add("telerik.common.css").Add("telerik.windows7.css").Add("telerik.hay.min.css").Combined(true).Compress(true)))
    1. @(Html.Telerik().ScriptRegistrar()) 
We have various CSS files included in our project. You can attach any CSS file of your choice to the Telerik Grid. This will give a different look and feel to the View. Hope you liked this article. If you have any doubt, you can comment, and I will try to help you.