Sunday, December 14, 2014

Delete Multiple Rows in Gridview using Checkbox in Asp.net

Here I will explain how to delete multiple rows in gridview using checkbox in asp.net in c# with confirmation message box.

Before use this example first design one table UserData in your database as shown below.

Column Name
Data Type
Allow Nulls
UserID
Int(IDENTITY=TRUE)
No
UserName
varchar(100)
Yes
FirstName
varchar(100)
Yes
LastName
varchar(100)
Yes

Once above table created in database then enter some data to test application after that write the following code in your aspx page.

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>delete Multiple Rows from gridview in asp.net using checkbox</title>
<script type="text/javascript">
function Confirmation() {
var result = confirm('Are you sure you want to delete selected User(s)?');
if (result) {
return true;
}
else {
return false;
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="gvUserData" DataKeyNames="UserID" AutoGenerateColumns="false" CellPadding="5" runat="server">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="chkSelect" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField HeaderText="UserID" DataField="UserID" />
<asp:BoundField HeaderText="UserName" DataField="UserName" />
<asp:BoundField HeaderText="FirstName" DataField="FirstName" />
<asp:BoundField HeaderText="LastName" DataField="LastName" />
</Columns>
<HeaderStyle BackColor="#df5015" Font-Bold="true" ForeColor="White" />
</asp:GridView>
<asp:Button ID="btnDelete" Text="Delete Records" runat="server" Font-Bold="true" OnClientClick="javascript:return Confirmation();" onclick="btnDelete_Click" />
</div>
</form>
</body>
</html>

Now goto code behind file(press F7 key) and write the following code

using System;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class DeleteRows : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindUserData();
}
}
protected void BindUserData()
{
DataTable dt = new DataTable();

SqlConnection con = new SqlConnection("Data Source=vibhavdb;Integrated Security=true;Initial Catalog=SampleDB");
con.Open();
SqlCommand cmd = new SqlCommand("Select * from UserData", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
con.Close();
gvUserData.DataSource = dt;
gvUserData.DataBind();
}
protected void btnDelete_Click(object sender, EventArgs e)
{
foreach (GridViewRow gvrow in gvUserData.Rows)
{
//Finiding checkbox control in gridview for particular row
CheckBox chkdelete = (CheckBox)gvrow.FindControl("chkSelect");
//Condition to check checkbox selected or not
if (chkdelete.Checked)
{
//Getting UserId of particular row using datakey value
int usrid = Convert.ToInt32(gvUserData.DataKeys[gvrow.RowIndex].Value);

SqlConnection con = new SqlConnection("Data Source=vibhavdb;Integrated Security=true;Initial Catalog=SampleDB");
con.Open();
SqlCommand cmd = new SqlCommand("delete from UserData where UserID=" + usrid, con);
cmd.ExecuteNonQuery();
con.Close();
}
}
BindUserData();
}
}

No comments:

Post a Comment