ASP.NET GridView RowIndex As CommandArgument

mirezus picture mirezus · Dec 23, 2008 · Viewed 188.7k times · Source

How can you access and display the row index of a gridview item as the command argument in a buttonfield column button?

<gridview>
<Columns>
   <asp:ButtonField  ButtonType="Button" 
        CommandName="Edit" Text="Edit" Visible="True" 
        CommandArgument=" ? ? ? " />
.....

Answer

George picture George · Dec 29, 2008

Here is a very simple way:

<asp:ButtonField ButtonType="Button" CommandName="Edit" Text="Edit" Visible="True" 
                 CommandArgument='<%# Container.DataItemIndex %>' />