Get RowIndex In GridView RowCommand Event Using DataKey

Get RowIndex Of GridView Row In RowCommand Event Using DataKey or CommandArgument in asp.net

In this post i am showing how to get RowIndex of gridview row in RowCommand Event through DataKey property or commandArgument.

RowCommand event is raised when a button inside gridview is clicked.

Method 1.

To get gridview rowindex through DataKey property we can use code as mentioned below.

For this we need to define DataKeyNames property in gridview.

HTML SOURCE
<asp:GridView ID="GridView1" runat="server" 
              AutoGenerateColumns="False" 
              DataSourceID="SqlDataSource1" 
              onrowcommand="GridView1_RowCommand" 
              DataKeyNames="DocID">
<Columns>
<asp:BoundField DataField="DocID" HeaderText="DocID" 
                InsertVisible="False" 
                ReadOnly="True" 
                SortExpression="DocID" />

<asp:ButtonField ButtonType="Button"  
                CommandName="Show" 
                Text="Show"/>
                
</Columns>
</asp:GridView>

Write following code in RowCommand Event of gridview to get rowindex or row number in which button was clicked.

C# CODE
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Show")
        {
            int index = Convert.ToInt32(e.CommandArgument);
            int documentID = Convert.ToInt32(GridView1.DataKeys[index].Value);
           
             // Write your further code
            
        }
    }

VB.NET
Protected Sub GridView1_RowCommand(sender As Object, e As GridViewCommandEventArgs)
 If e.CommandName = "Show" Then
  Dim index As Integer = Convert.ToInt32(e.CommandArgument)
  Dim documentID As Integer = Convert.ToInt32(GridView1.DataKeys(index).Value)
                 
                ' Write your further code
 End If
End Sub


Method 2.

To get rowindex without using DataKey we can write code as mentioned below.

C#
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Show")
        {
            int index = Convert.ToInt32(e.CommandArgument);
            GridViewRow row = GridView1.Rows[index];
            int docID = Convert.ToInt32(row.Cells[0].Text);
           
            // Write your further code here 
        }
     }

VB.NET
Protected Sub GridView1_RowCommand(sender As Object, e As GridViewCommandEventArgs)
 If e.CommandName = "Show" Then
  Dim index As Integer = Convert.ToInt32(e.CommandArgument)
  Dim row As GridViewRow = GridView1.Rows(index)
  Dim docID As Integer = Convert.ToInt32(row.Cells(0).Text)

               ' Write your further code here 
 End If
End Sub

If we have placed controls like label,textbox or dropdowns in the gridview row then we can access these controls in following manner.

HTML SOURCE
<asp:GridView ID="GridView1" runat="server" 
              AutoGenerateColumns="False" 
              DataSourceID="SqlDataSource1" 
              onrowcommand="GridView1_RowCommand">
<Columns>
<asp:BoundField DataField="DocID" HeaderText="DocID" 
                InsertVisible="False" 
                ReadOnly="True" 
                SortExpression="DocID" />

<asp:ButtonField ButtonType="Button"  
                CommandName="Show" 
                Text="Show"/>
                
<asp:TemplateField HeaderText="Message">
<ItemTemplate>
<asp:Label ID="lblMessage" runat="server" 
           Text='<%#Eval("DocName") %>'>
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>

C#
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Show")
        {
            int index = Convert.ToInt32(e.CommandArgument);
            GridViewRow row = GridView1.Rows[index];
            int docID = Convert.ToInt32(row.Cells[0].Text);
            string labelText = ((Label)row.FindControl("lblMessage")).Text.ToString();
        }
    }

If we want to place a button inside TemplateField then we need to define CommandArgument property manually in html source as follows.

<asp:TemplateField>
 <ItemTemplate>                
 <asp:Button runat="server" ID="btnDn"
             Text="Down" CommandName="Show" 
CommandArgument="<%#((GridViewRow)Container).RowIndex%>"/>
</ItemTemplate>
</asp:TemplateField>

Hope this helps


If you like this post than join us or share

Find More Articles