.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
david stephan
Gaurav Pal

Home >> Code Snippets >> DataGrid >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

Gridview delete row display items Confirmation

Posted By:satyapriyanayak       Posted Date: September 12, 2013    Points: 40    Category: DataGrid    URL: http://www.dotnetspark.com  

In this article we will know how to display data in grid view with a delete link for every data. When we delete data by clicking the delete link it will display a message that which data we want to delete Create table student (sid varchar(50),sname varchar(50))

Create table student (sid varchar(50),sname varchar(50))

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_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>Untitled Page</title>
    <form id="form1" runat="server">
     <asp:GridView  DataKeyNames="sid"  ID="GridView1"  runat="server"  AutoGenerateColumns="False"  OnRowDataBound="GridView1_RowDataBound"   OnRowDeleting="GridView1_RowDeleting">

<asp:BoundField DataField="sid" HeaderText="sid" />

<asp:BoundField DataField="sname" HeaderText="sname" />

<asp:TemplateField HeaderText="Select">


<asp:LinkButton ID="LinkButton1" CommandArgument='<%# Eval("sid") %>' CommandName="Delete" runat="server">Delete</asp:LinkButton>




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)
    private void BindGrid()
        con = new SqlConnection(connStr);
        cmd.Connection = con;
        cmd.CommandText = "Select * from student";
        GridView1.DataSource = cmd.ExecuteReader();
    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 + "'";


Further Readings:


No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend