Want to become a Vibe Coder? Join Vibe Coding Training here
x
C# Corner
Tech
News
Videos
Forums
Jobs
Books
Events
More
Interviews
Live
Learn
Training
Career
Members
Blogs
Challenges
Certification
Contribute
Article
Blog
Video
Ebook
Interview Question
Collapse
Feed
Dashboard
Wallet
Learn
Achievements
Network
Refer
Rewards
SharpGPT
Premium
Contribute
Article
Blog
Video
Ebook
Interview Question
Register
Login
Add,Insert,Update,Delete data Inside Gridview
WhatsApp
Karthikeyan K
Apr 22
2015
3.3
k
0
0
<
%@ Page
Language
=
"C#"
AutoEventWireup
=
"true"
CodeFile
=
"Default.aspx.cs"
Inherits
=
"_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
=
"Gridview1"
DataKeyNames
=
"ID"
runat
=
"server"
ShowFooter
=
"true"
OnRowCommand
=
"RowCommand"
AutoGenerateColumns
=
"false"
OnRowCancelingEdit
=
"RowCancelingEdit"
OnRowEditing
=
"RowEditing"
OnRowUpdating
=
"RowUpdating"
OnRowDeleting
=
"RowDeleting"
CellPadding
=
"4"
RowStyle-BackColor
="Aquamarine"
HeaderStyle
-BackColor
=
"AliceBlue"
>
<
Columns
>
<
asp:TemplateField
>
<
ItemTemplate
>
<
asp:Button
ID
=
"Button_edit"
runat
=
"server"
Text
=
"Edit"
CommandName
=
"Edit"
/>
<
asp:Button
ID
=
"Button_Delete"
runat
=
"server"
Text
=
"Delete"
CommandName
=
"Delete"
/>
</
ItemTemplate
>
<
EditItemTemplate
>
<
asp:Button
ID
=
"Button_update"
runat
=
"server"
Text
=
"Update"
CommandName
=
"Update"
/>
<
asp:Button
ID
=
"Button_Cancel"
runat
=
"server"
Text
=
"Cancel"
CommandName
=
"Cancel"
/>
</
EditItemTemplate
>
<
FooterTemplate
>
<
asp:Button
ID
=
"Button_Add"
runat
=
"server"
Text
=
"Add"
CommandName
=
"Add"
/>
</
FooterTemplate
>
</
asp:TemplateField
>
<
asp:TemplateField
HeaderText
=
"ID"
>
<
ItemTemplate
>
<
asp:Label
ID
=
"label1_ID"
runat
=
"server"
Text
=
'<%#Eval("ID")%>'
>
</
asp:Label
>
</
ItemTemplate
>
<
EditItemTemplate
>
<
asp:Label
ID
=
"label1_ID"
runat
=
"server"
Text
=
'<%#Eval("ID")%>'
>
</
asp:Label
>
</
EditItemTemplate
>
<
FooterTemplate
>
<
asp:TextBox
ID
=
"Add_Text"
runat
=
"server"
>
</
asp:TextBox
>
</
FooterTemplate
>
</
asp:TemplateField
>
<
asp:TemplateField
HeaderText
=
"Name"
>
<
ItemTemplate
>
<
asp:Label
ID
=
"label_Name"
runat
=
"server"
Text
=
'<%#Eval("Name")%>'
>
</
asp:Label
>
</
ItemTemplate
>
<
EditItemTemplate
>
<
asp:TextBox
ID
=
"ID_Text1"
runat
=
"server"
Text
=
'<%#Eval("Name") %>'
>
</
asp:TextBox
>
</
EditItemTemplate
>
<
FooterTemplate
>
<
asp:TextBox
ID
=
"Add_Text1"
runat
=
"server"
>
</
asp:TextBox
>
</
FooterTemplate
>
</
asp:TemplateField
>
<
asp:TemplateField
HeaderText
=
"Address"
>
<
ItemTemplate
>
<
asp:Label
ID
=
"label_Address"
runat
=
"server"
Text
=
'<%#Eval("Address")%>'
>
</
asp:Label
>
</
ItemTemplate
>
<
EditItemTemplate
>
<
asp:TextBox
ID
=
"ID_Text2"
runat
=
"server"
Text
=
'<%#Eval("Address") %>'
>
</
asp:TextBox
>
</
EditItemTemplate
>
<
FooterTemplate
>
<
asp:TextBox
ID
=
"Add_Text2"
runat
=
"server"
>
</
asp:TextBox
>
</
FooterTemplate
>
</
asp:TemplateField
>
</
Columns
>
</
asp:GridView
>
</
div
>
</
form
>
</
body
>
</
html
>
C# Codings
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Web;
using
System.Web.UI;
using
System.Data;
using
System.Web.UI.WebControls;
using
System.Data.SqlClient;
public
partial
class
_Default: System.Web.UI.Page
{
protected
void
Page_Load(
object
sender, EventArgs e)
{
if
(!IsPostBack)
{
binddata();
}
}
protected
void
binddata()
{
DataTable dt =
new
DataTable();
SqlConnection con =
new
SqlConnection(@
"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\Karthik\Documents\Visual Studio 2010\WebSites\WebSite3\App_Data\Database.mdf;Integrated Security=True;User Instance=True;"
);
con.Open();
SqlDataAdapter sda =
new
SqlDataAdapter(
"select * from Table1"
, con);
sda.Fill(dt);
Gridview1.DataSource = dt;
Gridview1.DataBind();
con.Close();
}
protected
void
RowEditing(
object
sender, System.Web.UI.WebControls.GridViewEditEventArgs e)
{
Gridview1.EditIndex = e.NewEditIndex;
binddata();
}
protected
void
RowCancelingEdit(
object
sender, System.Web.UI.WebControls.GridViewCancelEditEventArgs e)
{
Gridview1.EditIndex = -1;
binddata();
}
protected
void
RowCommand(
object
sender, System.Web.UI.WebControls.GridViewCommandEventArgs e)
{
if
(e.CommandName.Equals(
"Add"
))
{
TextBox ID = Gridview1.FooterRow.FindControl(
"Add_Text"
)
as
TextBox;
TextBox Name = Gridview1.FooterRow.FindControl(
"Add_Text1"
)
as
TextBox;
TextBox Address = Gridview1.FooterRow.FindControl(
"Add_Text2"
)
as
TextBox;
SqlConnection con =
new
SqlConnection(@
"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\Karthik\Documents\Visual Studio 2010\WebSites\WebSite3\App_Data\Database.mdf;Integrated Security=True;User Instance=True;"
);
con.Open();
SqlCommand cmd =
new
SqlCommand();
cmd =
new
SqlCommand(
"insert into Table1(ID,Name,Address) values('"
+ ID.Text +
"','"
+ Name.Text +
"','"
+ Address.Text +
"')"
, con);
cmd.ExecuteNonQuery();
con.Close();
Gridview1.EditIndex = -1;
binddata();
}
}
protected
void
RowDeleting(
object
sender, System.Web.UI.WebControls.GridViewDeleteEventArgs e)
{
int
ID = Convert.ToInt32(Gridview1.DataKeys[e.RowIndex].Values[
"ID"
].ToString());
TextBox Name = Gridview1.Rows[e.RowIndex].FindControl(
"ID_Text1"
)
as
TextBox;
TextBox Address = Gridview1.Rows[e.RowIndex].FindControl(
"ID_Text2"
)
as
TextBox;
SqlConnection con =
new
SqlConnection(@
"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\Karthik\Documents\Visual Studio 2010\WebSites\WebSite3\App_Data\Database.mdf;Integrated Security=True;User Instance=True;"
);
con.Open();
SqlCommand cmd =
new
SqlCommand(
"delete from Table1 where ID="
+ ID, con);
cmd.ExecuteNonQuery();
con.Close();
binddata();
}
protected
void
RowUpdating(
object
sender, System.Web.UI.WebControls.GridViewUpdateEventArgs e)
{
int
ID = Convert.ToInt32(Gridview1.DataKeys[e.RowIndex].Values[
"ID"
].ToString());
TextBox Name = Gridview1.Rows[e.RowIndex].FindControl(
"ID_Text1"
)
as
TextBox;
TextBox Address = Gridview1.Rows[e.RowIndex].FindControl(
"ID_Text2"
)
as
TextBox;
SqlConnection con =
new
SqlConnection(@
"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\Karthik\Documents\Visual Studio 2010\WebSites\WebSite3\App_Data\Database.mdf;Integrated Security=True;User Instance=True;"
);
con.Open();
SqlCommand cmd =
new
SqlCommand();
cmd =
new
SqlCommand(
"update Table1 set Name='"
+ Name.Text +
"',Address='"
+ Address.Text +
"'where ID="
+ ID, con);
cmd.ExecuteNonQuery();
con.Close();
Gridview1.EditIndex = -1;
binddata();
}
}
Demo
Gridview
Up Next
Add,Insert,Update,Delete data Inside Gridview