How to read DataKeyNames on GridView's RowDeleting event in asp.net

Introduction: In previous articles i explained How to read DataKeyNames from GridView on RowEditing event of gridview in asp.net and How to read DataKeyName from GridView on SelectedindexChanging event of GridView in asp.net and How to read DataKeyNames on GridView's RowUpdating Event in asp.net and How to set and read multiple DataKeyNames in Gridview in asp.net.

Now i will explain How to read DataKeyNames on GridView's RowDeleting event in asp.net.
Suppose you have a GridView grdEmp having DataKeyNames EMP_ID and you want to read EMP_ID from DataKeyNames on Gridview’s RowDeleting event then here is the code:


Place a GridView control on the design page(.aspx)

<asp:GridView ID="grdEmp" runat="server" DataKeyNames="EMP_ID">
</GridView>

C#.Net Code  to read DataKeyNames on GridView's RowDeleting event

protected void grdEmp_ RowDeleting (object sender, GridViewDeleteEventArgs e)
    {     //First way
     int empId = Convert.ToInt32(grdEmp.DataKeys[e.RowIndex].Value); 
      //Second way
     int empId = Convert.ToInt32(grdEmp.DataKeys[e.RowIndex].Value.ToString());
      //Third way
      int empId = Convert.ToInt32(grdEmp.DataKeys[e.RowIndex].Values["EMP_ID"].ToString());
    }

VB.NET Code to read DataKeyNames on GridView's RowDeleting event

Protected Sub grdEmp_RowDeleting(sender As Object, e As GridViewDeleteEventArgs)
    'First way                  
     Dim empId As Integer = Convert.ToInt32(grdEmp.DataKeys(e.RowIndex).Value)
     'Second way                   
     Dim empId As Integer = Convert.ToInt32(grdEmp.DataKeys(e.RowIndex).Value.ToString())           
'Third way                   
   Dim empId As Integer = Convert.ToInt32(grdEmp.DataKeys(e.RowIndex).Values("EMP_ID").ToString()) 
End Sub


Previous
Next Post »

2 comments

Click here for comments
March 04, 2016 ×

A lot of thanks, this help me so much.!.

Reply
avatar
March 26, 2016 ×

Thanks for your valuable feedback.Stay connected and keep reading for more updates.

Reply
avatar

If you have any question about any post, Feel free to ask.You can simply drop a comment below post or contact via Contact Us form. Your feedback and suggestions will be highly appreciated. Also try to leave comments from your account not from the anonymous account so that i can respond to you easily..