If you are using the TemplateField object and have to access the row index in the event handler method, set the button's CommandArgument property to an expression that identifies the current row. Sample code is:
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="AddButton" runat="server"
CommandName="AddToData"
CommandArgument="<%# ((GridViewRow) Container).RowIndex %>"
Text="Add to Cart" />
</ItemTemplate>
</asp:TemplateField>
Create a method for the RowCommand event of the GridView control as:
protected void GridView1_RowCommand(object sender,
GridViewCommandEventArgs e)
{
if (e.CommandName == "AddToData")
{
}
}