Related Posts Plugin for WordPress, Blogger...

About

Follow Us

Thursday, 5 February 2015

Introduction: 

 In this article we will discuss different way to get DataKeyNames from GridView on RowEditing event. Suppose you have a GridView grdEmp having DataKeyNames empid  and you want to read empid from DataKeyNames on Gridview’s RowEditing event then here is the code:

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



C#.NET Code to get  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  to get value
int empId = Convert.ToInt32(grdEmp.DataKeys[e.RowIndex].Value.ToString());//Second way to get value
int empId = Convert.ToInt32(grdEmp.DataKeys[e.RowIndex].Values["empid "].ToString());//Third way to get value
    }

VB.NET Code to get  DataKeyNames from GridView on RowEditing event of gridview

Protected Sub grdEmp_RowEditing(sender As Object, e As GridViewEditEventArgs
)

'First way to get value
 Dim empId As Integer = Convert.ToInt32(grdEmp.DataKeys(e.RowIndex).Value)   

 'Second way to get value
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

0 comments:

Post a Comment