Delete multiple rows records in Gridview with checkbox and confirmation |
Several times while developing web applications we need to implement functionality to delete bulk / multiple rows or records in Gridview using checkbox to select rows / records to be deleted with confirmation dialog box.In this example we are showing how to achieve it. |
Html markup of the gridview is like |
|
|
<asp:GridView ID="GridView1" runat="server"
AutoGenerateColumns="False"
DataSourceID="SqlDataSource1"
ShowFooter="true">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="chkSelect" runat="server" />
</ItemTemplate>
<FooterTemplate>
<asp:Button ID="btnDelete" runat="server" Text="Delete"
OnClick="btnDelete_Click"
OnClientClick="return DeleteConfirmation();"/>
</FooterTemplate>
</asp:TemplateField>
<asp:BoundField DataField="ID"
HeaderText="ID"
SortExpression="ID" />
<asp:BoundField DataField="Name"
HeaderText="Name"
SortExpression="Name" />
<asp:BoundField DataField="Location"
HeaderText="Location"
SortExpression="Location" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT [ID], [Name], [Location]
FROM [Details]"></asp:SqlDataSource> |
The JavaScript for delete confirmation of records is |
|
<script type="text/javascript" language="javascript">
function DeleteConfirmation()
{
if (confirm("Are you sure,"
"you want to delete selected records ?")==true)
return true;
else
return false;
}
</script> |
| Call this javascript by assigning on OnClientClick attribute to delete button in html markup like this |
|
<FooterTemplate>
<asp:Button ID="btnDelete" runat="server"
Text="Delete"
OnClick="btnDelete_Click"
OnClientClick="return DeleteConfirmation();"/>
</FooterTemplate> |
| In the code behind we are using StringCollection to store ID of records to be deleted by looping through gridview rows and checking which row is selected. |
C# code behind |
public partial class _Default : System.Web.UI.Page
{
//Define global Connection String
string strConnection = ConfigurationManager.ConnectionStrings
["ConnectionString"].ConnectionString;
protected void Page_Load(object sender, EventArgs e)
{
}
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();
}
/// <summary>
/// This is to Delete multiple records in gridview
/// </summary>
/// <param name="idCollection">stringCollection</param>
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.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();
}
}
} |