How to read DataKeyNames on GridView's RowUpdating Event in asp.net

Introduction: In previous articles i explained How to read DataKeyNames on GridView's RowDeleting event in asp.net and How to read DataKeyNames from GridView on RowEditing event of gridview and How to read DataKeyName from GridView onSelectedindexChanging event of GridView 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 RowUpdating Event in asp.net.Suppose you have a GridView grdEmp having DataKeyNames DataKeynames EMP_ID  and you want to read EMP_ID from DataKeyNames on Gridview’s RowUpdating Event then here is  the code:

Note: We are assuming EMP_ID is the primary key of the Employee table

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

C#.Net Code to read DataKeyNames on GridView's RowUpdating Event in asp.net

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

VB.Net Code to read DataKeyNames on GridView's RowUpdating Event in asp.net
Protected Sub grdEmp_RowUpdating(sender As Object, e As GridViewUpdateEventArgs)
                    Dim empId As Integer = Convert.ToInt32(grdEmp.DataKeys(e.RowIndex).Value) 'First way
                    Dim empId As Integer = Convert.ToInt32(grdEmp.DataKeys(e.RowIndex).Value.ToString())             'Second way
                    Dim empId As Integer = Convert.ToInt32(grdEmp.DataKeys(e.RowIndex).Values("EMP_ID").ToString())   'Third way
End Sub

Now over to you:

"If you like my work; you can appreciate by leaving your comments, hitting Facebook like button, following on Google+, Twitter, Linked in and Pinterest, stumbling my posts on stumble upon and subscribing for receiving free updates directly to your inbox . Stay tuned for more technical updates. "

Previous
Next Post »

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..