How to read DataKeyName from GridView on SelectedindexChanging event of GridView

IntroductionIn previous articles i explained with example 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 Read DataKeyNames on GridView's RowUpdating Event and How to set and read multiple DataKeyNames in Gridview in asp.net.
Now in this article i will explain How to read/get/fetch DataKeyName from GridView on SelectedindexChanging event of GridView in asp.net in both the C#.Net and VB.Net languages.

Description: Suppose you have a GridView grdEmp having DataKeyNames EMP_ID and you want to read EMP_ID from DataKeyNames in Gridview’s SelectedIndexChanging Event then below is  the code

Implementation: let's check through an example

NoteWe are assuming EMP_ID is the primary key of the Employee table

      <asp:GridView ID="gridEmp" runat="server"               DataKeyNames="EMP_ID" OnSelectedIndexChanging="gridEmp_SelectedIndexChanging"
     </asp:GridView>

C#.Net Code to get/read/fetch DataKeyName from GridView on SelectedindexChanging event of GridView 

    protected void grdEmp_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
    {
        int empId = Convert.ToInt32(grdEmp.DataKeys[e.NewSelectedIndex].Value);
    } 

VB.Net Code to get/read/fetch DataKeyName from GridView on SelectedindexChanging event of GridView

Protected Sub grdEmp_ SelectedIndexChanging (sender as Object, e As GridViewSelectEventArgs)
Dim empId As Integer = Convert.ToInt32(grdEmp.DataKeys(e.NewSelectedIndex).Value)
    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..