In this post we will discuss how to implement delete mechanism in gridview in asp.net. Also you can check out my previous posts on:
- Sql Server 2008 joins tutorial
- Asp.Net MVC interview questions and answers
- Retrieve browser details using C#.Net
As shown in the figure below, if you are showing some users and want to delete one user when someone clicks on the Delete link button inside the gridview.
Below is the full code:
Gridview code:
Here we need to handle the OnRowDeleting and OnRowCommand command event.
In the Delete button we have set the CommandArgument to the UserID which is the ID through which we will write the code to delete the record.
We have also set the CommandName as "Delete" which you can set any thing but you need to check in the OnRowCOmmand event.
It will also ask you a confirmation message before deleting the record.
.CS Code:
Hope you will able to delete the record.
- Sql Server 2008 joins tutorial
- Asp.Net MVC interview questions and answers
- Retrieve browser details using C#.Net
As shown in the figure below, if you are showing some users and want to delete one user when someone clicks on the Delete link button inside the gridview.
Below is the full code:
Gridview code:
<asp:GridView ID="GridView1"
runat="server"
AutoGenerateColumns="false"
OnRowCommand="GridView1_RowCommand"
OnRowDeleting="GridView1_RowDeleting">
<Columns>
<asp:TemplateField HeaderText="User Name">
<ItemTemplate>
<asp:HyperLink ID="hypUserName" runat="server" Text='<%#Eval("UserName") %>'></asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Email ID">
<ItemTemplate>
<asp:Label ID="lblEmailID"
runat="server"
Text='<%#Eval("EmailID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="LinkButton1"
runat="server"
CommandArgument='<%#Eval("UserID") %>'
CommandName="Delete"
OnClientClick="return
confirm('Are you sure you want to delete this user?');">Delete</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Here we need to handle the OnRowDeleting and OnRowCommand command event.
In the Delete button we have set the CommandArgument to the UserID which is the ID through which we will write the code to delete the record.
We have also set the CommandName as "Delete" which you can set any thing but you need to check in the OnRowCOmmand event.
It will also ask you a confirmation message before deleting the record.
.CS Code:
using System;
using
System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using
System.Web.UI.WebControls;
public partial class Default4 : System.Web.UI.Page
{
protected void
Page_Load(object sender, EventArgs e)
{
if
(!IsPostBack)
{
BindGridView();
}
}
protected void
GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
int
UserID = Convert.ToInt32(e.CommandArgument);
if
(e.CommandName == "Delete")
{
//Write
your code to delete the user based on the above UserID
}
}
protected void
GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
//Here no
need to write any logic inside.
}
void BindGridView()
{
//Here is the
code to Bind the gridview
}
}
Hope you will able to delete the record.