Click here to Skip to main content
15,885,777 members
Articles / Web Development / ASP.NET

Delete Data in GridView Using Template Button

Rate me:
Please Sign up or sign in to vote.
4.50/5 (5 votes)
20 Sep 2010CPOL 57.4K   11   1
Delete Data in GridView Using Template Button

This article snippet explains how to delete data from GridView using template buttons.
Now we look at how to add template button in GridView. First select Edit Columns in GridView.

Second, add Template Column in GridView.

Now configure Template Column, click Edit Template. Place a LinkButton on Template field. Click Edit Databindings, then select CommandArgument, after that, set field binding, bound to "ID" field, this ID field is used to delete data in server side code. Then press ok button.

Now we can configure LinkButton [Delete button].
Select Property of Link button. Set CommandName and Text as Delete. We can access this CommandName in GridView1_RowCommand event. After this, add GridView events GridView1_RowCommand and GridView1_RowDeleted.

The RowDeleted event is raised whenever a Delete button associated with an item in the GridView control is clicked, but after the GridView control deletes the record.

This allows you to provide an event-handling method that performs a custom routine, such as checking the results of a delete operation, whenever this event occurs. To avoid errors, we add one custom code in GridView1_RowDeleted event.

C#
//handling gridview delete excetion
e.ExceptionHandled = true;

Now the server side part.

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Default3 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void GridView1_RowDeleted(object sender, GridViewDeletedEventArgs e)
    {
        //handling gridview delete excetion
        e.ExceptionHandled = true;
    }
    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "dele")
        {
            //database Helper
            DBHelper objDBHelper = new DBHelper();
            //sql command to delete data from database
            string sqlCmdText = string.Format
		("DELETE FROM Table WHERE ID='{0}'", e.CommandArgument.ToString());
            //Executeing sql command
            objDBHelper.ExecuteScalar(sqlCmdText);
            //refresh gridview
            GridView1.DataBind();
        }
    }
}

ASPX Page:

HTML
<%@ Page Language="C#" AutoEventWireup="true" 
	CodeFile="Default3.aspx.cs" Inherits="Default3" %>
<!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>
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
            DataSourceID="SqlDataSource1" onrowcommand="GridView1_RowCommand"
            onrowdeleted="GridView1_RowDeleted">
            <Columns>
                <asp:BoundField DataField="biInvitationId" HeaderText="biInvitationId"
                    InsertVisible="False" ReadOnly="True" 
				SortExpression="biInvitationId" />
                <asp:BoundField DataField="biEventName" HeaderText="biEventName"
                    SortExpression="biEventName" />
                <asp:BoundField DataField="biHostName" HeaderText="biHostName"
                    SortExpression="biHostName" />
                <asp:BoundField DataField="biTelephone" HeaderText="biTelephone"
                    SortExpression="biTelephone" />
                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:LinkButton ID="LinkButton1" runat="server"
                            CommandArgument='<%# Eval("biInvitationId") %>' 
				CommandName="dele">Delete</asp:LinkButton>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server">
        </asp:SqlDataSource>
    </div>
    </form>
</body>
</html>

Thank you!

Feel free to comment.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
United Kingdom United Kingdom
Microsoft Certified Professional Developer.

Comments and Discussions

 
GeneralMy vote of 3 Pin
Elvin Mammadov2-Jan-13 2:04
Elvin Mammadov2-Jan-13 2:04 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.