Wednesday, 25 June 2014

Adding items to datatable dynamically

No comments
Adding rows in datatable
     
        DataTable dt = new DataTable();
        if (Session["data"] != null)
                {
                    dt = (DataTable)Session["data"];
                }
                else
                {
                    DataColumn proid = new DataColumn("p_id");
                    DataColumn Product_id = new DataColumn("Product");                 

                    dt.Columns.Add(proid);
                    dt.Columns.Add(Product);                
                }
                DataRow dr = dt.NewRow();
                dr["p_id"] = productid.Text;
                dr["Product"] = pid.Text;                            
                dt.Rows.Add(dr);
                Session["data"] = dt;

Updating data in datatable

 protected void Edit_Click(object sender, EventArgs e)
     {      
        Button im = (Button)sender;
        GridViewRow gr = (GridViewRow)im.NamingContainer;
        DataTable dt = (DataTable)Session["myDt"];
        DataRow dr = dt.Rows[gr.RowIndex];
        Session["dr"] = dr;
        Label lblpro = (Label)GridView1.Rows[gr.RowIndex].FindControl("lblpronm");
        texttBox1.text=lblpro.text;
        }   

  protected void Update_Click(object sender, EventArgs e)
     {    
            DataTable dt = (DataTable)Session["myDt"];
            DataRow dr = (DataRow)Session["dr"];
            dr["Proname"] = lblpro.text;         
            GridView1.DataSource = dt;
            GridView1.DataBind();
      }

Deleting rows from datatable

 protected void Delete_Click(object sender, ImageClickEventArgs e)
    {
        ImageButton img = (ImageButton)sender;
        GridViewRow gr = (GridViewRow)img.NamingContainer;
        DataTable dt = (DataTable)Session["data"];
        DataRow dr = dt.Rows[gr.RowIndex];
        dr.Delete();
        GridView1.DataSource = dt;
        GridView1.DataBind();
      
    }

Inserting data from datatable to database

protected void btn_checkout_Click(object sender, EventArgs e)
    {
 DataTable dt = (DataTable)Session["data"];
for (int j = 0; j < dt.Rows.Count; j++)
        {
            dl.p_id = Convert.ToInt16(dt.Rows[j]["p_id"]);
            dl.products = Convert.ToInt16(dt.Rows[j]["Product"]);
            bl.insert_order(dl);                                             
        }