Sunday, September 19, 2010

Delete Data in GridView Using Template Button

This article explain how to delete data from GridView using Template Buttons

Now we look how to add template button in GridView.
first select Edit Columns in GridView.

2nd 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



<%@ 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") %>' CommandName="dele">Delete</asp:LinkButton>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server">

Thank you,

Feel free to comment.


No comments: