Click here to Skip to main content
15,891,951 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i want to use checkbox in gridview.then i select this checkbox,press the delete button ,the selected row should be deleted..advance thanks..
Posted

Hi,

Try this if could help...

C#
 //instantiate new ArrayList to hold our checked items
 ArrayList checkedItems = new ArrayList();
 CheckBox chk;
 string chkBoxIndex = string.Empty;
 //loop through each row in the GridView
 foreach (GridViewRow row in GridContacts.Rows)
 {
    //get the index of the current CheckBox
    chkBoxIndex = (string)GridContacts.DataKeys[row.RowIndex].Value.ToString();
    chk = (CheckBox)row.FindControl("CheckBox1");
    if (!(Session["CheckedItems"] == null))
    {
      checkedItems = (ArrayList)Session["CheckedItems"];
    }
    if (chk.Checked)
    {
      if (!(checkedItems.Contains(chkBoxIndex)))
      {
         //add to the list
         checkedItems.Add(chkBoxIndex);
      }
      else
      {
         //remove from list since it's unchecked
         checkedItems.Remove(chkBoxIndex);
      }
    }
}
if (checkedItems.Count > 0)
{
   for (int i = 0; i < checkedItems.Count; i++)
   {
       // Your code to delete the items in you database here...
       //...
   }
}
/pre>


Regards,
 
Share this answer
 
v2
I think this post will help you much: GridView all in one[^]
 
Share this answer
 

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