Insert data in Grid View without using database

Insert the data from the textbox into the Grid View without using any database  in ASP. NET using C#.

Step 1: To do this firstly open the Microsoft Visual Studio 2010 then click the File->New->Web Site->select the ASP. NET website.

1.jpg

Step 2: The design window get appears and create a Grid View control and  textbox control and  button control. And set the property of controls by using the property window.

2.jpg

3.jpg

Step 3:  Now double click on the button control to write the source code of its click event.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
            dt = new DataTable();
            DataColumn dc1 = new DataColumn("NAME");
            DataColumn dc2 = new DataColumn("ADDRESS");
            dt.Columns.Add(dc1);
            dt.Columns.Add(dc2);
            DataRow dr1 = dt.NewRow();
            GridView1.DataSource = dt;
            GridView1.DataBind();
    }
    DataTable dt;
    protected void Button1_Click(object sender, EventArgs e)
    {
        DataRow dr1 = dt.NewRow();
        dr1[0] = TextBox1.Text;
        dr1[1] = TextBox2.Text;
        dt.Rows.Add(dr1); 
        GridView1.DataSource = dt;
        GridView1.DataBind();
    }

}

Step 4: Run the application by pressing F5 keys and insert the value in the textboxes then click on the button.

The Output is as Follows:

4.jpg

5.jpg