Bind GridView With Database In MVC 5 In C#

In this tutorial, I’ll show you how to bind GridView using MVC5 C# using Razor. In the previous tutorial I haven’t used any database, but here we will use and then we manually feed some data into our table and bind that data to GridView. So it’s a kind of Binding GridView with database.

Step 1

Open Visual Studio 2010, Go to the New Project - Visual C#, Web, then ASP.NET MVC Web Application and click OK.



Step 2

After Clicking OK, New ASP.NET MVC5 Project window will open and there you have to choose MVC and press OK.



Step 3

After Clicking OK, you will see something like the following image in your Solution Explorer. You need to look out for Model, Controller and View folders that are the main files in MVC, others are too but these are main files.



DATABASE CHAMBER:

Step 4

Right Click on your Project - Add New Item, then SQL Server Database and Add it. Go to your Database that resides in Server Explorer - [Database.mdf], We will create a table - tbl_data, Go to the database.mdf, Table and Add New table. Design your table like the following:



Web.Config File:

  1. <connectionStrings>  
  2.     <add name="DefaultConnection" connectionString="Data Source=(LocalDb)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\aspnet-MVCBindGridView-20150920090324.mdf;Initial Catalog=aspnet-MVCBindGridView-20150920090324;Integrated Security=True"  
  3.       providerName="System.Data.SqlClient" />  
  4.   
  5.     <add name="StudentContext" connectionString="Data Source=NiluNilesh;Initial Catalog=mynewdata;Integrated Security=True" providerName="System.Data.SqlClient"></add>  
  6.           
  7. </connectionStrings>  
In Model

Step 4

Right Click on Models - Add New Item, then Add a Class File [Class.cs] and ame it as Student.cs. Write the following code in Student.cs file.

Student.cs
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.ComponentModel.DataAnnotations;  
  6. using System.ComponentModel.DataAnnotations.Schema;  
  7.   
  8. namespace MVCBindGridView.Models  
  9. {  
  10.     [Table("tbl_data")]  
  11.     public class Student  
  12.     {  
  13.         [Key]  
  14.         public int id { getset; }  
  15.         public string name { getset; }  
  16.         public string city { getset; }  
  17.   
  18.     }  
  19. }  
Add another class file to Model name and name it “StudentContext”.

StudentContext.cs
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Data.Entity;  
  6.   
  7. namespace MVCBindGridView.Models  
  8. {  
  9.     public class StudentContext : DbContext  
  10.     {  
  11.         public DbSet<Student> student { getset; }  
  12.     }  
  13. }  
In Controller

Step 5

Right Click on Controller, Add Empty Controller, Name it – StudentController.cs. Write the following code and don’t forget to add namespace of model.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.Mvc;  
  6. using MVCBindGridView.Models;  
  7.   
  8. namespace MVCBindGridView.Controllers  
  9. {  
  10.     public class StudentController : Controller  
  11.     {  
  12.         // GET: Student  
  13.         public ActionResult Index()  
  14.         {  
  15.   
  16.   
  17.             StudentContext stdntcntxt = new StudentContext();  
  18.   
  19.             var data = stdntcntxt.student;  
  20.             return View(data.ToList());  
  21.         }  
  22.     }  
  23. }  
In View

Step 6

Right click on Index() method and Add View like the following image.



You will see under view that there is a folder named student created and inside that folder get your index.cshtml. Write the following code:

Index.cshtml

  1. @model IEnumerable<MVCBindGridView.Models.Student>  
  2.   
  3. @{  
  4.     ViewBag.Title = "Bind Gridview with Database";  
  5.     WebGrid grid = new WebGrid(Model);  
  6. }  
  7.   
  8. <h2>Bind GridView in MVC5 with Database</h2>  
  9. @grid.GetHtml(columns: new[]  
  10.   
  11. {  
  12.     grid.Column("id"),  
  13.     grid.Column("name"),  
  14.     grid.Column("city")  
  15.       
  16.   
  17. }   
  18. )  
OUTPUT CHAMBER:


Hope you like it. Thank you for reading. Have a good day.

Up Next
    Ebook Download
    View all
    Learn
    View all