Insert , update and Delete in Gridview
Aspx
page Code
<%@ Page Language="C#"
AutoEventWireup="true"
CodeFile="Default.aspx.cs"
Inherits="_Default"
%>
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server" style="text-align :center ;">
<div style="text-align:center; width :600px; margin-left:auto;margin-right:auto; font-weight: 700;">
<table style="width:200px; margin-left:auto; margin-right :auto; ">
<tr>
<td>Name</td>
<td>
<asp:TextBox ID="txtname" runat="server" >
</asp:TextBox></td>
</tr>
<tr>
<td>Address</td>
<td>
<asp:TextBox ID="txtaddress" runat="server">
</asp:TextBox></td>
</tr>
<tr>
<td></td>
<td><asp:Button ID="Button1" runat="server" Text="Add" onclick="Button1_Click" /></td>
</tr>
</table>
<br />
<asp:GridView ID="gvstudent" runat="server" AutoGenerateColumns="false"
EmptyDataText="Record not Found"
onrowcancelingedit="gvstudent_RowCancelingEdit"
onrowediting="gvstudent_RowEditing" onrowdeleting="gvstudent_RowDeleting"
onrowupdating="gvstudent_RowUpdating"
>
<Columns>
<asp:TemplateField HeaderText
="Id">
<ItemTemplate>
<asp:Label ID="lblid" runat="server" Text='<%# Eval("id") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText
="Name">
<ItemStyle Width="180px" />
<ItemTemplate>
<asp:Label ID="lblname" runat="server" Text='<%# Eval("name") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtname" runat="server" Text='<%# bind("name") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Address">
<ItemStyle Width="180px" />
<ItemTemplate>
<asp:Label ID="lbladdress" runat="server" Text='<%# Eval("stu_add") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtaddress" runat="server" Text='<%# bind("stu_add") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="btnDelete" runat="server" Commandname="Delete"
CommandArgument ='<%# Container.DataItemIndex %>' Text="Delete"
/>
<asp:Button ID="btnEdit" runat="server" CommandName="Edit" CommandArgument
='<%# Container.DataItemIndex %>' Text="Edit" />
</ItemTemplate>
<EditItemTemplate>
<asp:Button ID="btnupdate" runat="server" CommandArgument='<%# Container.DataItemIndex %>' CommandName="Update"
Text="Update"
/>
<asp:Button ID="btncanel" runat="server" CommandArgument='<%# Container.DataItemIndex %>' CommandName ="Cancel"
Text ="Cancel"
/>
</EditItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>
Aspx.cs Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using dbModel;
public partial class _Default :
System.Web.UI.Page
{
/// <summary>
/// Create a object publicaly
/// </summary>
restaurentEntities
entity = new restaurentEntities();
protected void Page_Load(object
sender, EventArgs e)
{
if
(!IsPostBack)
{
gvbind();
}
}
/// <summary>
/// Method for bind GridView
/// </summary>
protected void gvbind()
{
gvstudent.DataSource = entity.stu_info.Select(x => x).ToList();
gvstudent.DataBind();
}
/// <summary>
/// Method for creating row into edit mode
/// </summary>
/// <param
name="sender"></param>
/// <param
name="e"></param>
protected void gvstudent_RowEditing(object
sender, GridViewEditEventArgs e)
{
gvstudent.EditIndex = e.NewEditIndex;
gvbind();
}
after click on Edit button in GridView will be look like as givent below:-
/// <summary>
/// Method for cancel Edit
/// </summary>
/// <param
name="sender"></param>
/// <param
name="e"></param>
protected void gvstudent_RowCancelingEdit(object sender, GridViewCancelEditEventArgs
e)
{
gvstudent.EditIndex = -1;
gvbind();
}
/// <summary>
/// Method for Deletion row on behalf of Id(primary key)
/// </summary>
/// <param
name="sender"></param>
/// <param
name="e"></param>
protected void gvstudent_RowDeleting(object
sender, GridViewDeleteEventArgs e)
{
int id
= Convert.ToInt32(((Label)gvstudent.Rows[e.RowIndex].Cells[0].Controls[1]).Text);
var v =
entity.stu_info.Where(x => x.id == id).Select(x => x).FirstOrDefault();
if (v
!= null)
{
entity.stu_info.DeleteObject(v);
entity.SaveChanges();
}
gvbind();
}
/// <summary>
/// Method for Add new Record and binding GridView
/// </summary>
/// <param name="sender"></param>
/// <param
name="e"></param>
protected void Button1_Click(object
sender, EventArgs e)
{
stu_info
st = new stu_info();
st.name = txtname.Text;
st.stu_add = txtaddress.Text;
entity.AddTostu_info(st);
entity.SaveChanges();
gvbind();
}
/// <summary>
/// Method for updating a particular row on behalf of id(
Primary Key)
/// </summary>
/// <param
name="sender"></param>
/// <param
name="e"></param>
protected void gvstudent_RowUpdating(object
sender, GridViewUpdateEventArgs e)
{
int id
= Convert.ToInt32(((Label)gvstudent.Rows[e.RowIndex].Cells[0].Controls[1]).Text);
TextBox
textname = (TextBox)gvstudent.Rows[e.RowIndex].Cells[1].FindControl("txtname");
TextBox
textaddress = (TextBox)gvstudent.Rows[e.RowIndex].Cells[2].FindControl("txtaddress");
var v =
entity.stu_info.Where(x => x.id == id).Select(x => x).FirstOrDefault();
if (v
!= null)
{
v.name = textname.Text;
v.stu_add = textaddress.Text;
entity.SaveChanges();
}
gvstudent.EditIndex = -1;
gvbind();
}
}