Click here to Skip to main content
15,910,212 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello friends,

how to select multiple row in gridview using checkbox and delete multiple record.
give me some example..
Posted
Updated 30-May-11 1:13am
v3

 
Share this answer
 
Hi...
You can select the checked rows in a grid view as,
for(int i=0;i<gridview1.rows.count;i++)
{
   //Find checkbox
   CheckBox Chk=(CheckBox)Gridview1.Rows[i].FindControl("ChkSelect");
   if(Chk.checked)
   {
     //Select the corresponding rows details
     //delete query write here
   }
}
 
Share this answer
 
v3
C#
protected void btnDelete_Click(object sender, EventArgs e)
{
//Create String Collection to store 
//IDs of records to be deleted 
  StringCollection idCollection = new StringCollection();
  string strID = string.Empty;

  //Loop through GridView rows to find checked rows 
   for (int i = 0; i < GridView1.Rows.Count; i++)
   {
    CheckBox chkDelete = (CheckBox)
       GridView1.Rows[i].Cells[0].FindControl("chkSelect");
            if (chkDelete != null)
            {
                if (chkDelete.Checked)
                {
                 strID = GridView1.Rows[i].Cells[1].Text;
                 idCollection.Add(strID);
                }
            }
        }

        //Call the method to Delete records 
        DeleteMultipleRecords(idCollection);

        // rebind the GridView
        GridView1.DataBind();
    }
private void DeleteMultipleRecords
                            (StringCollection idCollection)
{
//Create sql Connection and Sql Command
 SqlConnection con = new SqlConnection(strConnection);
 SqlCommand cmd = new SqlCommand();
 string IDs = "";

foreach (string id in idCollection)
{
  IDs += id.ToString() + ",";
}
          try
            {
               string strIDs = 
                IDs.Substring(0, IDs.LastIndexOf(","));
                   string strSql = "Delete from Details 
                           WHERE ID in (" + strIDs + ")";
                cmd.CommandType = CommandType.Text;
                cmd.CommandText = strSql;
                cmd.Connection = con;
                con.Open();
                cmd.ExecuteNonQuery();
            }
            catch (SqlException ex)
            {
                string errorMsg = "Error in Deletion";
                errorMsg += ex.Message;
                throw new Exception(errorMsg);
            }
            finally
            {
                con.Close();
            }
}
 
Share this answer
 
v2
hiiii


Using for loop or foreach
for(int i=1 i<=dtbl.rows.count;i++)
{
write the code here
}
 
Share this answer
 
Comments
Manfred Rudolf Bihy 12-Aug-11 11:04am    
Answering questions that already have very good answers with such rubbish will be punished.

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