TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
C# Corner
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
GridView: Update and Delete data in GridView Using ASP.NET C#
Mohammed Zaid Meraj
Mar 05
2016
Code
2.2
k
0
0
facebook
twitter
linkedIn
Reddit
WhatsApp
Email
Bookmark
expand
HTML Markup:
<asp:GridView ID=
"dgvDesignation"
class
=
"table table-striped table-bordered table-hover"
runat=
"server"
AllowPaging=
"True"
AllowSorting=
"True"
AutoGenerateColumns=
"False"
OnPageIndexChanging=
"dgvDesignation_PageIndexChanging"
OnRowCancelingEdit=
"dgvDesignation_RowCancelingEdit"
OnRowDeleting=
"dgvDesignation_RowDeleting"
OnRowEditing=
"dgvDesignation_RowEditing"
OnRowUpdating=
"dgvDesignation_RowUpdating"
>
<Columns>
<asp:TemplateField HeaderText=
"Designation_id"
Visible=
"False"
>
<EditItemTemplate>
<asp:Label ID=
"lblEditDesignationid"
runat=
"server"
Text=
'<%# Bind("Bank_id") %>'
></asp:Label>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID=
"lblDesignationid"
runat=
"server"
Text=
'<%# Bind("Bank_id") %>'
></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText=
"Bank Name"
>
<EditItemTemplate>
<asp:TextBox ID=
"txtEditDesignationName"
runat=
"server"
Text=
'<%# Bind("Bank_name") %>'
></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID=
"lblDesignationName"
runat=
"server"
Text=
'<%# Bind("Bank_name") %>'
></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<EditItemTemplate>
<asp:ImageButton ID=
"imgbtnUpdate"
runat=
"server"
CommandName=
"Update"
ImageUrl=
"~/Image/update.jpg"
ToolTip=
"Update"
Width=
"20px"
/>
<asp:ImageButton ID=
"imgbtnCancle"
runat=
"server"
CommandName=
"Cancel"
ImageUrl=
"~/Image/Cancel.jpg"
ToolTip=
"Cancel"
Width=
"20px"
/> </EditItemTemplate>
<ItemTemplate>
<asp:ImageButton ID=
"imgbtnEdit"
runat=
"server"
CommandName=
"Edit"
ImageUrl=
"~/Image/Edit.jpg"
ToolTip=
"Edit"
Width=
"20px"
/>
<asp:ImageButton ID=
"imgbtnDelete"
runat=
"server"
CommandName=
"Delete"
ImageUrl=
"~/Image/delete.jpg"
ToolTip=
"Delete"
Width=
"20px"
/> </ItemTemplate>
<HeaderStyle Width=
"60px"
/>
<ItemStyle Width=
"60px"
/> </asp:TemplateField>
</Columns>
</asp:GridView>
C#
protected
void
Page_Load(
object
sender, EventArgs e)
{
if
(!IsPostBack)
{
FillGrid();
//lblMsg.Visible = false;
}
}
private
void
FillGrid()
{
string
str =
"Select * from tbl_bank order by Bank_name"
;
DataTable dt_Designation = c1.filldt(str);
dgvDesignation.DataSource = dt_Designation;
dgvDesignation.DataBind();
}
private
void
Save()
{
string
str =
"Insert into tbl_bank (Bank_name) values('"
+ txtDesignationName.Text.Trim() +
"')"
;
c1.IUD(str);
FillGrid();
}
protected
void
dgvDesignation_PageIndexChanging(
object
sender, GridViewPageEventArgs e)
{
dgvDesignation.PageIndex = e.NewPageIndex;
FillGrid();
}
protected
void
dgvDesignation_RowCancelingEdit(
object
sender, GridViewCancelEditEventArgs e)
{
dgvDesignation.EditIndex = -1;
FillGrid();
}
protected
void
dgvDesignation_RowEditing(
object
sender, GridViewEditEventArgs e)
{
dgvDesignation.EditIndex = e.NewEditIndex;
FillGrid();
}
protected
void
dgvDesignation_RowUpdating(
object
sender, GridViewUpdateEventArgs e)
{
Label id = (Label) dgvDesignation.Rows[e.RowIndex].FindControl(
"lblEditDesignationId"
);
TextBox name = (TextBox) dgvDesignation.Rows[e.RowIndex].FindControl(
"txtEditDesignationName"
);
string
str =
"Update tbl_bank set Bank_name='"
+ name.Text +
"' where Bank_id="
+ Convert.ToInt32(id.Text);
c1.IUD(str);
//lblSave.Text = "Record Update Successfully";
//lblSave.Visible = true;
dgvDesignation.EditIndex = -1;
FillGrid();
}
protected
void
dgvDesignation_RowDeleting(
object
sender, GridViewDeleteEventArgs e)
{
Label id = (Label) dgvDesignation.Rows[e.RowIndex].FindControl(
"lblDesignationid"
);
string
str =
"Delete from tbl_bank where Bank_id="
+ Convert.ToInt32(id.Text);
c1.IUD(str);
//lblSave.Text = "Record Delete Successfully";
//lblSave.Visible = true;
FillGrid();
}
protected
void
btnCancle_Click(
object
sender, EventArgs e)
{}
protected
void
btnSave_Click(
object
sender, EventArgs e)
{
Save();
}
}
GridView
Upadate data in GridView