Click here to Skip to main content
15,896,430 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
hi,

how get parent grid row index while clicking a button in Child grid.

I used code from this link
(nested gridview):How to Get parent gridview rowindex on a child gridview link button click[^]

but it's not working. showing casting type error.

help me ..


Thanks in advance...

Vineetha..
Posted

1 solution

Here gvInner is the inner(child) gridview. The gvInner contains a linkbutton named Edit whose commandname is set to Edit.



protected void gvInner_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Edit")
            {
                GridViewRow inner = (GridViewRow)((LinkButton)e.CommandSource).NamingContainer;
                GridView child = (GridView)inner.Parent.Parent;//This returns the child gridview
                GridViewRow parent = (GridViewRow)child.NamingContainer;// this returns the row of parent gridview.
                int row = parent.RowIndex; // this returns the row index of parent gridview
            }
        }



OR

int rindex = (((GridViewRow)(((Control)(sender)).Parent.BindingContainer))).RowIndex;


In this sender returns the inner(child) and the Parent property returns the DataControlFieldCell of outer(parent) gridview and BindingContainer returns the GridviewRow of Parent gridview


Hope this solves your problem!! :)
 
Share this answer
 
Comments
Vineetha Ravindranath 24-Jul-14 3:07am    
Thank you..

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900