GridViewRow from TemplateField

Yesterday I came across a problem wherein I was unable to get the GridViewRow on RowCommand Event when ImageButton was clicked. Googling around I came across a solution which is quite simple.

Here is the Template field in GridView

<TemplateField>
 <ItemTemplate>
 <asp:ImageButton id=”image” runat=”server”>
 </ItemTemplate>
 </TemplateField>

//Code in GridView RowCommand Event.


ImageButton button = ((ImageButton)(e.CommandSource))
 GridViewRow row = button.Parent.Parent

Here button.Parent will give you GridViewCell, and calling GridViewCell.Parent will give you GridViewRow.

Njoi Coding