Click here to Skip to main content
15,610,588 members
Please Sign up or sign in to vote.
4.50/5 (2 votes)
See more:
public partial class AddMaterial : System.Web.UI.Page
        DataTable dt = new DataTable();
        DataColumn dc = new DataColumn();
protected void Button1_Click(object sender, EventArgs e)
       if (dt.Columns.Count == 0)
            dt.Columns.Add("Category", typeof(string));
            dt.Columns.Add("Mtl", typeof(string));

        DataRow NewRow = dt.NewRow();
        NewRow[0] = TextBox1.Text;
        NewRow [1]=TextBox2.Text;
        GridView1.DataSource = dt;


On each button-click each row to be added to the grid-view.
(ie. if user clicked 3times there should be 3rows).
The above code always showing 1 row even after multiple click.
Please provide corrected solution..

[Improved readability. Corrected textspeak.]
Updated 12-Apr-11 20:33pm
Saurabh Singh Pratapgarh 3-Sep-11 3:51am    

Since this is a web application, you need to store your dt to a Session variable in order to persist its value between postbacks. See example below.

To retrieve
if(Session["dataTable"] != null)
    dt = (DataTable)Session["dataTable"];

To Store
Session["dataTable"] = dt;
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900