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=" ? ? ? " />
.....
Use the RowIndex property to determine the index of the GridViewRow object in the Rows collection of a GridView control. This property applies only to data rows.
You can get the data on gridview_RowCommand event by placing below code: Int32 HistoryId = Convert. ToInt32(e. Row.
As the object suggests (GridViewUpdateEventArgs) 'e' stands for the events relating to the update of a grid view. You can get similar method signatures that relate to other events such as deletions etc. The 'RowIndex' relates to the index of the row on which this event was fired.
Here is a very simple way:
<asp:ButtonField ButtonType="Button" CommandName="Edit" Text="Edit" Visible="True"
CommandArgument='<%# Container.DataItemIndex %>' />
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 the CommandArgument to <%# Container.DataItemIndex %> when the GridView control has no paging enabled.
So you shouldn't need to set it manually. A row command with GridViewCommandEventArgs would then make it accessible; e.g.
protected void Whatever_RowCommand( object sender, GridViewCommandEventArgs e )
{
int rowIndex = Convert.ToInt32( e.CommandArgument );
...
}
Here is Microsoft Suggestion for this http://msdn.microsoft.com/en-us/library/bb907626.aspx#Y800
On the gridview add a command button and convert it into a template, then give it a commandname 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 for create on the RowCommand event of the gridview identify when the "AddToCart" command is triggered, and do whatever 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 was making is that I wanted to add the actions on my template button instead of doing it directly on the RowCommand Event.
I think this will work.
<gridview>
<Columns>
<asp:ButtonField ButtonType="Button" CommandName="Edit" Text="Edit" Visible="True" CommandArgument="<%# Container.DataItemIndex %>" />
</Columns>
</gridview>
void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
Button b = (Button)e.CommandSource;
b.CommandArgument = ((GridViewRow)sender).RowIndex.ToString();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With