MVC 4 How to Display Data in Gridview

MVC 4 How to Display Data in Gridview

Introduction : This Artical Explain How to Perform Select Operation Using Gridview with SQL Server Database in MVC4.
STEP 1 : Open Visual Studio 2012 and Create Databse

CREATE TABLE [dbo].[tbl_user] (
    [Id]      INT           IDENTITY (1, 1) NOT NULL,
    [name]    NVARCHAR (50) NOT NULL,
    [city]    NVARCHAR (50) NOT NULL,
    [address] NVARCHAR (50) NOT NULL,
    PRIMARY KEY CLUSTERED ([Id] ASC)
); 


 Step 2: Now Create class DAL.cs, BAL.cs  in  Models
  Now, DAL.cs

 public class DAL
    {
        public int id { get; set; }
        public string name { get; set; }
        public string city { get; set; }
        public string address { get; set; }

    } 

In Classs BAL.cs We can write the Select Query  with using List<> t

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
namespace MvcGrid.Models
{
    public class BAL
    {
        SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=
        D:\Chetan_program\App_Data\Database1.mdf;Integrated Security=True"
);

        public List<DAL> get()
        {
            List<DAL> L = new List<DAL>();
            con.Open();
            SqlCommand cmd = new SqlCommand("select * from tbl_user",con);
            SqlDataReader dr = cmd.ExecuteReader();
           
            while (dr.Read())
            {
                DAL da = new DAL();
              
                da.name = (dr["name"]).ToString();
                da.address = (dr["address"]).ToString();
                da.city = (dr["city"]).ToString();
                L.Add(da);

            }
            con.Close();
            return L;
        }
   } 



Step 3:  Now Create Controller As Following and Give name as ABCController.cs ,controller can
interfere between View And Model.

















you can need to add Name Space using MvcGrid.Models; means Projec_name.Models
 using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcGrid.Models;
namespace MvcGrid.Controllers
{
    public class ABCController : Controller
    {
        BAL ba = new BAL();
        public ActionResult Index()
        {
            return View();
        }
        public ActionResult Grid()
        {
            DAL da = new DAL();
            ba.get();
            return View(ba.get());
        }
Now, you can Add View As Following






















 Now Add View As  Grid.cshtml  Following,

  @model List<MvcGrid.Models.DAL>

@{
    ViewBag.Title = "Grid";
}

<h2>Grid</h2>
@{

   var Grid=new WebGrid(Model);
   @Grid.GetHtml();

}

Step 4 : Run Program



See Other Tutorial :

*  AngularJS Crude With ASP.NET MVC

*  Convert Rows to columns using 'Pivot' in SQL Server

*  Mvc Registration page With user exist using Ajax method

*  MVC 4 How to Perform Insert Update Delete Edit Select Operation

*  MVC4 Edit,update,Delete,cancel inside gridview using sql database

*  MVC 4 Gridview To Display Data Using SQL Server Database With Simple code 

*  Login page in asp.net Mvc4 Web application

*  Mvc4 How to bind Dropdown List using Sql Database

Gridview find control in asp.net
Previous
Next Post »