Introduction: In previous articles i explained How to read DataKeyNames from GridView on RowEditing event of gridview 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 on GridView's RowDeleting event in asp.net.
Suppose you have a GridView grdEmp having DataKeyNames EMP_ID and you want to read EMP_ID from DataKeyNames on Gridview’s RowDeleting event then here is the code:
Place a GridView control on the design page(.aspx)
Now i will explain How to read DataKeyNames on GridView's RowDeleting event in asp.net.
Suppose you have a GridView grdEmp having DataKeyNames EMP_ID and you want to read EMP_ID from DataKeyNames on Gridview’s RowDeleting event then here is the code:
Place a GridView control on the design page(.aspx)
<asp:GridView ID="grdEmp"
runat="server" DataKeyNames="EMP_ID">
</GridView>
C#.Net Code to read DataKeyNames on GridView's RowDeleting event
protected void grdEmp_ RowDeleting (object sender, GridViewDeleteEventArgs e)
protected void grdEmp_ RowDeleting (object sender, GridViewDeleteEventArgs e)
{ //First way
int empId = Convert.ToInt32(grdEmp.DataKeys[e.RowIndex].Value);
//Second way
//Second way
int empId = Convert.ToInt32(grdEmp.DataKeys[e.RowIndex].Value.ToString());
//Third way
//Third way
int empId = Convert.ToInt32(grdEmp.DataKeys[e.RowIndex].Values["EMP_ID"].ToString());
}
VB.NET Code to read DataKeyNames on GridView's RowDeleting event
Protected Sub grdEmp_RowDeleting(sender
As Object, e As GridViewDeleteEventArgs)
'First way
Dim empId As Integer = Convert.ToInt32(grdEmp.DataKeys(e.RowIndex).Value)
Dim empId As Integer = Convert.ToInt32(grdEmp.DataKeys(e.RowIndex).Value)
'Second way
Dim empId As Integer = Convert.ToInt32(grdEmp.DataKeys(e.RowIndex).Value.ToString())
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())
Dim empId As Integer = Convert.ToInt32(grdEmp.DataKeys(e.RowIndex).Values("EMP_ID").ToString())
End Sub
2 comments
Click here for commentsA lot of thanks, this help me so much.!.
ReplyThanks for your valuable feedback.Stay connected and keep reading for more updates.
ReplyIf 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..