Search code examples
c#asp.netvisual-studio-2010sql-server-2005web-applications

How to display values entered via a TextBox in a GridView on button-click without saving the data to the database?


I have created two TextBoxes to enter the FirstName and LastName of an employee and a button in a web-based ASP.NET application using C# on Visual studio 2010.when I click on the button , the values that I enter in the TextBoxes should be displayed in a Gridview without being stored in the database.

How can I do that? Can you provide a sample code to execute the above mentioned functionality?


Solution

  • This is a working code..

    protected void Page_Load(object sender, EventArgs e)
    {
            dt = new DataTable();
            DataColumn dc1 = new DataColumn("FIRST NAME");
            DataColumn dc2 = new DataColumn("LAST NAME");
            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();
    }