get the rowindex in Gridview using Templatefield in ROWCOMMAND event

  • Thread starter Thread starter Muhammad Saad Abbasi
  • Start date Start date
M

Muhammad Saad Abbasi

Hello, I have a problem that I may not be able to get the row index in
template column in grid view. Can anyone help me for this problem.
 
Hello, I also had this problem of getting row index in template column.
I searched alot on internet but was unable to find a solution. So I
tried by myself and got success in it. Here is the solution.

Basically I work in C# but libraries are same for both C#.net and VB.net

I bind that control in RowDataBound

protected void GridView1_RowDataBound(object sender,
GridViewRowEventArgs e)
{
if (e.Row.RowType != DataControlRowType.Header && e.Row.RowType
!= DataControlRowType.Footer && e.Row.RowType !=
DataControlRowType.Pager)
{
LinkButton lblTagLink =new LinkButton();
lblTagLink = (LinkButton)e.Row.FindControl("btnAddTags");
lblTagLink.CommandArgument= e.Row.RowIndex.ToString();
}
}

So got the row index in

protected void GridView1_RowCommand(object sender,
GridViewCommandEventArgs e)
{

int RowIndex_ = int.Parse(e.CommandArgument.ToString());
Label lblMediaID_ =
(Label)GridView1.Rows[RowIndex_].FindControl("lblMedia_ID");

}

as I got the row index, I had access of each control in that
row as mentioned above.
 
Back
Top