How to delete a a prticular row in datagrid using asp.net
how to delete a a prticular row in datagrid using asp.net .please the code to delte the row in data grid .
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Lakshmanan Sethu SankaranarayanRank #124 · 17.7K reputationPosted Jun 14, 2014, 2:55 AM EDT
Pls refer this
http://www.aspsnippets.com/Articles/ASPNet-GridView---Delete-Row-with-Confirmation.aspx
selvi subramanianRank #2,007 · 723 reputationPosted Jun 14, 2014, 3:07 AM EDT
Satyapriya NayakRank #47 · 31.5K reputationPosted Jun 14, 2014, 5:24 AM EDT
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
string connStr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
SqlCommand cmd = new SqlCommand();
SqlConnection con;
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
BindGrid();
}
}
private void BindGrid()
{
con = new SqlConnection(connStr);
cmd.Connection = con;
cmd.CommandText = "Select * from student";
con.Open();
GridView1.DataSource = cmd.ExecuteReader();
GridView1.DataBind();
con.Close();
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
LinkButton l = (LinkButton)e.Row.FindControl("LinkButton1");
l.Attributes.Add("onclick", "javascript:return " + "confirm('Are you sure you want to delete this record:: " + DataBinder.Eval(e.Row.DataItem, "sid") + "')");
}
}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
string sid = (string)GridView1.DataKeys[e.RowIndex].Value;
SqlConnection con = new SqlConnection(connStr);
cmd.Connection = con;
cmd.CommandText = "DELETE FROM student WHERE sid='" + sid + "'";
con.Open();
cmd.ExecuteNonQuery();
con.Close();
BindGrid();
}
}
Abhay ShankerRank #375 · 6.7K reputationPosted Jun 14, 2014, 10:22 AM EDT
http://csharpexample.com/CsharpExampleSeeDetail.aspx?Did=10&CatID=21
http://www.dotnetgallery.com/kb/resource10-How-to-perform-insert-update-delete-and-select-rows-in-ASPNET-gridview-c.aspx
selvi subramanianRank #2,007 · 723 reputationPosted Jun 15, 2014, 8:38 PM EDT
Lakshmanan Sethu SankaranarayanRank #124 · 17.7K reputationPosted Jun 15, 2014, 9:04 PM EDT
Please Post your data grid code here.