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

How to delete row from grid view showing confirmation JavaScript using asp.net 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 ?

HTML

@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="TestDesign_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></title>
</head>
<body>
  <form id="form1" runat="server">
  <div>
  <asp:GridView ID="Studentlist" CssClass="Grid" runat="server" OnRowDeleting="OnRowDeleting"
  AutoGenerateColumns="false" OnRowDataBound="OnRowDataBound">
  <Columns>
  <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" />
  </Columns>
  </asp:GridView>
  </div>
  </form>
</body>
</html>

NAMESPACE

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Data;

CODE BEHIND

partial class TestDesign_Default : System.Web.UI.Page
{
  protected void Page_Load(object sender, EventArgs e)
  {
 
  if (!IsPostBack)
  {
 
  if (!Page.IsPostBack)
  {
  ViewState["StudentList"] = GetStudentList();
  BindGrid();
 
 
 
  }
 
  }
 
  }
 
 
  protected void BindGrid()
  {
  Studentlist.DataSource = ViewState["StudentList"];
  Studentlist.DataBind();
  }
  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;
  dt.Rows[index].Delete();
  ViewState["dt"] = dt;
  BindGrid();
  }
 
  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 asp.net c#.
How to create grid view paging in asp.net using c#
How to bind Multi-Dimensional Arrays Data to gird view in asp.net using C#
How to bind Two-Dimensional Arrays Data to gird view in asp.net using C#
How to bind Single-Dimensional Arrays Data to gird view in asp.net using C#
how to export gridview data to pdf in asp.net c#
how to export gridview data to word in asp.net using c#
how to export gridview data to excel in c#
How to delete row from grid view showing confirmation JavaScript using asp.net c#
How to delete data from grid view using row deleting event in asp.net c#
How to bind data into gridview using code behind in asp.net c#
How to scroll gird view overflow data in asp.net c#
How to Bind data into grid view control using asp.net c#

Article Category

How to create asp.net control dynamically
Learn HTML for beginner
DataList example in C Sharp
Mail sending in asp.net 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#
asp.net c # basic tutorial
How to use ajax toolkit in asp.net C#
How to use different types of validation control using asp.net c#
How to use grid view in asp.net c#
Protected by Copyscape Online Plagiarism Detection