Click here to Skip to main content
12,831,554 members (30,241 online)
Click here to Skip to main content
Add your own
alternative version


11 bookmarked
Posted 20 Sep 2010

Delete Data in GridView Using Template Button

, 20 Sep 2010 CPOL
Rate this:
Please Sign up or sign in to vote.
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 configuring 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.

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

Now the server side part.

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
            //refresh gridview

ASPX Page:

<%@ Page Language="C#" AutoEventWireup="true" 
	CodeFile="Default3.aspx.cs" Inherits="Default3" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
<html xmlns="">
<head runat="server">
    <form id="form1" runat="server">
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
            DataSourceID="SqlDataSource1" onrowcommand="GridView1_RowCommand"
                <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:LinkButton ID="LinkButton1" runat="server"
                            CommandArgument='<%# Eval("biInvitationId") %>' 
        <asp:SqlDataSource ID="SqlDataSource1" runat="server">

Thank you!

Feel free to comment.


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


About the Author

raju melveetilpurayil
Software Developer (Senior)
United Kingdom United Kingdom
Microsoft Certified Professional Developer.

You may also be interested in...


Comments and Discussions

GeneralMy vote of 3 Pin
Elvin Mammadov2-Jan-13 3:04
memberElvin Mammadov2-Jan-13 3: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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170326.1 | Last Updated 20 Sep 2010
Article Copyright 2010 by raju melveetilpurayil
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid