Deleting from GridView using checkbox selections (C#)

Discussion in 'ASP.NET' started by Jymoz, Aug 17, 2011.

  1. Jymoz

    Jymoz New Member

    Joined:
    Aug 17, 2011
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    I have a gridview (just called GridView1) containing a selection of tasks that the user can add to and edit etc. I've added in checkboxes so that the user can select multiple rows at once and the hope is that they can then click on a button to delete those rows.

    I picked up some code on the internet that allows me to go through the check boxes, see which ones are checked and then return to a label which tasks would be deleted. It didn't provide the code for actually deleting them however and i havent been able to find out how to do it, so i was hoping someone could help me.

    My code is below:

    Code:
     
    protected void DeleteButton_Click(object sender, GridViewDeleteEventArgs e)
       {
           bool AtLeastOneRowDeleted = false;
     
           foreach (GridViewRow row in GridView1.Rows)
                {
                  CheckBox cb = (CheckBox)row.FindControl("chk1");
                  if (cb != null && cb.Checked)
                      {
                         AtLeastOneRowDeleted = true;
                        string TaskName = Convert.ToString(GridView1.DataKeys[row.RowIndex].Value);
     
                         DeleteResults.Text += string.Format("This would have deleted task - {0} <br />", TaskName);
     
                         DeleteResults.Visible = AtLeastOneRowDeleted;
                       }
                  }
    }
    
    I'm linking the gridview by a SQLDataSource linked to a SQL Server 2008 database. I'll probably have to link to it to delete it from there but when i've started to try that they come back with immediate errors.

    Any help would be greatly appreciated
     
  2. Christopherwilde

    Christopherwilde New Member

    Joined:
    Nov 10, 2012
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Nice try and very interesting topic you can use code:
    Code:
    Using System.Data.SqlClient;
    using System.Collections.Specialized;
    using System.Text;
    
    Here’s the code block for deleting multiple records
    
    #region Multiple Delete
    private void DeleteRecords(StringCollection sc)
    {
       SqlConnection conn = new SqlConnection(GetConnectionString());
       StringBuilder sb = new StringBuilder(string.Empty);
     
       foreach (string item in sc)
       {
         const string sqlStatement = "DELETE FROM Customers WHERE CustomerID";
         sb.AppendFormat("{0}='{1}'; ",sqlStatement, item);
       }
       try
       {
                conn.Open();
                SqlCommand cmd = new SqlCommand(sb.ToString(), conn);
                cmd.CommandType = CommandType.Text;
                cmd.ExecuteNonQuery();
        }
        catch (System.Data.SqlClient.SqlException ex)
        {
                string msg = "Deletion Error:";
                msg += ex.Message;
                throw new Exception(msg);
         }
         finally
         {
                conn.Close();
         }
    }
     
    Last edited by a moderator: Nov 11, 2012
  3. alia123

    alia123 New Member

    Joined:
    Jan 8, 2016
    Messages:
    65
    Likes Received:
    5
    Trophy Points:
    0

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice