Asp.Net GridView get current row on TemplateField

asp.netgridview

I have a gridView binded to a data source, and I have a ButtonField as one of the columns in the grid to perform a custom action.

When I use the ButtonField, the CommandArgument of the event raised by it has the zero-based index of the row containing the clicked ButtonField.

I want to convert the ButtonField to a TemplateField so I can add a control to the Footer of the column, the problem is that when I convert it to a TemplateField, the CommandArgument no longer has the row index.

Is there a way to get such index on a TemplateField?

I cannot use the gridView.SelectedIndex because the button can be clicked on a row that isn't the selected one.

Thanks for your help

Best Answer

In the LinkButton (or whatever) in your TemplateField you can set the CommandArgument to the following to get the row index:

CommandArgument='<%# Container.DataItemIndex %>'