Posted by : Anwar Hossain
Category : How to use grid view in c#

How to delete row from grid view showing confirmation JavaScript using c#

Dear viewers I will show how to delete data from grid view using on row deleting event and confirmation yes/no button . First I have created virtual data using data table to supply data to the grid view But real-time data needs to be collected from database. So when user click particular row then a delete confirmation yes/no window display by retrieving particular data if yes then the particular data will be deleted.

Gridview row deleting with Confirmation ?


@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="TestDesign_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<html xmlns="">
<head runat="server">
  <form id="form1" runat="server">
  <asp:GridView ID="Studentlist" CssClass="Grid" runat="server" OnRowDeleting="OnRowDeleting"
  AutoGenerateColumns="false" OnRowDataBound="OnRowDataBound">
  <asp:BoundField DataField="StudentName" HeaderText="StudentName" />
  <asp:BoundField DataField="Address" HeaderText="Address" />
  <asp:BoundField DataField="Phone" HeaderText="Phone" />
  <asp:BoundField DataField="JoingDate" HeaderText="JoingDate" />
  <asp:CommandField ShowDeleteButton="True" ButtonType="Button" HeaderText="Delete"
  HeaderStyle-BackColor="#FF286F" />


using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Data;


partial class TestDesign_Default : System.Web.UI.Page
  protected void Page_Load(object sender, EventArgs e)
  if (!IsPostBack)
  if (!Page.IsPostBack)
  ViewState["StudentList"] = GetStudentList();
  protected void BindGrid()
  Studentlist.DataSource = ViewState["StudentList"];
  static DataTable GetStudentList()
  //I  Have Created Virtual table
  DataTable table = new DataTable();
  table.Columns.Add("StudentName", typeof(string));
  table.Columns.Add("Address", typeof(string));
  table.Columns.Add("Phone", typeof(string));
  table.Columns.Add("JoingDate", typeof(DateTime));
  // Now have created some row to fill the the datatable
  table.Rows.Add( "Samim", "Dhaka","01856587548", "11/5/2007");
  table.Rows.Add("Ahsan", "Chittagong", "01856587548","12/ 5/2007");
  table.Rows.Add("Nirub", "Sylhet", "01856587544", "11/10/ 2007");
  table.Rows.Add("Zaman", "Rajsahi", "01856587578", "11 /15/ 2007");
  table.Rows.Add("Habib", "Barisal", "01856587548", "11 /5 / 2010");
  return table;
  protected void OnRowDeleting(object sender, GridViewDeleteEventArgs e)
   int index = Convert.ToInt32(e.RowIndex);
  DataTable dt = ViewState["StudentList"] as DataTable;
  ViewState["dt"] = dt;
  protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
  if (e.Row.RowType == DataControlRowType.DataRow)
  //Find cell Postion to find student Name
  string StudentName = e.Row.Cells[0].Text;
  foreach (Button button in e.Row.Cells[4].Controls.OfType<Button>())
  if (button.CommandName == "Delete")
  button.Attributes["onclick"] = "if(!confirm('Do you want to delete " + StudentName + "?')){ return false; };";

Out Put


Realted Article Headline

Gridview datetime format example in ASP.Net c#
Displaying total column value in the Gridview Footer using c#.
How to create grid view paging in using c#
How to bind Multi-Dimensional Arrays Data to gird view in using C#
How to bind Two-Dimensional Arrays Data to gird view in using C#
How to bind Single-Dimensional Arrays Data to gird view in using C#
how to export gridview data to pdf in c#
how to export gridview data to word in using c#
how to export gridview data to excel in c#
How to delete row from grid view showing confirmation JavaScript using c#
How to delete data from grid view using row deleting event in c#
How to bind data into gridview using code behind in c#
How to scroll gird view overflow data in c#
How to Bind data into grid view control using c#

Article Category

How to create control dynamically
Learn HTML for beginner
DataList example in C Sharp
Mail sending in c#
State Management in ASP C #
Basic sql tutorial for Beginner
DataTable example in ASP.Net C#
How to use LINQ in ASP.NET C# c # basic tutorial
How to use ajax toolkit in C#
How to use different types of validation control using c#
How to use grid view in c#
Protected by Copyscape Online Plagiarism Detection