Click here to Skip to main content
15,881,424 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi i have been stuck on that problem for last 2 days. i want to get the data from textboxes and on button click i want that data to be inserted in gridview. on every click there shud be a new row created and old row must not be removed. here is my code. whenever i enter the new data and click on button my old row is deleted and new row is stored in place of it. Kindly help me with my that problem thanx.


C#
        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 GridView1_SelectedIndexChanged(object sender, EventArgs e)
       {

       }

       protected void TextBox4_TextChanged(object sender, EventArgs e)
       {

       }

       protected void txt_productCode_TextChanged(object sender, EventArgs e)
       {

       }

       protected void GridView1_SelectedIndexChanged1(object sender, EventArgs e)
       {

       }

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();
            }
            
            //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();
            //}
            
        }
    } 
Posted

thanks alot buddy. i have solved it myself. now i m getting problem in deleting a row from it.can anyone help.
 
Share this answer
 
Why this problem happens is Asp.Net cannot persist the changes in DataTable you created between different requested. You need to write code to persist these changes. One solution is to store the data table in a Session variable after adding the new row. Also make sure to retrieve the same data table from this session before adding the next row.

The code will look like this;
C#
DataTable dt1=new DataTable();
       protected void Page_Load(object sender, EventArgs e)
       {
           if (!IsPostBack)
           {
               Session["a"] = null;
           }
       }

C#
protected void Button3_Click(object sender, EventArgs e)
{
if  Session["a"]==null)
            {
                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);
 Session["datas"]=dt1;
                GridView1.DataSource = dt1;
                GridView1.DataBind();
            }
            else             {
                Int32 total = Convert.ToInt32(txt_quantity.Text) * Convert.ToInt32(txt_price.Text);
DataTable dtcached = (DataTable)Session["a"];
                DataRow dr = dtcached.NewRow();
                dr["pName"] = DropDownList2.SelectedItem;
                dr["pCategory"] = DropDownList1.SelectedItem;
                dr["price"] = txt_price.Text;
                dr["pQuantity"] = txt_quantity.Text;
                dr["totalPrice"] = total;
DataTable dtCached=(DataTable)Session["datas"];
                dtCached.Rows.Add(dr);
 
                GridView1.DataSource = dtCached;                GridView1.DataBind();
            }
}

Changed code I have shown it in BOLD, Hope this helps.. It is not good to use lot of session variable as it adds more loads to server, but it gives you a hint
 
Share this answer
 
v2

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