GridView in MVC 2 in Asp.Net

Step 1: Create a Project with MVC 2

Step 2: Go to Models Folder and add a Class with TestModels.cs name and write the below code.

using System;

using System.Collections.Generic;
using
System.ComponentModel;
using
System.ComponentModel.DataAnnotations;
using
System.Globalization;
using
System.Linq;
using
System.Web;
using
System.Web.Mvc;
using
System.Web.Security;
using
System.Data.SqlClient;
using
System.Data;

namespace MVCTEST1.Models

{
    public class LogOnModel
    {

        string ConnString = @"server=IT-WSPC-F10\SQL;Initial Catalog=pankaj;User Id=sa;Password=abc@123";

        DataSet ds;

        [Required]

        [DisplayName("User name")]

        public string UserName { get; set; }

        [Required]

        [DataType(DataType.Password)]

        [DisplayName("Password")]

        public string Password { get; set; }

        [Required]

        [DisplayName("Email")]

        public string Email { get; set; }

        public DataTable GetGridData()

        {

           
try

            {

                ds = new DataSet();

                SqlConnection con = new SqlConnection(ConnString);

                SqlDataAdapter ada = new SqlDataAdapter("select * from User_Login", ConnString);

                ada.Fill(ds);

                return ds.Tables[0];

            }

            catch (Exception err)

            {

                throw err;

            }

        }

    }

}

Step 3: Go to Controller Folder and add a new controller class with TestController.cs name
and write the below code.

public ActionResult About()

{
DataTable dtGrid = new DataTable();

LogOnModel objGrid = new LogOnModel();

dtGrid = objGrid.GetGridData();

List<LogOnModel> Gridd = new List<LogOnModel>();

foreach
(DataRow dr in dtGrid.Rows)
{

LogOnModel users = new LogOnModel();

users.UserName = dr["UserName"].ToString();
 

users.Email = dr["User_Pass"].ToString();

users.Email = dr["City"].ToString();
Gridd.Add(users);

}

return
View("About", Gridd);
}

Step 4: Go to view folder and add a view named About.aspx and write the below code.

<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<IEnumerable<MVCTEST1.Models.LogOnModel>>" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head id="Head1" runat="server">
    <title>About</title
>

</
head>
<
body>
    <div
>

        <p
>

            <%: ViewData["Message"]
%>

        </p
>

    </div
>

    <div
>

        <table width
="50%">

            <tr
>

                <td width="10%" height
="30px">

                    ID

                </td
>

                <td width
="20%">

                    User Name

                </td
>

                <td width
="10%">

                    Age

                </td
>

                <td width
="10%">

                    Gender

                </td
>

            </tr
>

            <%foreach (var item in Model)

              {
%>

            <tr
>

                <td
>

                    No Data

                </td
>

                <td
>

                    <%=Html.DisplayFor(x => item.UserName)
%>

                </td
>

                <td
>

                    No Data

                </td
>

                <td
>

                    <%=Html.DisplayFor(x => item.Password)
%>

                </td
>

                <td
>

                    <%=Html.DisplayFor(x => item.Email)
%>

                </td
>

            </tr
>

            <%}
%>

        </table
>

    </div
>

</
body>
</
html>

And run application. You can put your table as your requirement

Ebook Download
View all
Learn
View all