Umer Shehzad
Umer Shehzad

Reputation: 53

How to insert data from Textboxes into GridView on button click in ASP.NET

I want to get the data from textboxes, and on button click I want that data to be inserted into GridView. On every click there should be a new row created, and old row must not be removed. Whenever I enter the new data and click on button, my old row is deleted and new row is stored in place of it. Here is my code:

DataTable dt1 = new DataTable();
bool flag = false;  

private void gridVIEWData()
{
   dt1.Columns.Add("pName", typeof(string));
   dt1.Columns.Add("pCategory", typeof(string));
   dt1.Columns.Add("price", typeof(string));
   dt1.Columns.Add("pQuantity", typeof(string));
   dt1.Columns.Add("totalPrice", typeof(string));
}
protected void Button3_Click(object sender, EventArgs e)
{
    if (!flag)
    {
        gridVIEWData();
        flag = true;
        Int32 total = Convert.ToInt32(txt_quantity.Text) * Convert.ToInt32(txt_price.Text);
        DataRow dr = dt1.NewRow();
        dr["pName"] = DropDownList2.SelectedItem;
        dr["pCategory"] = DropDownList1.SelectedItem;
        dr["price"] = txt_price.Text;
        dr["pQuantity"] = txt_quantity.Text;
        dr["totalPrice"] = total;
        dt1.Rows.Add(dr);

        GridView1.DataSource = dt1;
        GridView1.DataBind();
    }
    else if (!IsPostBack)
    {
        Int32 total = Convert.ToInt32(txt_quantity.Text) * Convert.ToInt32(txt_price.Text);
        DataRow dr = dt1.NewRow();
        dr["pName"] = DropDownList2.SelectedItem;
        dr["pCategory"] = DropDownList1.SelectedItem;
        dr["price"] = txt_price.Text;
        dr["pQuantity"] = txt_quantity.Text;
        dr["totalPrice"] = total;
        dt1.Rows.Add(dr);

        GridView1.DataSource = dt1;
        GridView1.DataBind();
    }
}

Upvotes: 5

Views: 25441

Answers (2)

Amit
Amit

Reputation: 22086

This is because you are calling gridVIEWData(); every time on postback that create table columns again.

Try this

DataTable dt1 = new DataTable();

private void gridVIEWData() {
    dt1.Columns.Add("pName", typeof(string));
    dt1.Columns.Add("pCategory", typeof(string));
    dt1.Columns.Add("price", typeof(string));
    dt1.Columns.Add("pQuantity", typeof(string));
    dt1.Columns.Add("totalPrice", typeof(string));
}

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        gridVIEWData();
        GridView1.DataSource = dt1;
        GridView1.DataBind();
        Session["dt1"]=dt1;
    }
}

protected void Button3_Click(object sender, EventArgs e)
{
    if(Session["dt1"]!=null) dt1 = (DataTable) Session["dt1"];
    Int32 total = Convert.ToInt32(txt_quantity.Text) * Convert.ToInt32(txt_price.Text);
    DataRow dr = dt1.NewRow();
    dr["pName"] = DropDownList2.SelectedItem.Text;
    dr["pCategory"] = DropDownList1.SelectedItem.Text;
    dr["price"] = txt_price.Text;
    dr["pQuantity"] = txt_quantity.Text;
    dr["totalPrice"] = total.ToString();
    dt1.Rows.Add(dr);

    GridView1.DataSource = dt1;
    GridView1.DataBind();
}

Upvotes: 1

Adil
Adil

Reputation: 148180

The old data is removed as you bind it with new data. You have to keep the old data in the data source which data table in your case. Usually we have persistent medium for storing data like database of files.

For your understanding I will store datatable in Session but it is usually not practised, you should store in database or what ever medium you like.

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        gridVIEWData();
        GridView1.DataSource = dt1;
        GridView1.DataBind();
    }
}

private void gridVIEWData() {
   dt1.Columns.Add("pName", typeof(string));
   dt1.Columns.Add("pCategory", typeof(string));
   dt1.Columns.Add("price", typeof(string));
   dt1.Columns.Add("pQuantity", typeof(string));
   dt1.Columns.Add("totalPrice", typeof(string));
   Session["dtInSession"] = dt1;     //Saving Datatable To Session 
}


protected void Button3_Click(object sender, EventArgs e)
{
        if(Session["dtInSession"] != null)
             dt1 = (DataTable)Session["dtInSession"]; //Getting datatable from session 

        Int32 total = Convert.ToInt32(txt_quantity.Text) * Convert.ToInt32(txt_price.Text);
        DataRow dr = dt1.NewRow();
        dr["pName"] = DropDownList2.SelectedItem;
        dr["pCategory"] = DropDownList1.SelectedItem;
        dr["price"] = txt_price.Text;
        dr["pQuantity"] = txt_quantity.Text;
        dr["totalPrice"] = total;
        dt1.Rows.Add(dr);

        Session["dtInSession"] = dt1;     //Saving Datatable To Session 
        GridView1.DataSource = dt1;
        GridView1.DataBind();

    }
 }

Upvotes: 2

Related Questions