Saturday, April 27, 2013

delete all gridview rows using checkbox in asp.net

Title:
How to delete rows in grid view using check box in asp.net

Introduction:
Hi all,I think you have seen so many articles and websites on grid view functionalists.But why i have given again the same delete records in grid view?".This question should get every one when you reading the title.The answer is,I would like to share the code or logic what i have done earlier in easy manner.Let go to the our task.I hope the below explanation is useful for us.Thanks to reading..

Example:
Some previous articles
select the multiple rows of grid view,
Get the check box in grid view using Jquery.
In this post i will show how to delete the all rows from grid view using check box in grid view.The design page have the grid view layout with item templates and and check which is used to select the specified rows in grid view to make functionality better
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Delete All Rows in grid view with check box in asp.net</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GvOrder" runat="server" AutoGenerateColumns="False" CellPadding="4" GridLines="None" >
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="chk" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Order NO.">
<ItemTemplate>
<asp:Label ID="lb1OrderId" Text='<%#Eval("OrdrNo") %>' runat="server" />
</ItemTemplate>
</asp:TemplateField><asp:TemplateField HeaderText="OrderName Name">
<ItemTemplate>
<%#Eval("Ordrname") %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Amount">
<ItemTemplate>
<%#Eval("Amtsal") %>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
<asp:Button ID="btnchkall" runat="server" OnClick="btnchk_Click">Check All</asp:Button><asp:Button ID="btnDelete" runat="server" OnClick="btnDelete_Click">Delete</asp:Button>
</form>
</body>
</html>
Code behind:
The connection string settings in the config file has been made as per in the below snippet.It contain server and data base name.Before going to add this code we have to create a connection to database in web.config file. Here my connection name is "OrderDetailsConnection".

<appSettings>
<connectionStrings>
<add key="OrderDetailsConnection" value="server=bhaskar/SQLEXPRESS;database=Order_Db;uid=bhaskar;password=br123;" />
</connectionStrings>
</appSettings>

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;

public partial class _GrdivewChkDelete : System.Web.UI.Page 
{
SqlConnection cn = new SqlConnection(ConfigurationSettings.AppSettings("OrderDetailsConnection");
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
BindOrdrGrid();
}
private void BindOrdrGrid()
{
SqlDataAdapter OrdrAd = new SqlDataAdapter("Select * from Order",ocn);
DataSet Ordrds = new DataSet();
OrdrAd.Fill(Ordrds);
GvOrder.DataSource = Ordrds;
GvOrder.DataBind();
}
protected void btnchkall_Click(object sender, EventArgs e)
{
CheckBox rchk;
for (int i = 0; i < GvOrder.Rows.Count; i++)
{
rchk = ((CheckBox)(GvOrder.Rows[i].FindControl("orbrcb")));
if (orbrcb.Checked == false)
//Check all check box in gridview
orbrcb.Checked = true;
}
}
protected void btnDelete_Click(object sender, EventArgs e)
{
CheckBox rchk;
String ordrId = String.Empty;
for (int i = 0; i < GvOrder.Rows.Count; i++)
{
rchk = ((CheckBox)(GvOrder.Rows[i].FindControl("orbrcb")));
if (orbrcb.Checked){//Get the Id from lable
ordrId +=((Label)(GvOrder.Rows[i].FindControl("lb1OrderId"))).Text + ",";
}
}//Get the Order_Id
ordrId = ordrId.Substring(0, ordrId.Length - 1);
cn.Open();
//Delete all grdivew records
SqlCommand cmd=new SqlCommand("delete from Order where Order_Id in("+ordrId.ToString()+")",cn);
cmd.ExecuteNonQuery();
BindOrdrGrid();
}
}
//In the above code i have used to delete the records using "IN"  in sql command.By using this we can delete the all records with in the list.Keep coming to get latest updates 
Post a Comment

Bel