Basic Gridview

(ASP.NET MVC)


Hi,
In this article i am discussing about how to create basic gridview in asp.net mvc using Html Table.

View:-

@{
    Layout = "~/Views/Shared/_Layout.cshtml";
}

@model System.Collections.Generic.List<ASP.NETMVCFORUMS.Controllers.Products>

<h2>Products</h2>

<fieldset>
    <legend>Products</legend>
    <table>
        <tr>
        <th>
            ProductID
        </th>
            <th>

            </th>
        <th>
            ProductName
        </th>
            <th>

            </th>
        <th>
            SupplierID
        </th>
            <th>

            </th>
        <th>
            UnitPrice
        </th>
            <th>

            </th>
    </tr>
 

    @foreach (var item in Model) {
        <tr>
            <td>
    <div class="display-label">
        @Html.DisplayFor(model => item.ProductID)
    </div>
                </td>
             <td>
    <div class="display-field">
       @Html.TextBoxFor(model=>item.ProductID)
    </div>
</td>
             <td>
    <div class="display-label">
        @Html.DisplayFor(model =>item.ProductName)
    </div>
                 </td>
             <td>
    <div class="display-field">
        @Html.TextBoxFor(model => item.ProductName)
    </div>
</td>
             <td>
    <div class="display-label">
        @Html.DisplayFor(model => item.SupplierID)
    </div>
                 </td>
             <td>
    <div class="display-field">
        @Html.TextBoxFor(model => item.SupplierID)
    </div>
</td>
             <td>
    <div class="display-label">
        @Html.DisplayFor(model => item.UnitPrice)
    </div>
                 </td>
             <td>
    <div class="display-field">
       @Html.TextBoxFor(model => item.UnitPrice)
    </div>
                 </td>
             </tr>
    }
          
          </table>
  
</fieldset>
<p>
    @Html.ActionLink("Edit", "Edit", new { /* id=Model.PrimaryKey */ }) |
    @Html.ActionLink("Back to List", "Index")
</p>



Controller and Model:-

 private DBConnect db = new DBConnect();

   public ActionResult ProductsView()
        {
            return View(db.GetProducts().ToList());
        }

  public class Products
    {
        public int ProductID { get; set; }
        public String ProductName { get; set; }
        public String SupplierID { get; set; }
        public string UnitPrice { get; set; }
    }

  public class DBConnect
      {
          public List<Products> GetProducts()
          {
              string StrCon = "Data Source=GIRI-PC\\SQLEXPRESS;Initial Catalog=Northwnd;Integrated Security=True;";
              List<Products> products = new List<Products>();
              using (SqlConnection con = new SqlConnection(StrCon))
              {
                  string SqlCommand = "SELECT  ProductID,ProductName,SupplierID,UnitPrice FROM Products";
                  SqlCommand cmd = new SqlCommand(SqlCommand, con);
                  con.Open();

                  SqlDataReader dr = cmd.ExecuteReader();
                  while (dr.Read())
                  {
                      Products p = new Products();
                      p.ProductID = Convert.ToInt32(dr[0]);
                      p.ProductName = dr[1].ToString();
                      p.SupplierID = dr[2].ToString();
                      p.UnitPrice = dr[3].ToString();

                      products.Add(p);
                  }

                  con.Close();

              }
              return products;

          }
      }


Image

Ratings


Average Rating: 5.00 by 1 users
Giri Prasad
7/1/2013
7/1/2013
Download PDF

Comments