Search This Blog

Tuesday, August 31, 2010

Get GridView Row and GridView Row Index on RowCommand Event

Below is a simple ASP.Net GridView Control with a ButtonField and an OnRowCommand Event

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns = "false" 
    OnRowCommand = "OnRowCommand">
 <Columns>
  <asp:ButtonField CommandName = "ButtonField"  DataTextField = "CustomerID"
        ButtonType = "Button"/>
 </Columns>
</asp:GridView>
Now when the Button of the button is clicked the OnRowCommand event is executed.

protected void OnRowCommand(object sender, GridViewCommandEventArgs e)
{
    int index = Convert.ToInt32(e.CommandArgument);
    GridViewRow gvRow = GridView1.Rows[index]; 
}

The RowIndex of the GridView Row is contained in the CommandArgument Property. Thus with that index you can get the reference of the GridView Row

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.