Vinod
Vinod

Reputation: 4892

Row index of LinkButton in GridView

I am using Link button as Template Field in GridView.

Now i want to display the row index of the Linkbutton clicked.

Please suggest me a solution Thanks in advance

Upvotes: 6

Views: 29152

Answers (4)

nicotina
nicotina

Reputation: 41

Use Container.DisplayIndex inside Page's gridview template. On code behind RowCommand handler, you'll have the row index as argument.

 <asp:TemplateField HeaderText="Actions">
   <ItemTemplate>
     <asp:LinkButton ID="lbExample" runat="server" CommandName="DoAction" CommandArgument='<%# Container.DisplayIndex %>' ToolTip="Action link" CssClass="btn btn-sm btn-light"><i class="fa fa-edit"></i></asp:LinkButton>
   </ItemTemplate>
</asp:TemplateField>

Upvotes: 1

Mujassir Nasir
Mujassir Nasir

Reputation: 1730

Please try this one:

protected void userGridview_RowCommand(object sender, GridViewCommandEventArgs e)
    {
         GridViewRow rowSelect = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer);
            int rowindex = rowSelect.RowIndex;                
    }   

Upvotes: 8

Jignesh Rajput
Jignesh Rajput

Reputation: 3558

suppose in Item Template this is ur link button

  <ItemTemplate>
               <asp:LinkButton ID="lnkapprove" Font-Underline="true" runat="server" Text="Approve"             OnClick="lnkapprove_Click"></asp:LinkButton>
  </ItemTemplate>

in Code Behind:

   protected void lnkapprove_Click(object sender, EventArgs e)
    {
        LinkButton btn = (LinkButton)sender;
        GridViewRow row = (GridViewRow)btn.NamingContainer;
        int i = Convert.ToInt32(row.RowIndex);
    }

you can get row.RowIndex like this..

Hope this helps..

Upvotes: 14

Mujassir Nasir
Mujassir Nasir

Reputation: 1730

You can also try this code in row bound event

GridViewRow rowSelect = (GridViewRow)(((Button)e.CommandSource).NamingContainer);
                int rowindex = rowSelect.RowIndex;

like this

protected void userGridview_RowBound(object sender, GridViewCommandEventArgs e)
    {
         GridViewRow rowSelect = (GridViewRow)(((Button)e.CommandSource).NamingContainer);
            int rowindex = rowSelect.RowIndex;                
    }

Upvotes: 1

Related Questions