How to cancel Update and Delete Operations in a GridView

I was recently asked a simple way to cancel update and delete operation in a GridView. It is quite simple to do so as shown here:

C#

protected void gv_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
e.Cancel = true;
}

void gv_RowDeleting(Object sender, GridViewDeleteEventArgs e)
{
// Check if there is atleast one row left in the GridView
// before deleting
if (gv.Rows.Count <= 1)
{
e.Cancel = true;
}
}

VB.NET

Protected Sub gv_RowUpdating(ByVal sender As Object, _
ByVal e As GridViewUpdateEventArgs)
e.Cancel = True
End Sub

Private Sub
gv_RowDeleting(ByVal sender As Object, _
ByVal e As GridViewDeleteEventArgs)
' Check if there is atleast one row left in the GridView
' before deleting
If gv.Rows.Count <= 1 Then
e.Cancel = True
End If
End Sub
You can also add your own conditions before cancelling these events.

Will you give this article a +1 ? Thanks in advance


About The Author

Suprotim Agarwal
Suprotim Agarwal, ASP.NET Architecture MVP (Microsoft Most Valuable Professional) works as an Architect Consultant and provides consultancy on how to design and develop Web applications.

Suprotim is also the founder and primary contributor to DevCurry, DotNetCurry and SQLServerCurry. He is the Editor of a Developer Magazine called DNC Magazine. He has also written two EBooks 51 Recipes using jQuery with ASP.NET Controls. and The Absolutely Awesome jQuery CookBook

Follow him on twitter @suprotimagarwal

No comments: