Gridview without database

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

gridview without databse

Here the program create a DataTable and create the data structure.

gridview from datatable

After creating the data structure, it adds rows in the DataTable.

dynamic gridview , gridviwe from data structures

Finally the DataTable add in the Dataset and make this dataset as GridView's DataSource.

ds.Tables.Add(dt); GridView1.DataSource = ds.Tables[0]; GridView1.DataBind();

Default.aspx

<!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 runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <asp:GridView ID="GridView1" runat="server"> </asp:GridView> </div> </form> </body> </html>

C# Source Code

using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Data; using System.Data.SqlClient; public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { 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"] = "Product 1"; dr["Quantity"] = 2; dr["Price"] = 200; dt.Rows.Add(dr); dr = dt.NewRow(); dr["Product_Name"] = "Product 2"; dr["Quantity"] = 5; dr["Price"] = 480; dt.Rows.Add(dr); dr = dt.NewRow(); dr["Product_Name"] = "Product 3"; dr["Quantity"] = 8; dr["Price"] = 100; dt.Rows.Add(dr); dr = dt.NewRow(); dr["Product_Name"] = "Product 4"; dr["Quantity"] = 2; dr["Price"] = 500; dt.Rows.Add(dr); ds.Tables.Add(dt); GridView1.DataSource = ds.Tables[0]; GridView1.DataBind(); } }

VB.Net Source Code

Imports System.Drawing Imports System.Data.SqlClient Imports System.Data Partial Class _Default Inherits System.Web.UI.Page Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load loadDataTable() End Sub Private Sub loadDataTable() Dim ds As New DataSet() Dim dt As DataTable Dim dr As DataRow Dim pName As DataColumn Dim pQty As DataColumn Dim pPrice As DataColumn Dim i As Integer = 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") = "Product 1" dr("Quantity") = 2 dr("Price") = 200 dt.Rows.Add(dr) dr = dt.NewRow() dr("Product_Name") = "Product 2" dr("Quantity") = 5 dr("Price") = 480 dt.Rows.Add(dr) dr = dt.NewRow() dr("Product_Name") = "Product 3" dr("Quantity") = 8 dr("Price") = 100 dt.Rows.Add(dr) dr = dt.NewRow() dr("Product_Name") = "Product 4" dr("Quantity") = 2 dr("Price") = 500 dt.Rows.Add(dr) ds.Tables.Add(dt) GridView1.DataSource = ds.Tables(0) GridView1.DataBind() End Sub End Class