Description:-
In
this example I will explain that how to delete multiple rows or records in
GridView based on CheckBox selection and with displaying confirmation message
box before deleting rows in ASP.Net.
In which I have added a template field with
checkboxes with each and every rows of gridview and also I have added a check all checkbox in the
Header Template of the ASP.net GridView Control. When user check the header
checkbox the all rows’s checkbox will be
automatically checked.
This is nothing but a It is one kind of
Facility that for example suppose you have Delete 100 Record or All Record in Gridview at that this
Example is very useful Because you can Select All Record through Checkbox and
press delete Button the all Record are Deleted successfully otherwise you have
to Delete Record one by one and it is very time consuming process so that this
Facility is very useful.
In this
Example we provide Facility to user just Checkmark on the Header Checkbox the
All Record that are Display in Gridview are Checked. And when Uncheckmark the
Header Checkbox All Record that are Display on Gridview are Unchecked.
You can
also Delete Selected Record for example suppose Gridview contains 100 Record
and we want to delete a 5
and 25 and 35 number Record so it is possible through this Example.
GridView Code:-
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
BackColor="White" BorderColor="#336699" BorderStyle="Solid" BorderWidth="1px"
CellPadding="0" DataKeyNames="id" Font-Size="10pt"
Font-Names="Arial" GridLines="Vertical" Width="40%">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="chkStatus" runat="server"
AutoPostBack="true" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="RollNo" HeaderText="RollNo" />
<asp:BoundField DataField="StudentName" HeaderText="StudentName" />
</Columns>
</asp:GridView>
<asp:button runat="server" text="Button" onClick="delclick" />
ClickEvent For Delete:-
protected void delclick(object sender, ImageClickEventArgs e)
{
foreach (GridViewRow grdRow in GridView1.Rows)
{
CheckBox c =
(CheckBox)(GridView1.Rows[grdRow.RowIndex].Cells[0].FindControl("CheckBox1"));
string a = GridView1.Rows[grdRow.RowIndex].Cells[1].Text;
if (c.Checked)
{
string con = @"Data
Source=SQLDB;Initial Catalog=Demo;User ID=Demod;Password=Demo1@";
string q = "delete from bio where
id = " + a.ToString();
SqlConnection conn = new SqlConnection(con);
SqlCommand cmd = new SqlCommand(q, conn);
conn.Open();
cmd.ExecuteNonQuery();
}
}
grid();
}
Bingrid
Function:-
protected void grid()
{
DataSet ds = new DataSet();
string con = @"Data
Source=SQLDB;Initial Catalog=Demo;User ID=Demod;Password=Demo1@";
string q = "select * from
bio";
SqlConnection conn = new SqlConnection(con);
SqlCommand cmd = new SqlCommand(q, conn);
SqlDataAdapter sa = new SqlDataAdapter();
conn.Open();
cmd.ExecuteNonQuery();
sa.SelectCommand = cmd;
sa.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
}
0 comments:
Post a Comment