Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Asp.Net GridView get current row on TemplateField

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

like image 292
willvv Avatar asked Sep 07 '09 17:09

willvv


1 Answers

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

CommandArgument='<%# Container.DataItemIndex %>'
like image 163
patmortech Avatar answered Oct 14 '22 15:10

patmortech