To get the Primary Key reference we can use the Data Keys collection of the Grid View.

This collection only requires the row index no and it will give us the primary key.

gridview rowupdating get id-42

In the above code, I set two properties Show Edit Button and Show Cancel Button of Command Field.

These properties will automatically render Edit, Update and Cancel buttons inside Grid View column.

If we use proper command names for the buttons like “EDIT”, “DELETE”, “UPDATE”, “CANCEL” these will also trigger the appropriate Grid View Events.

Ok let’s move on to Code behind what we have to do when user clicks Edit, Delete, Update and Cancel When we click on Edit the On Row Editing event will be fired.

Now I will show you the C# code behind file of the above ASP.

NET page that will handle different Grid View events to provide complete editing functionality.

I am using Template Field’s Item Template and Edit Item Template for providing read only and editing interface of the same column in the Grid View.

By Default, Grid View control displays Item Template which directly shows the Product Name and Unit Price columns but when user clicks Edit button Grid View automatically switches to Edit Item Template and display the control available inside Edit Item Template for user editing.

Then again we have to call that Bind Grid Data () method to bind data for the textboxes. You can leave a response, or trackback from your own site.