Monday, 9 July 2012

How to delete checked row form Grid View in Asp.Net…………

How to delete checked row form Grid View in Asp.Net…………

// execute this query in sql server……………………………..

create database College
use college
create table Student(RollNo int primary key,Name nvarchar(50) ,EmailId nvarchar(50) unique,Course nvarchar(50))
insert into Student values (1,'Somesh Katiyar','someshkatiyar@gmail.com','B.Tech')

// Source code for DeleteGridView.aspx ………………………………………………

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="DeleteGridView.aspx.cs" Inherits="DeleteGridView" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type="text/css">
.style1
{
height: 23px;
}
.style2
{
width: 165px;
}
.style3
{
height: 23px;
width: 165px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>

 <table width="100%">
<tr>
<td colspan="2">
<asp:Label ID="Label1" runat="server"
Text="Delete Row in Grid View with selected Check Box" Font-Italic="True"
ForeColor="Red"></asp:Label>
</td>
</tr>
<tr>
<td class="style2">
RollNo</td>
<td>
<asp:TextBox ID="txtrollno" runat="server"
></asp:TextBox>
</td>
</tr>
<tr>
<td class="style2">
Name</td>
<td>
<asp:TextBox ID="txtname" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td class="style2">
EmailId</td>
<td>
<asp:TextBox ID="txtemailid" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td class="style2">
Course</td>
<td>
<asp:DropDownList ID="ddlcourse" runat="server">
<asp:ListItem Selected="True">--Select--</asp:ListItem>
<asp:ListItem>M C A</asp:ListItem>
<asp:ListItem>B C A</asp:ListItem>
<asp:ListItem>B. Tech</asp:ListItem>
<asp:ListItem>M. Tech</asp:ListItem>
<asp:ListItem>B. E.</asp:ListItem>
<asp:ListItem></asp:ListItem>
</asp:DropDownList>
</td>
</tr>
<tr>
<td class="style2">
&nbsp;</td>
<td>
<asp:Button ID="btnSave" runat="server" onclick="btnSave_Click" Text="Save" />
</td>
</tr>
<tr>
<td class="style1" colspan="2">
<asp:GridView ID="GridView2" runat="server"  ShowFooter="True">
<Columns>
<asp:TemplateField HeaderText="All">
<FooterTemplate>
<asp:LinkButton ID="linkButtonDelete" runat="server"
onclick="linkButtonDelete_Click">Delete </asp:LinkButton>
</FooterTemplate>
<HeaderTemplate>
<asp:CheckBox ID="ChkAll" runat="server" AutoPostBack="True"
oncheckedchanged="ChkAll_CheckedChanged1" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server"  />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<EmptyDataTemplate>
There is no any Data
</EmptyDataTemplate>
</asp:GridView>
</td>
</tr>
<tr>
<td class="style3">
&nbsp;</td>
<td class="style1">
<asp:Label ID="lblmessage" runat="server" Text="Message" Visible="False"></asp:Label>
</td>
</tr>
<tr>
<td colspan="2">

</td>
</tr>
</table>



</div>
</form>
</body>
</html>


// Source code for DeleteGridView.aspx.cs ………………………………………………

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;
using System.Data.SqlClient;

public partial class DeleteGridView : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostBack)
GridViewBind();
}
DataSet1TableAdapters.StudentTableAdapter st = new DataSet1TableAdapters.StudentTableAdapter();
// Insert into table with DataSet Control..............................
protected void btnSave_Click(object sender, EventArgs e)
{
try
{
st.Insert(Convert.ToInt32(txtrollno.Text), txtname.Text, txtemailid.Text, ddlcourse.SelectedValue);
lblmessage.Visible = true;
lblmessage.ForeColor = Color.Green;
GridViewBind();
TextClear();
lblmessage.Text = "Add Successfully";
}
catch (Exception ex)
{
lblmessage.Visible = true;
lblmessage.ForeColor = Color.Red;
lblmessage.Text = ex.Message;
}

}

//bind GridView with DataSet Control...................
private void  GridViewBind()
{


DataSet1.StudentDataTable dt=st.GetData();
GridView2.DataSource = dt;
GridView2.DataBind();


}
//Code for when we click CheckBox in Header Template then all the checkbox in ItemTemplate is checked and vice versa...
protected void ChkAll_CheckedChanged1(object sender, EventArgs e)
{
CheckBox ch = (CheckBox)sender;
if (ch.Checked)
{
foreach (GridViewRow grv in GridView2.Rows)
{
((CheckBox)grv.Cells[0].Controls[1]).Checked = true;
}
}
else
{
foreach (GridViewRow grv in GridView2.Rows)
{
((CheckBox)grv.Cells[0].Controls[1]).Checked = false;
}
}
}
//Code for Delete all checked CheckBox from GridView........................ using sql in keyword...
string rollno = "";
protected void linkButtonDelete_Click(object sender, EventArgs e)
{

try
{
foreach (GridViewRow grv in GridView2.Rows)
{
if (((CheckBox)grv.Cells[0].Controls[1]).Checked == true)
{
rollno += grv.Cells[1].Text + ",";
}

}

SqlConnection con = new SqlConnection(@"Data Source=KUSH-PC\KUSH;Initial Catalog=College;Integrated Security=True");
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "delete from Student where RollNo in( " + rollno.Remove(rollno.Length - 1) + " )";
cmd.Connection = con;
con.Open();
int i = cmd.ExecuteNonQuery();
if (i != 0)
{

lblmessage.Visible = true;
lblmessage.ForeColor = Color.Green;
lblmessage.Text = "Delete Successfull checked Row in GridView";

}
con.Close();
GridViewBind();
}
catch (Exception ex)
{
lblmessage.Visible = true;
lblmessage.ForeColor = Color.Red;
lblmessage.Text =ex.Message;
}


}
//Code for all TextBox Clear
private void TextClear()
{
txtemailid.Text = "";
txtname.Text = String.Empty;
txtrollno.Text = "";
ddlcourse.SelectedIndex = 0;

}

}


1 comments: