How to set and read multiple DataKeyNames in Gridview 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 read DataKeyNames on GridView's RowUpdating Event in asp.net and How to read DataKeyNames on GridView's RowUpdating Event in asp.net.

Now in this article i  will explain How to set and read multiple DataKeyNames in Gridview in asp.net. Suppose you have a GridView grdEmp having DataKeyNames EMP_ID and EMP_NAME  and you want to read EMP_ID and EMP_NAME from DataKeyNames on Gridview’s RowEditing or Gridview’s RowUpdating or Gridview’s RowDeleting event then here is the code:
  • To set multiple DataKeyNames write as:
<asp:GridView ID="grdEmp" runat="server"  DataKeyNames="EMP_ID,EMP_NAME">
</GridView>

Note: We are reading from DataKeyNames on GridView's RowDeleting event. The code is same for Gridview’s RowEditing and  Gridview’s RowUpdating events

C#.Net Code to set and read multiple DataKeyNames in Gridview in asp.net
protected void grdEmp_ RowDeleting (object sender, GridViewEditEventArgs e)
    {     
int empId = Convert.ToInt32(grdEmp.DataKeys[e.RowIndex].Values["EMP_ID"].ToString());
string empName = grdEmp.DataKeys[e.RowIndex].Values["EMP_NAME"].ToString();
    }

VB.Net Code to set and read multiple DataKeyNames in Gridview in asp.net

Protected Sub grdEmp_RowDeleting(sender As Object, e As GridViewEditEventArgs)
Dim empId As Integer = Convert.ToInt32(grdEmp.DataKeys(e.RowIndex).Values("EMP_ID").ToString())
Dim empName As String = grdEmp.DataKeys(e.RowIndex).Values("EMP_NAME").ToString()
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..