Wednesday 16 November 2011

checkbox added in gridview

. right click on your DataGrid -->Property Builder -->Columns --> Template Column --> Apply --> Ok.
2. Right click again --> EidtTemplate --> choose Column you add --> drag CheckBox control from Toolbox and drop it in ItemTemplate field and if you need CheckBox in edit mode put another CheckBox control in EditItemTemplate field --> End Template Editing.

Code on the button:

  1. private void btn_Delete_Click(object sender, System.EventArgs e)
  2. {
  3. for(int i=0;i<dg_EditProduct.Items.Count;i++)
  4. {
  5. CheckBox chk = new CheckBox();
  6. chk = (CheckBox)dg_EditProduct.Items[i].FindControl("chk_Delete");
  7. if(chk.Checked)
  8. {
  9. DataTable myTable;
  10. myTable=(DataTable)Session["ProductTable"];
  11. int ID_Delete = Convert.ToInt32( myTable.Rows[i]["ProductID"]);
  12. Str_Conn = ConfigurationSettings.AppSettings["Fashion"];
  13. MyConn = new SqlConnection(Str_Conn);
  14. //Command to delete Product
  15. // SqlCommand cmd_DeleteProduct = new SqlCommand("dbo.DeleteProduct",MyConn);
  16. SqlCommand cmd_DeleteProduct = new SqlCommand("DeleteProduct",MyConn);
  17. cmd_DeleteProduct.CommandType = CommandType.StoredProcedure;
  18. SqlParameter ProductID_Delete = cmd_DeleteProduct.Parameters.Add("@ProductID",SqlDbType.Int);
  19. ProductID_Delete.Value = ID_Delete ;
  20. MyConn.Open();
  21. dg_EditProduct.DataSource = myTable;
  22. cmd_DeleteProduct.ExecuteNonQuery();
  23. MyConn.Close();
  24. }
  25. }
  26. BindData(); // it is the function you use to bind data to datagrid on page load
  27. }

No comments:

Post a Comment