Hello Friends!
Here I will teach you How to Delete Multiple Records In Grid View Control. For This Purpose I have used checkbox in the Grid View to delete multiple records.
Step 1 : Create a table in sql server database for bind grid view .Here I am created one table that name it dsh_poco_regi. For the quickly operation I filled the manually data in database table.
Step 2 : Open Visual Studio and create New Project (ASP.NET Web Application).
Step 3 : Add One Grid View Control in Default.aspx. Here I also perform the search data and Highlight that data in GridView Control so I have use one dropdownlist ,one textbox inside gridview and one search button.
Html Code of Default.aspx page
Design page look like below picture.
Step 4 : open code behind file and write the following code.
Check the checkbox which we want to delete records:
For Search Records:
Search Results:
Here I will teach you How to Delete Multiple Records In Grid View Control. For This Purpose I have used checkbox in the Grid View to delete multiple records.
Step 1 : Create a table in sql server database for bind grid view .Here I am created one table that name it dsh_poco_regi. For the quickly operation I filled the manually data in database table.
Step 2 : Open Visual Studio and create New Project (ASP.NET Web Application).
Step 3 : Add One Grid View Control in Default.aspx. Here I also perform the search data and Highlight that data in GridView Control so I have use one dropdownlist ,one textbox inside gridview and one search button.
Html Code of Default.aspx page
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="gridSearch._Default" %>
<!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>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
<div>
<asp:GridView ID="gridserach" runat="server" AutoGenerateColumns="false" AllowPaging="true"
DataKeyNames="userid" ShowFooter="true" OnRowCommand="gridserach_RowCommand"
EmptyDataText="data not found ! keep trying" EmptyDataRowStyle-BackColor="Beige">
<EmptyDataRowStyle BackColor="Beige"></EmptyDataRowStyle>
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<asp:CheckBox ID="chkheader" runat="server" OnCheckedChanged="chkheader_CheckedChanged"
AutoPostBack="true" />
<asp:LinkButton ID="lnkdelete" runat="server" OnClick="lnkdelete_Click" CommandName="Delete">Delete</asp:LinkButton>
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="chkinside" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField HeaderText="Fname" DataField="fname" />
<asp:BoundField HeaderText="Lname" DataField="lname" />
<asp:BoundField HeaderText="Age" DataField="age" />
<asp:BoundField HeaderText="Gender" DataField="gender" />
<asp:BoundField HeaderText="Contactno" DataField="contactno" />
<asp:TemplateField>
<FooterTemplate>
<asp:DropDownList ID="drsearch" runat="server">
<asp:ListItem>fname</asp:ListItem>
<asp:ListItem>age</asp:ListItem>
</asp:DropDownList>
<asp:TextBox ID="txtsearch" runat="server" AutoPostBack="true"></asp:TextBox>
<asp:Button ID="btnserach" Text="Search" runat="server" CommandName="search" />
</FooterTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>
Design page look like below picture.
Step 4 : open code behind file and write the following code.
using System;Step 5 : Run The Application
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
namespace gridSearch
{
public partial class _Default : System.Web.UI.Page
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["demostring"].ConnectionString);
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
bindgrd();
}
}
//Method to Fill GridView
private void bindgrd()
{
string query = "select * from dsh_poco_regi";
SqlDataAdapter adp = new SqlDataAdapter(query, con);
DataTable dt = new DataTable();
adp.Fill(dt);
gridserach.DataSource = dt;
gridserach.DataBind();
}
protected void gridserach_RowCommand(object sender, GridViewCommandEventArgs e)
{
//Search Dropdownlist in Gridview
DropDownList drp = (DropDownList)gridserach.FooterRow.FindControl("drsearch");
if (drp.SelectedValue == "fname")
{
//Search TextBox in GridView
TextBox tx = (TextBox)gridserach.FooterRow.FindControl("txtsearch");
string query = "select * from dsh_poco_regi where fname like '" + tx.Text + "%'";
SqlDataAdapter adp = new SqlDataAdapter(query, con);
DataTable dt = new DataTable();
adp.Fill(dt);
gridserach.DataSource = dt;
gridserach.DataBind();
}
else
{
//Search TextBox in GridView
TextBox tx = (TextBox)gridserach.FooterRow.FindControl("txtsearch");
string query = "select * from dsh_poco_regi where age =" + tx.Text + "";
SqlDataAdapter adp = new SqlDataAdapter(query, con);
DataTable dt = new DataTable();
adp.Fill(dt);
gridserach.DataSource = dt;
gridserach.DataBind();
}
}
protected void chkheader_CheckedChanged(object sender, EventArgs e)
{
//Get Checkbox value from HeaderTemplate
CheckBox chh = (CheckBox)gridserach.HeaderRow.FindControl("chkheader");
foreach (GridViewRow row in gridserach.Rows)
{
//Get Checkbox value from ItemTemplate
CheckBox chh1 = (CheckBox)row.FindControl("chkinside");
if (chh.Checked == true)
{
chh1.Checked = true;
}
else
{
chh1.Checked = false;
}
}
}
// click event fro delete multiple record using link button
protected void lnkdelete_Click(object sender, EventArgs e)
{
foreach (GridViewRow row in gridserach.Rows)
{
CheckBox chh1 = (CheckBox)row.FindControl("chkinside");
if (chh1.Checked == true)
{
//Get Value of Row Id using DataKeyNames
int pkid = Convert.ToInt32(gridserach.DataKeys[row.RowIndex].Values["userid"]);
string query1 = "delete from dsh_poco_regi where userid=" + pkid + "";
SqlCommand cmd = new SqlCommand(query1, con);
cmd.Connection = con;
cmd.Connection.Open();
cmd.ExecuteNonQuery();
cmd.Connection.Close();
}
else
{
}
}
bindgrd();
}
}
}
Check the checkbox which we want to delete records:
See below picture after click on delete (LinkButton):
For Search Records:
Search Results: