Gridview without database


The following program show how to create a GridView without taking values from database.

Default.aspx page:-
  

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <center> 
    <div>
        <h1>Gridview without database:-</h1>
    <asp:GridView ID="Gridview1" runat="server">
        </asp:GridView>
       
    </div>
            </center>
    </form>
</body>
</html>




Default.aspx.cs:-

  

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Text;

public partial class Default3 : System.Web.UI.Page
{
    
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
          
            loadDataTable();
        }

    }
     private void loadDataTable()
    {
        DataSet ds = new DataSet();
        DataTable dt;
        DataRow dr;
        DataColumn pName;
        DataColumn pQty;
        DataColumn pPrice;
        int i = 0;
        dt = new DataTable();
        pName = new DataColumn("Product_Name", Type.GetType("System.String"));
        pQty = new DataColumn("Quantity", Type.GetType("System.Int32"));
        pPrice = new DataColumn("Price", Type.GetType("System.Int32"));
        dt.Columns.Add(pName);
        dt.Columns.Add(pQty);
        dt.Columns.Add(pPrice);
        dr = dt.NewRow();
        dr["Product_Name"] = "Book";
        dr["Quantity"] = 2;
        dr["Price"] = 200;
        dt.Rows.Add(dr);
        dr = dt.NewRow();
        dr["Product_Name"] = "Pen";
        dr["Quantity"] = 5;
        dr["Price"] = 480;
        dt.Rows.Add(dr);
        dr = dt.NewRow();
        dr["Product_Name"] = "Bike";
        dr["Quantity"] = 8;
        dr["Price"] = 1000000;
        dt.Rows.Add(dr);
        dr = dt.NewRow();
        dr["Product_Name"] = "Car";
        dr["Quantity"] = 2;
        dr["Price"] = 5000000;
        dt.Rows.Add(dr);
        ds.Tables.Add(dt);

        Gridview1.DataSource = ds.Tables[0];
        Gridview1.DataBind();
    }
}