Search Asp.Net Here

Select Multipal rows in GridView




Select and Delete Multiple Rows in Gridview control

How to Select and delete Multiple Rows of GridView using CheckBox

GridView Control allow to select only one row at a time but But a little logic is required to select more than one row or Multiple rows in GridView control. if we require to select Multiple row and delete them in a single click than we can do this by writing a little code.

In my example I am using CheckBox control of asp.net . A CheckBox control is added in every row of grid view ( using TemplateField) and any GridView row can be selected by selecting CheckBox control of that particular row.




Following sqlserver table (tbemp) is used in the example


Here is code for .aspx page

<div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="3" GridLines="None" BackColor="White" BorderColor="White" BorderStyle="Ridge" BorderWidth="2px" CellSpacing="1">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="cb" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Emp NO.">
<ItemTemplate>
<asp:Label ID="lb1" Text='<%#Eval("eno") %>' runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Emp Name">
<ItemTemplate>
<%#Eval("ename") %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Emp Address">
<ItemTemplate >
<%#Eval("eadd") %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Emp Salary">
<ItemTemplate>
<%#Eval("esal") %>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#C6C3C6" ForeColor="Black" />
<RowStyle BackColor="#DEDFDE" ForeColor="Black" />
<SelectedRowStyle BackColor="#9471DE" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#C6C3C6" ForeColor="Black" HorizontalAlign="Right" />
<HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#E7E7FF" />
</asp:GridView>

</div>
<br />
<asp:LinkButton ID="LinkButton1" runat="server" OnClick="LinkButton1_Click">Select All</asp:LinkButton>
 |
<asp:LinkButton ID="LinkButton2" runat="server" OnClick="LinkButton2_Click">Deselct All</asp:LinkButton>
 |
<asp:LinkButton ID="LinkButton3" runat="server" OnClick="LinkButton3_Click">Delete</asp:LinkButton>
<br />
<asp:Label ID="Label1" runat="server"></asp:Label>

Code for .cs file (Replace the Page_Load event with following code)


protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
gridbind();
}


//method to bind GridView control to database records

private void gridbind()
{
SqlDataAdapter adp = new SqlDataAdapter("Select * from tbemp", "server=tiger;database=database1;uid=sa");
DataSet ds = new DataSet();
adp.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
}

//Event to select all rows in GridView at one click

protected void LinkButton1_Click(object sender, EventArgs e)
{
CheckBox cb;
for (int i = 0; i < GridView1.Rows.Count; i++) 


cb = ((CheckBox)(GridView1.Rows[i].FindControl("cb"))); 
if (cb.Checked == false) 
cb.Checked = true; 

}
//Event to De-select all rows in GridView at one click

protected void LinkButton2_Click(object sender, EventArgs e)
{
CheckBox cb;
for (int i = 0; i < GridView1.Rows.Count; i++) 


cb = ((CheckBox)(GridView1.Rows[i].FindControl("cb"))); 
if (cb.Checked) 
cb.Checked = false; 

}
//Button Clicked Event to delete selected rows form gridview and database

protected void LinkButton3_Click(object sender, EventArgs e)
{
CheckBox cb;
String d = "";
for (int i = 0; i < GridView1.Rows.Count; i++) 

 { 
cb = ((CheckBox)(GridView1.Rows[i].FindControl("cb")));
if (cb.Checked) 
d +=((Label)( GridView1.Rows[i].FindControl("lb1"))).Text + ","; 

d = d.Substring(0, d.Length - 1); 
SqlConnection con = new SqlConnection("Server=tiger;database=database1;uid=sa"); 
con.Open();
SqlCommand cmd=new SqlCommand("delete from tbemp where eno in("+d.ToString()+")",con); cmd.ExecuteNonQuery(); 
gridbind(); 
}

In LinkButton3_Click event first we loop through each row of gridview and and then tested if they are selected (CheckBox checked) or not, the ‘Emp ID’ of each selected row is concatinated in a string variable named ‘d’ and passed as parameter in delete query “delete from tbemp where eno in("+d.ToString()+")"

Download Source Code


Comments :

1
Anonymous said...
on 

Nice article....

Post a Comment