Introduction:-
Here
I have explained RowEditing, RowUpdating, RowDeleting and RowCommand Events of gridview
in asp.net.
Description:-
In my previous article I have explained How to get row number from gridview on textbox change event using javascript without page refreshing , Different ways to bind data in a textBox from gridview cells by click on select button using both row command and selected index changed event , Adding dynamic rows in asp.net gridview control with textboxes and with delete functionality , Upload a excel file and display it in a gridview as well as display this excel sheet from a starting row to ending row , How to break the text in a gridview , Using asp.net gridview EmptyDataTemplate and FooterTemplate add record to database , Ajax tab inside a gridview , Delete row from gridview and database using jQuery in asp.net .
Here I will explain Row Editing, Row Updating, Row
Deleting, Row Command events of gridview with suitable examples.
For this take one gridview control inside your page. Here
I am going to provide an example with source code, take a look.
Here
is my Source Code:-
<%@ Page Language="C#"
AutoEventWireup="true"
CodeFile="GridViewComplete.aspx.cs"
Inherits="GridViewComplete" %>
<!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 id="Head1" runat="server">
<title>Grid View Add Update Delete</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="ShibashishGridDemo" runat="server" AutoGenerateColumns="False" DataKeyNames="Id"
ShowFooter="true" AllowPaging="true" PageSize="4"
AllowSorting="True"
OnRowCommand="ShibashishGridDemo_RowCommand"
OnPageIndexChanging="ShibashishGridDemo_PageIndexChanging"
OnRowDeleting="ShibashishGridDemo_RowDeleting"
OnRowEditing="ShibashishGridDemo_RowEditing"
OnRowUpdating="ShibashishGridDemo_RowUpdating"
OnSorting="ShibashishGridDemo_Sorting"
OnRowCancelingEdit="ShibashishGridDemo_RowCancelingEdit">
<Columns>
<asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />
<asp:TemplateField HeaderText="Id" InsertVisible="False" SortExpression="Id">
<EditItemTemplate>
<asp:Label ID="Label1" runat="server"
Text='<%# Eval("Id") %>'></asp:Label>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server"
Text='<%# Bind("Id") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Id" ShowHeader="True"/>
<asp:TemplateField HeaderText="Name" SortExpression="Name">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server"
Text='<%# Bind("Name") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server"
Text='<%# Bind("Name") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="QuantityTextBox" runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Description" SortExpression="Description">
<EditItemTemplate>
<asp:TextBox ID="TextBox2" runat="server"
Text='<%# Bind("Description") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label3" runat="server"
Text='<%# Bind("Description") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="DescriptionTextBox" runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField>
<FooterTemplate>
<asp:LinkButton ID="btnNew" runat="server"
CommandName="New" Text="New" />
</FooterTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
<div style="color:Red">
<asp:Label ID="lblMsg" runat="server"></asp:Label>
</div>
</form>
</body>
</html>
CodeFile="GridViewComplete.aspx.cs"
Inherits="GridViewComplete" %>
<!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 id="Head1" runat="server">
<title>Grid View Add Update Delete</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="ShibashishGridDemo" runat="server" AutoGenerateColumns="False" DataKeyNames="Id"
ShowFooter="true" AllowPaging="true" PageSize="4"
AllowSorting="True"
OnRowCommand="ShibashishGridDemo_RowCommand"
OnPageIndexChanging="ShibashishGridDemo_PageIndexChanging"
OnRowDeleting="ShibashishGridDemo_RowDeleting"
OnRowEditing="ShibashishGridDemo_RowEditing"
OnRowUpdating="ShibashishGridDemo_RowUpdating"
OnSorting="ShibashishGridDemo_Sorting"
OnRowCancelingEdit="ShibashishGridDemo_RowCancelingEdit">
<Columns>
<asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />
<asp:TemplateField HeaderText="Id" InsertVisible="False" SortExpression="Id">
<EditItemTemplate>
<asp:Label ID="Label1" runat="server"
Text='<%# Eval("Id") %>'></asp:Label>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server"
Text='<%# Bind("Id") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Id" ShowHeader="True"/>
<asp:TemplateField HeaderText="Name" SortExpression="Name">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server"
Text='<%# Bind("Name") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server"
Text='<%# Bind("Name") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="QuantityTextBox" runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Description" SortExpression="Description">
<EditItemTemplate>
<asp:TextBox ID="TextBox2" runat="server"
Text='<%# Bind("Description") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label3" runat="server"
Text='<%# Bind("Description") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="DescriptionTextBox" runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField>
<FooterTemplate>
<asp:LinkButton ID="btnNew" runat="server"
CommandName="New" Text="New" />
</FooterTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
<div style="color:Red">
<asp:Label ID="lblMsg" runat="server"></asp:Label>
</div>
</form>
</body>
</html>
Here
is my Code Behind:-
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class GridViewComplete : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindShibashishGridDemo();
}
}
public void BindShibashishGridDemo()
{
if (Session["dt"] == null)
{
ShibashishGridDemo.DataSource = CreateDGDataSource();
ShibashishGridDemo.DataBind();
}
else
{
ShibashishGridDemo.DataSource = Session["dt"] as DataTable;
ShibashishGridDemo.DataBind();
}
}
public DataTable CreateDGDataSource()
{
// Create sample data for the DataList control.
DataTable dt = new DataTable();
DataRow dr;
int i;
int y;
// Define the columns of the table.
dt.Columns.Add(new DataColumn("ID", typeof(int)));
dt.Columns.Add(new DataColumn("Name", typeof(string)));
dt.Columns.Add(new DataColumn("Description", typeof(string)));
//Make some rows and put some sample data in
for (i = 1; i <= 5; i++)
{
dr = dt.NewRow();
dr[0] = i;
dr[1] = "Name" + "-" + i;
dr[2] = "Item " + "_" + i;
//add the row to the datatable
dt.Rows.Add(dr);
}
Session["y"] = i;
Session["dt"] = dt;
return dt;
}
public ICollection CreateDGDataSource(int CategoryID)
{
DataView dv = new DataView(CreateDGDataSource(), "ID=" + CategoryID, null,
DataViewRowState.CurrentRows);
return dv;
}
protected void ShibashishGridDemo_RowCommand(object sender, GridViewCommandEventArgs e)
{
try
{
if (e.CommandName.Equals("New"))
{
LinkButton btnNew = e.CommandSource as LinkButton;
GridViewRow row = btnNew.NamingContainer as GridViewRow;
if (row == null)
{
return;
}
TextBox txtCatName = row.FindControl("QuantityTextBox") as TextBox;
TextBox txtDescription = row.FindControl("DescriptionTextBox") as TextBox;
DataTable dt = Session["dt"] as DataTable;
DataRow dr;
int intId = (int)Session["y"];
dr = dt.NewRow();
dr["Id"] = intId++;
Session["y"] = intId;
dr["Name"] = txtCatName.Text;
dr["Description"] = txtDescription.Text;
dt.Rows.Add(dr);
dt.AcceptChanges();
Session["dt"] = dt;
ShibashishGridDemo.DataSource = Session["dt"] as DataTable;
ShibashishGridDemo.DataBind();
}
}
catch (Exception ex)
{
}
}
protected void ShibashishGridDemo_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
ShibashishGridDemo.PageIndex = e.NewPageIndex;
BindShibashishGridDemo();
}
protected void ShibashishGridDemo_Sorting(object sender, GridViewSortEventArgs e)
{
DataTable dataTable = Session["dt"] as DataTable;
if (dataTable != null)
{
DataView dataView = new DataView(dataTable);
dataView.Sort = e.SortExpression + " " + ConvertSortDirectionToSql(e.SortDirection);
ShibashishGridDemo.DataSource = dataView;
ShibashishGridDemo.DataBind();
}
}
private string ConvertSortDirectionToSql(SortDirection sortDireciton)
{
string newSortDirection = String.Empty;
switch (sortDireciton)
{
case SortDirection.Ascending:
newSortDirection = "ASC";
break;
case SortDirection.Descending:
newSortDirection = "DESC";
break;
}
return newSortDirection;
}
protected void ShibashishGridDemo_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
int ID = (int)ShibashishGridDemo.DataKeys[e.RowIndex].Value;
// Query the database and get the values based on the ID and delete it.
lblMsg.Text = "Deleted Record Id" +ID.ToString();
}
protected void ShibashishGridDemo_RowEditing(object sender, GridViewEditEventArgs e)
{
ShibashishGridDemo.EditIndex = e.NewEditIndex;
BindShibashishGridDemo();
}
protected void ShibashishGridDemo_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
// Retrieve the row being edited.
int index = ShibashishGridDemo.EditIndex;
GridViewRow row = ShibashishGridDemo.Rows[index];
TextBox t1 = row.FindControl("TextBox1") as TextBox;
TextBox t2 = row.FindControl("TextBox2") as TextBox;
string t3 = ShibashishGridDemo.DataKeys[e.RowIndex].Value.ToString();
// Write your Query here For Updating Your Data.
lblMsg.Text = "Updated record " + t1.Text + "," + t2.Text + "Value From Bound Field" + t3;
}
protected void ShibashishGridDemo_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
ShibashishGridDemo.EditIndex = -1;
BindShibashishGridDemo();
}
}
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class GridViewComplete : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindShibashishGridDemo();
}
}
public void BindShibashishGridDemo()
{
if (Session["dt"] == null)
{
ShibashishGridDemo.DataSource = CreateDGDataSource();
ShibashishGridDemo.DataBind();
}
else
{
ShibashishGridDemo.DataSource = Session["dt"] as DataTable;
ShibashishGridDemo.DataBind();
}
}
public DataTable CreateDGDataSource()
{
// Create sample data for the DataList control.
DataTable dt = new DataTable();
DataRow dr;
int i;
int y;
// Define the columns of the table.
dt.Columns.Add(new DataColumn("ID", typeof(int)));
dt.Columns.Add(new DataColumn("Name", typeof(string)));
dt.Columns.Add(new DataColumn("Description", typeof(string)));
//Make some rows and put some sample data in
for (i = 1; i <= 5; i++)
{
dr = dt.NewRow();
dr[0] = i;
dr[1] = "Name" + "-" + i;
dr[2] = "Item " + "_" + i;
//add the row to the datatable
dt.Rows.Add(dr);
}
Session["y"] = i;
Session["dt"] = dt;
return dt;
}
public ICollection CreateDGDataSource(int CategoryID)
{
DataView dv = new DataView(CreateDGDataSource(), "ID=" + CategoryID, null,
DataViewRowState.CurrentRows);
return dv;
}
protected void ShibashishGridDemo_RowCommand(object sender, GridViewCommandEventArgs e)
{
try
{
if (e.CommandName.Equals("New"))
{
LinkButton btnNew = e.CommandSource as LinkButton;
GridViewRow row = btnNew.NamingContainer as GridViewRow;
if (row == null)
{
return;
}
TextBox txtCatName = row.FindControl("QuantityTextBox") as TextBox;
TextBox txtDescription = row.FindControl("DescriptionTextBox") as TextBox;
DataTable dt = Session["dt"] as DataTable;
DataRow dr;
int intId = (int)Session["y"];
dr = dt.NewRow();
dr["Id"] = intId++;
Session["y"] = intId;
dr["Name"] = txtCatName.Text;
dr["Description"] = txtDescription.Text;
dt.Rows.Add(dr);
dt.AcceptChanges();
Session["dt"] = dt;
ShibashishGridDemo.DataSource = Session["dt"] as DataTable;
ShibashishGridDemo.DataBind();
}
}
catch (Exception ex)
{
}
}
protected void ShibashishGridDemo_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
ShibashishGridDemo.PageIndex = e.NewPageIndex;
BindShibashishGridDemo();
}
protected void ShibashishGridDemo_Sorting(object sender, GridViewSortEventArgs e)
{
DataTable dataTable = Session["dt"] as DataTable;
if (dataTable != null)
{
DataView dataView = new DataView(dataTable);
dataView.Sort = e.SortExpression + " " + ConvertSortDirectionToSql(e.SortDirection);
ShibashishGridDemo.DataSource = dataView;
ShibashishGridDemo.DataBind();
}
}
private string ConvertSortDirectionToSql(SortDirection sortDireciton)
{
string newSortDirection = String.Empty;
switch (sortDireciton)
{
case SortDirection.Ascending:
newSortDirection = "ASC";
break;
case SortDirection.Descending:
newSortDirection = "DESC";
break;
}
return newSortDirection;
}
protected void ShibashishGridDemo_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
int ID = (int)ShibashishGridDemo.DataKeys[e.RowIndex].Value;
// Query the database and get the values based on the ID and delete it.
lblMsg.Text = "Deleted Record Id" +ID.ToString();
}
protected void ShibashishGridDemo_RowEditing(object sender, GridViewEditEventArgs e)
{
ShibashishGridDemo.EditIndex = e.NewEditIndex;
BindShibashishGridDemo();
}
protected void ShibashishGridDemo_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
// Retrieve the row being edited.
int index = ShibashishGridDemo.EditIndex;
GridViewRow row = ShibashishGridDemo.Rows[index];
TextBox t1 = row.FindControl("TextBox1") as TextBox;
TextBox t2 = row.FindControl("TextBox2") as TextBox;
string t3 = ShibashishGridDemo.DataKeys[e.RowIndex].Value.ToString();
// Write your Query here For Updating Your Data.
lblMsg.Text = "Updated record " + t1.Text + "," + t2.Text + "Value From Bound Field" + t3;
}
protected void ShibashishGridDemo_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
ShibashishGridDemo.EditIndex = -1;
BindShibashishGridDemo();
}
}
In the above example you found all the events related to gridview.
Still you have some question then contact me through social media.
Hope you will like this article.
Keep coding……
No comments:
Post a Comment
Please don't spam, spam comments is not allowed here.