How add the rows dynamically in datatable asp.net

 protected void btnSubmitDetails_Click(object sender, EventArgs e)
        {
            if (Session["dt"] != null)
            {
                dt = (DataTable)Session["dt"];
                DataRow dr1 = dt.NewRow();
                dr1["description"] = txtDescription.Text.Trim();

                dr1["Quantity"] = txtQuantity.Text.Trim();
                dr1["Price"] = txtPrice.Text.Trim();
                dr1["total"] = txtTotal.Text.Trim();
                txtDescription.Text = "";
                txtQuantity.Text = "";
                txtPrice.Text = "";
                txtTotal.Text = "";
                dt.Rows.Add(dr1);
              
                Session["dt"] = dt;
                litMsg.Text = "";
            }
            BindGridView();
        }

Bind the Gridview with datatable, check datatable exists in session or not.

   public void BindGridView()
        {
            if (Session["dt"] != null)
            {
                dt = (DataTable)Session["dt"];
                if (dt.Rows.Count > 0)
                {
                    if (dt.Rows.Count > 0)
                    {
                        int RecordCount = dt.Rows.Count;

                        for (int i = 0; i < RecordCount; i++)
                        {
                            dt.Rows[i]["SrNumber"] = i+1;
                        }
                    }
                    GridView1.DataSource = dt;
                    GridView1.DataBind();
                }
                else
                {
                    
                    createTable();
                    GridView1.DataSource = dt;
                    GridView1.DataBind();

                }
            }
            else
            {
               
                createTable();
                GridView1.DataSource = dt;
                GridView1.DataBind();
           
            }
         
        }

To create the DataTable dynamically www.infoa2z.com/asp.net/create-datatable-dynamically-in-asp.net

Related Alrticles

FREE!!! Registration