How to read DataKeyNames from GridView on RowEditing event of gridview

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

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

C#.NET Code to read DataKeyNames from GridView on RowEditing event of gridview

protected void grdEmp_ RowEditing (object sender, GridViewEditEventArgs 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 from GridView on RowEditing event of gridview

Protected Sub grdEmp_RowEditing(sender As Object, e As GridViewEditEventArgs)
                    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


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