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>
CODE BEHIND
public 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; };";
}
}
}
}
}