Friday, October 8, 2010

DataKey from a GridView RowCommand Event

Method to get DataKey from a GridView RowCommand Event

protected void grdEmployees_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Select")
{
GridViewRow row = (GridViewRow)((LinkButton)e.CommandSource).NamingContainer;
int index = row.RowIndex;
int employeeID = Convert.ToInt32(grdEmployees.DataKeys[index].Value);
}

}

For this eg: There should be GridView Control with a TemplateField and that should contain a Linkbutton with CommandName="Select"

4 comments:

  1. usefull...Worked for me in rowcommand event

    ReplyDelete
  2. It's Really good....

    ReplyDelete
  3. very simply expalained...thnx alot

    ReplyDelete