ASP.NET GridView RowIndex as CommandArgument

How can you access and display the row index of a gridview element as an argument to a command in a button in a button column of a button?

<gridview> <Columns> <asp:ButtonField ButtonType="Button" CommandName="Edit" Text="Edit" Visible="True" CommandArgument=" ? ? ? " /> ..... 
+55
c # gridview
Dec 23 '08 at 16:48
source share
8 answers

Here is a very simple way:

 <asp:ButtonField ButtonType="Button" CommandName="Edit" Text="Edit" Visible="True" CommandArgument='<%# Container.DataItemIndex %>' /> 
+90
Dec 29 '09 at 19:06
source share
β€” -

MSDN says that:

The ButtonField class automatically populates the CommandArgument property with the appropriate index value. For other command buttons, you must manually set the CommandArgument property of the command button. For example, you can set CommandArgument to <% # Container.DataItemIndex%> when the GridView control has no paging.

Therefore, you do not need to install it manually. Then the row command with GridViewCommandEventArgs will make it available; eg.

 protected void Whatever_RowCommand( object sender, GridViewCommandEventArgs e ) { int rowIndex = Convert.ToInt32( e.CommandArgument ); ... } 
+35
Feb 24 '10 at 17:43
source share

Here is the Microsoft suggestion for this http://msdn.microsoft.com/en-us/library/bb907626.aspx#Y800

In gridview, add a command button and convert it to a template, and then give it the command name in this case " AddToCart ", and also add CommandArgument "<% # ((GridViewRow) Container) .RowIndex%>"

 <asp:TemplateField> <ItemTemplate> <asp:Button ID="AddButton" runat="server" CommandName="AddToCart" CommandArgument="<%# ((GridViewRow) Container).RowIndex %>" Text="Add to Cart" /> </ItemTemplate> </asp:TemplateField> 

Then, to create the RowCommand gridview event, specify when the "AddToCart" command is run, and do what you want from there

 protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e) { if (e.CommandName == "AddToCart") { // Retrieve the row index stored in the // CommandArgument property. int index = Convert.ToInt32(e.CommandArgument); // Retrieve the row that contains the button // from the Rows collection. GridViewRow row = GridView1.Rows[index]; // Add code here to add the item to the shopping cart. } } 

** One mistake I made was that I wanted to add actions to the button on the template instead of doing it directly on the RowCommand event.

+11
Feb 17 '11 at 4:12
source share

I think it will work.

 <gridview> <Columns> <asp:ButtonField ButtonType="Button" CommandName="Edit" Text="Edit" Visible="True" CommandArgument="<%# Container.DataItemIndex %>" /> </Columns> </gridview> 
+5
Dec 23 '08 at 17:04
source share
 void GridView1_RowCommand(object sender, GridViewCommandEventArgs e) { Button b = (Button)e.CommandSource; b.CommandArgument = ((GridViewRow)sender).RowIndex.ToString(); } 
+1
Dec 23 '08 at 17:23
source share

I usually bind this data using the RowDatabound event with a GridView:

 protected void FormatGridView(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { ((Button)e.Row.Cells(0).FindControl("btnSpecial")).CommandArgument = e.Row.RowIndex.ToString(); } } 
0
Dec 23 '08 at 16:56
source share
 <asp:TemplateField HeaderText="" ItemStyle-Width="20%" HeaderStyle-HorizontalAlign="Center"> <ItemTemplate> <asp:LinkButton runat="server" ID="lnkAdd" Text="Add" CommandName="Add" CommandArgument='<%# Eval("EmpID"))%>' /> </ItemTemplate> </asp:TemplateField> 

This is the traditional way and the latest version of asp.net infrastructure with strongly typed data, and you do not need to use, for example, "EMPID" as a string

0
Jan 18 '12 at 10:25
source share
 <asp:LinkButton ID="LnkBtn" runat="server" Text="Text" RowIndex='<%# Container.DisplayIndex %>' CommandArgument='<%# Eval("??") %>' OnClick="LnkBtn_Click" /> 

inside the event handler:

 Protected Sub LnkBtn_Click(ByVal sender As Object, ByVal e As EventArgs) dim rowIndex as integer = sender.Attributes("RowIndex") 'Here you can use also the command argument for any other value. End Sub 
-one
Jun 16 '15 at 9:48
source share



All Articles