If you have single key in Data Key
DataKeyNames="empId"
then use
int empId= Convert.ToInt32(GridView1.DataKeys[row.RowIndex].Value);
If you have two keye in Data Key
DataKeyNames="empId,depId"
then use
int EmployeeID = Convert.ToInt32(GridView1.DataKeys[row.RowIndex].Values[0]);
As per your comment I have added this below part....
Why you have mentioned EditIndex in the HTML itself?no issue just to confirm...
Please find the code below and check you are doing the same....
HTML
--------
<asp:GridView ID="gvw" runat="server" AutoGenerateColumns="False" CssClass="basix"
Width="408px" CellPadding="4" ForeColor="#333333" GridLines="None" Height="127px"
OnSelectedIndexChanged="gvwExample_SelectedIndexChanged" OnRowDeleting="gvw_RowDeleting1"
OnRowUpdating="gvw_RowUpdating" OnRowCancelingEdit="gvw_RowCancelingEdit"
OnRowEditing="gvw_RowEditing" DataKeyNames="ID">
<Columns>
<asp:BoundField DataField="ID" HeaderText="ID" AccessibleHeaderText="ID" />
<asp:BoundField DataField="MedDoze" HeaderText="DOZE" AccessibleHeaderText="DOZE" />
<asp:CommandField HeaderText="Delete" ShowDeleteButton="True" />
<asp:CommandField ShowEditButton="True" />
</Columns>
<PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />
<HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
</asp:GridView>
Code Behind
-----------
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
fillGrid();
}
}
protected void gvw_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
gvw.EditIndex = -1;
fillGrid();
}
protected void gvw_RowDeleting1(object sender, GridViewDeleteEventArgs e)
{
}
protected void gvw_RowEditing(object sender, GridViewEditEventArgs e)
{
gvw.EditIndex = e.NewEditIndex;
fillGrid();
}
protected void gvw_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
int myID = Convert.ToInt32(gvw.DataKeys[e.RowIndex].Value);
Response.Write(myID);
}
protected void gvwExample_SelectedIndexChanged(object sender, EventArgs e)
{
}
mark it as solution if it answer your question