Bind girdview(webgrid) with database using ado operation in MVC

As we aware about datagridview and gridview of control in asp.net but as we know that MVC does not work with server control it works only with client side control means html control. So here we will bind our code with webgrid .Many times we need to bind our database with gridview in MVC so here we can do with entity framework or ADO operation so we will learn about how to bind webgrid with database using ado operation in MVC with sorting and paging also.


DataBase


Model:-

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace MVCCODESOLUTIONS.Models
{
   

    public class recordDetail
    {
        public int record_id { get; set; }
        public string Name { get; set; }
        public string Contact_no { get; set; }
        public string Email { get; set; }


       
    }
}

Controller:-

using MVCCODESOLUTIONS.Models;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Web;
using System.Web.Mvc;

namespace MVCCODESOLUTIONS.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {

            List<recordDetail> listRD = new List<recordDetail>();

            string constr = "Data Source=NEERAJ-PC;Initial Catalog=CodeSolution;Persist Security Info=True;User ID=sa; password=12345678";
            SqlConnection con = new SqlConnection(constr);
            SqlCommand cmd = new SqlCommand("select * from record", con);
            con.Open();

            SqlDataAdapter sda = new SqlDataAdapter(cmd);

            DataSet ds = new DataSet();
            sda.Fill(ds);

            foreach (DataRow dr in ds.Tables[0].Rows)

            {
                listRD.Add(new recordDetail
                {


                    record_id = Convert.ToInt16(dr["record_id"]),

                    Name = dr["Name"].ToString(),
                    Contact_no = dr["Contact_no"].ToString(),

                    Email = dr["Email"].ToString()
                });
            }
            return View(listRD);

        }

    }
}

View:-

@model IEnumerable<MVCCODESOLUTIONS.Models.recordDetail>

@{

    Layout = null;
    WebGrid grid = new WebGrid(Model, rowsPerPage: 4);
    grid.Pager(WebGridPagerModes.FirstLast);
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>How to bind in gridview with database in MVC</title>
</head>
<body>
@using (Html.BeginForm("Index", "Home", FormMethod.Post))
{

        <fieldset style="width: 500px;">
            <legend>Bind gridview with database in MVC</legend>



            @grid.GetHtml(columns:

                                grid.Columns
                                (
                                    grid.Column("record_id", "Id"),
                                    grid.Column("Name", "Name"),
                                    grid.Column("Contact_no", "Contact Number"),
                                    grid.Column("Email", "Email")


                                ), mode: WebGridPagerModes.Numeric)



        </fieldset>
    }
    <script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
    <script type="text/javascript">
        var message = "@ViewBag.Message";
        $(function () {
            if (message != "") {
                alert(message);
            }
        });
    </script>
</body>
</html>
Out-Put



Bind girdview(webgrid) with database using ado operation in MVC Bind girdview(webgrid) with database using ado operation in MVC Reviewed by NEERAJ SRIVASTAVA on 9:22:00 AM Rating: 5

No comments:

Powered by Blogger.