Connecting Tech Pros Worldwide Forums | Help | Site Map

get the rowindex in Gridview using Templatefield in ROWCOMMAND event

Muhammad Saad Abbasi
Guest
 
Posts: n/a
#1: Feb 27 '07
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.



*** Sent via Developersdex http://www.developersdex.com ***
Muhammad Saad Abbasi
Guest
 
Posts: n/a
#2: Feb 28 '07

re: get the rowindex in Gridview using Templatefield in ROWCOMMAND event


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.





*** Sent via Developersdex http://www.developersdex.com ***
Closed Thread