Gridview delete row display items confirmation

In this blog 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))

 

<%@ 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>

</head>

<body>

    <form id="form1" runat="server">

    <div>

     <asp:GridView  DataKeyNames="sid"  ID="GridView1"  runat="server"  AutoGenerateColumns="False"  OnRowDataBound="GridView1_RowDataBound"   OnRowDeleting="GridView1_RowDeleting">

<Columns>

 

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

 

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

 

<asp:TemplateField HeaderText="Select">

 

<ItemTemplate>

 

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

</ItemTemplate>

 

</asp:TemplateField>

 

</Columns>

 

</asp:GridView>

    </div>

    </form>

</body>

</html>

 

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();

    }

}

 

Ebook Download
View all
Learn
View all