Retrieve Row Index ID from GridView
Using Gridview and you want to retrieve row index id (in consequence the Data value ID of it).
here is the trick.
HTML
<
asp:GridView ID="FilmList" DataKeyNames="FilmID" OnRowCommand="FilmList_RowCommand" runat="server" AutoGenerateColumns="false" ><
Columns><asp:TemplateField>
<
asp:ImageButton ID="ImageButton1" runat="server" ImageUrl="~/Images/submyscore.jpg" CommandArgument="<%# Container.DataItemIndex %>" CommandName="Vote" /> </td></
ItemTemplate></asp:TemplateField>
CODE.
protected
void FilmList_RowCommand(object sender, GridViewCommandEventArgs e)
{
//reference to http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowcommand.aspx
if (e.CommandName == "Vote")
{
// Convert the row index stored in the CommandArgument
// property to an Integer.
int index = Convert.ToInt32(e.CommandArgument); // This is the Gridview Row Index!
int filmid = (int)FilmList.DataKeys[index]["FilmID"]; //The value of FilmID;
// Retrieve the row that contains the button clicked
// by the user from the Rows collection.
GridViewRow row = FilmList.Rows[index];
Label lbl = (Label)row.FindControl("Label1");
lbl.Text =
"Dected row index!: " + index.ToString() + " and FilmID is:" + filmid.ToString() ;
}
}