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

As we aware about datagridview and gridview of control in 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.



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


    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; }



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);

            SqlDataAdapter sda = new SqlDataAdapter(cmd);

            DataSet ds = new DataSet();

            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);




@model IEnumerable<MVCCODESOLUTIONS.Models.recordDetail>


    Layout = null;
    WebGrid grid = new WebGrid(Model, rowsPerPage: 4);

<!DOCTYPE html>

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

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


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

                                ), mode: WebGridPagerModes.Numeric)

    <script type="text/javascript" src="//"></script>
    <script type="text/javascript">
        var message = "@ViewBag.Message";
        $(function () {
            if (message != "") {

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.