-->

How to use Telerik Grid in MVC4 application



Introduction

Here In this article I would like to explain how to use Telerik Grid in MVC4 application.
Telerik Grid - supporting paging/sorting/filtering, as well as advanced features like grouping, hierarchy and master/detail views.

Now we are going to to use Telerik Grid for display data in MVC4 application. Please follow the below steps...

Steps :

Step - 1 : Create New Project.

Go to File > New > Project > Select asp.net MVC4 web application > Entry Application Name > Click OK > Select Internet Application > Select view engine Razor > OK

Step-2: Add a Database.

Go to Solution Explorer > Right Click on App_Data folder > Add > New item > Select SQL Server Database Under Data > Enter Database name > Add.


Step-3: Create a table for Fetch Data.

Open Database > Right Click on Table > Add New Table > Add Columns > Save > Enter table name > Ok.
In this example, I have used table as below

Step-4: Add Entity Data Model.

Go to Solution Explorer > Right Click on Project name form Solution Explorer > Add > New item > Select ADO.net Entity Data Model under data > Enter model name > Add.
A popup window will come (Entity Data Model Wizard) > Select Generate from database > Next >
Chose your data connection > select your database > next > Select tables > enter Model Namespace > Finish.

Step-5: Add Reference of TelerikMvcExtensions from Nuget.

Go to Solution Explorer > Manage Nuget Packages... > Search for "TelerikMvcExtensions" > Install.

Step-6: Create a Controller .

Go to Solution Explorer > Right Click on Controllers folder form Solution Explorer > Add > Controller > Enter Controller name > Select Templete "empty MVC Controller"> Add.

Here I have created a controller "HomeController"

Step-7: Add new action into your controller for Get view and show data in Telerik Grid

Here I have used "Index" Action into "Home" Controller. Please write this following code
  1. public ActionResult Index()
  2. {
  3. List<TopCompany> list = new List<TopCompany>();
  4. using (MyDatabaseEntities dc = new MyDatabaseEntities())
  5. {
  6. list = dc.TopCompanies.OrderBy(a => a.Rank).ToList();
  7. }
  8. return View(list);
  9. }

Step-8: Add view for the Action & design.

Right Click on Action Method (here right click on Index action) > Add View... > Enter View Name > Select View Engine (Razor) > Add.
Complete View
  1. @using Telerik.Web.Mvc.UI;
  2. @model IEnumerable<MVCTelerikGrid.TopCompany>
  3.  
  4. @{
  5. ViewBag.Title = "Index";
  6. }
  7.  
  8. @{
  9. Html.Telerik().Grid(Model)
  10. .Name("MyGrid")
  11. .Columns(col =>
  12. {
  13. col.Bound(o => o.SLID).Width(120).Groupable(false);
  14. col.Bound(o => o.Rank).Width(120).Groupable(false);
  15. col.Bound(o => o.CompanyName).Groupable(false);
  16. col.Bound(o => o.Industry).Groupable(true); // I want only this as Groupable
  17. col.Bound(o => o.Revenue).Groupable(false);
  18. })
  19. .Pageable(a => a.PageSize(10)) // for enable paging
  20. .Groupable() // for enable grouping
  21. .Sortable() // for sorting
  22. .Filterable() // for Filtering
  23. .Render();
  24. }
  25. @* Here We will use telerik grid for show data *@
  26.  
  27.  
  28. @* Here have to add some css and js for everything work as expected *@
  29. @(Html.Telerik().StyleSheetRegistrar().DefaultGroup(group=> group.Add("telerik.common.css").Add("telerik.windows7.css").Combined(true).Compress(true)))
  30. @(Html.Telerik().ScriptRegistrar())

Step-9: Run Application.

Download Live Demo



Download    Live Demo
 

Hello ! My name is Sourav Mondal. I am a software developer working in Microsoft .NET technologies since 2010.

I like to share my working experience, research and knowledge through my site.

I love developing applications in Microsoft Technologies including Asp.Net webforms, mvc, winforms, c#.net, sql server, entity framework, Ajax, Jquery, web api, web service and more.

Related Posts: