Check this example, I am storing the ID of the Customer in a Hidden Field and then using that ID I am deleting the row
HTML
<head runat="server">
<title></title>
<style type="text/css">
body { font-family: Arial; font-size: 10pt; }
table { border: 1px solid #ccc; }
table th { background-color: #F7F7F7; color: #333; font-weight: bold; }
table th, table td { padding: 5px; border-color: #ccc; }
</style>
</head>
<body>
<form id="form1" runat="server">
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="CustomerId" HeaderText="Id" HeaderStyle-Width="30" />
<asp:BoundField DataField="Name" HeaderText="Name" HeaderStyle-Width="150" />
<asp:BoundField DataField="Country" HeaderText="Country" HeaderStyle-Width="150" />
<asp:TemplateField HeaderStyle-Width="50">
<ItemTemplate>
<asp:HiddenField ID ="hfCustomerId" runat ="server" Value = '<%# Eval("CustomerId") %>' />
<asp:LinkButton ID="lnkDelete" Text="Delete" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
$(function () {
$("[id*=GridView1] [id*=lnkDelete]").click(function () {
if (confirm("Do you want to delete this Customer?")) {
var row = $(this).closest("tr");
var customerId = parseInt(row.find("[id*=hfCustomerId]").val());
$.ajax({
type: "POST",
url: "CS.aspx/DeleteCustomer",
data: '{customerId: ' + customerId + '}',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (r) {
if (r.d) {
row.remove();
if ($("[id*=GridView1] td").length == 0) {
$("[id*=GridView1] tbody").append("<tr><td colspan = '4' align = 'center'>No records found.</td></tr>")
}
alert("Customer record has been deleted.");
}
}
});
}
return false;
});
});
</script>
</form>
</body>
</html>
Namespaces
using System.Data;
using System.Web.Services;
using System.Data.SqlClient;
using System.Configuration;
Code
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.BindGrid();
}
}
private void BindGrid()
{
string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(conString))
{
using (SqlCommand cmd = new SqlCommand("SELECT * FROM Customers"))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Connection = con;
sda.SelectCommand = cmd;
DataTable dt = new DataTable();
sda.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
}
}
[WebMethod]
public static bool DeleteCustomer(int customerId)
{
string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(conString))
{
using (SqlCommand cmd = new SqlCommand("DELETE FROM Customers WHERE CustomerId = @CustomerId"))
{
cmd.Connection = con;
cmd.Parameters.AddWithValue("@CustomerId", customerId);
con.Open();
int rowsAffected = cmd.ExecuteNonQuery();
con.Close();
return rowsAffected > 0;
}
}
}
SQL
CREATE TABLE [dbo].[Customers](
[CustomerId] [int] NOT NULL,
[Name] [varchar](100) NOT NULL,
[Country] [varchar](50) NOT NULL
) ON [PRIMARY]
GO
INSERT INTO Customers
SELECT 1, 'John Hammond', 'United States'
UNION ALL
SELECT 2, 'Mudassar Khan', 'India'
UNION ALL
SELECT 3, 'Suzanne Mathews', 'France'
UNION ALL
SELECT 4, 'Robert Schidner', 'Russia'