-->

Grouping Gridview in MVC 4

Grouping Gridview in MVC

Introduction

In this post, I explain how to Group data in Gridview using MVC. Sometimes we need to show group data in our MVC Application like show List of State group by Country. Here I have done this using MVC 4. Steps are given below:-

For ASP.Net Developer visit: Grouping Gridview in C#.Net.

Prerequisite

I used followings:
  • .Net framework 4.0
  • Entity Framework
  • SQL Server 2008

Steps :

Just follow the steps and get result easily.

Step - 1 : Create New Project

Go to File > New > Project > Select asp.net mvc4 web application > Entry Application Name > Click 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 two tables and insert data for show grouped data in Gridview

Open Database > Right Click on Table > Add New Table > Add Columns > Save > Enter table name > Ok.
In this example, I have created two table 1. CountryMaster 2. StateMaster.
Create the table and insert data for Show in grouped gridview.

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: Creare a ModelView

Here in this example, I need a ViewModel for getting data as a single entity.
Add a Folder(ViewModel) into your project root directory. > Right Click on the folder > Add > Class > Enter Class name > Add.
Write the following code to your class.

  1. public class CountryState
  2. {
  3. public int CountryID { get; set; }
  4. public string CountryName { get; set; }
  5. public int StateID { get; set; }
  6. public string StateName { get; set; }
  7. }

STEP-6: Add Action For Populate Data.

Go To Controller > Add your action > write the following the code and Rebuild your application.
  1. public ActionResult Gridview()
  2. {
  3. List<CountryState> StateList = new List<CountryState>();
  4. using (CountryStateEntities dc = new CountryStateEntities())
  5. {
  6. var v = (from a in dc.CountryMasters
  7. join b in dc.StateMasters on a.CountryID equals b.CountryID
  8. select new
  9. {
  10. a.CountryID,
  11. a.CountryName,
  12. b.StateID,
  13. b.StateName
  14. });
  15. foreach (var i in v)
  16. {
  17. StateList.Add(new CountryState
  18. {
  19. CountryID = i.CountryID,
  20. CountryName = i.CountryName,
  21. StateID = i.StateID,
  22. StateName = i.StateName
  23. });
  24. }
  25. }
  26. return View(StateList);
  27. }

Step-7: Add View for show data on a page.

Right Click on your Action > Add View > Enter View name > Check Create a strongly-type view > Select your model class > Select Scaffold templete > Select list > Add.

Modify Your View as follow
  1. @model IEnumerable<MVCGridviewGrouping.ViewModel.CountryState>
  2. @{
  3. ViewBag.Title = "Gridview";
  4. }
  5. <h2>Gridview Grouping</h2>
  6. <div id="accordion">
  7. @{
  8. foreach (var item in Model.Select(a => a.CountryName).Distinct())
  9. {
  10. <h3>Country : @item</h3>
  11. <div>
  12. <table cellspacing="10">
  13. <tr>
  14. <th>
  15. State ID
  16. </th>
  17. <th>
  18. State Name
  19. </th>
  20. <th></th>
  21. </tr>
  22. @foreach (var i in Model.Where(a=>a.CountryName.Equals(@item)))
  23. {
  24. <tr>
  25. <td>
  26. @Html.DisplayFor(modelItem => i.StateID)
  27. </td>
  28. <td>
  29. @Html.DisplayFor(modelItem => i.StateName)
  30. </td>
  31. <td>
  32. @Html.ActionLink("Edit", "Edit", new { /* id=item.PrimaryKey */ }) |
  33. @Html.ActionLink("Details", "Details", new { /* id=item.PrimaryKey */ }) |
  34. @Html.ActionLink("Delete", "Delete", new { /* id=item.PrimaryKey */ })
  35. </td>
  36. </tr>
  37. }
  38. </table>
  39. </div>
  40. }
  41. }
  42. </div>
  43. <script language="javascript">
  44. $(function () {
  45. $("#accordion").accordion({
  46. heightStyle: "content"
  47. });
  48. });
  49. </script>

Step-8: Modify your _Layout.cshtml view

Add some 2 js and 1 CSS file to _Layout.cshtml view

  1. <link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" type="text/javascript" />
  2. <script src="http://code.jquery.com/jquery-1.9.1.js" type="text/javascript"></script>
  3. <script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js" type="text/javascript"></script>
Remove Line

@Scripts.Render("~/bundles/jquery")

Complete Code Here _Layout.cshtml view 


  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8" />
  5. <title>@ViewBag.Title - My ASP.NET MVC Application</title>
  6. <link href="~/favicon.ico" rel="shortcut icon" type="image/x-icon" />
  7. <meta name="viewport" content="width=device-width" />
  8. @Styles.Render("~/Content/css")
  9. @Scripts.Render("~/bundles/modernizr")
  10. <link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" type="text/javascript" />
  11. <script src="http://code.jquery.com/jquery-1.9.1.js" type="text/javascript"></script>
  12. <script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js" type="text/javascript"></script>
  13. </head>
  14. <body>
  15. <header>
  16. <div class="content-wrapper">
  17. <div class="float-left">
  18. <p class="site-title">@Html.ActionLink("your logo here", "Index", "Home")</p>
  19. </div>
  20. <div class="float-right">
  21. <section id="login">
  22. @Html.Partial("_LoginPartial")
  23. </section>
  24. <nav>
  25. <ul id="menu">
  26. <li>@Html.ActionLink("Home", "Index", "Home")</li>
  27. <li>@Html.ActionLink("About", "About", "Home")</li>
  28. <li>@Html.ActionLink("Contact", "Contact", "Home")</li>
  29. <li>@Html.ActionLink("Gridview Grouping","GridView","Home")</li>
  30. </ul>
  31. </nav>
  32. </div>
  33. </div>
  34. </header>
  35. <div id="body">
  36. @RenderSection("featured", required: false)
  37. <section class="content-wrapper main-content clear-fix">
  38. @RenderBody()
  39. </section>
  40. </div>
  41. <footer>
  42. <div class="content-wrapper">
  43. <div class="float-left">
  44. <p>&copy; @DateTime.Now.Year - My ASP.NET MVC Application</p>
  45. </div>
  46. </div>
  47. </footer>
  48. @RenderSection("scripts", required: false)
  49. </body>
  50.  
  51. </html>

Step-9: Run Application

Look Result show in your browser.

Grouping Gridview in MVC

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: