简体   繁体   中英

Row index from hierarchical gridview

I have my gridview structure like this -

  <asp:GridView ID="gvTest" runat="server" AutoGenerateColumns="false" 
        Width="100%">
        <Columns>
            <asp:TemplateField HeaderText="Rule Name" ItemStyle-VerticalAlign="Top">
                <ItemTemplate>
                    <asp:Label ID="lblRuleName" runat="server" Text='<%# Bind("RuleName") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>


            <asp:TemplateField HeaderText="Source" ItemStyle-VerticalAlign="Top">
                <ItemTemplate>
                    <asp:GridView ID="gvSource" runat="server" ShowHeader="false" AutoGenerateColumns="false">
                     <Columns>
                            <asp:TemplateField>
                                <ItemTemplate>
                                 <asp:ImageButton ID="imgExpandSource" runat="server" ImageUrl="~/plus.png" onclick="imgExpandSource_Click"/>
                                  <asp:ImageButton ID="imgExpandSource1" runat="server" ImageUrl="~/Minus.png" onclick="imgExpandSource1_Click" Visible="false"/>
                                    <asp:Label ID="lblSourceObjName" runat="server" Text='<%# Bind("SourceObjName") %>'></asp:Label>                                       

                                    <asp:GridView ID="gvSourceObj" runat="server" ShowHeader="false" Visible="false">
                                    </asp:GridView>
                                </ItemTemplate>
                            </asp:TemplateField>
                        </Columns>
                    </asp:GridView>
                </ItemTemplate>
            </asp:TemplateField>


            <asp:TemplateField HeaderText="Destination" ItemStyle-VerticalAlign="Top">
                <ItemTemplate>
                    <asp:GridView ID="gvDestination" runat="server" ShowHeader="false" AutoGenerateColumns="false">
                        <Columns>
                            <asp:TemplateField>
                                <ItemTemplate>
                                 <asp:ImageButton ID="imgExpand" runat="server" ImageUrl="~/plus.png" onclick="imgExpand_Click"/>
                                 <asp:ImageButton ID="imgExpand1" runat="server" ImageUrl="~/Minus.png" onclick="imgExpand1_Click" Visible="false"/>
                                    <asp:Label ID="lblDestObjName" runat="server" Text='<%# Bind("DestObjName") %>'></asp:Label>                                      

                                    <asp:GridView ID="gvDestObj" runat="server" ShowHeader="false" Visible="false">
                                    </asp:GridView>
                                </ItemTemplate>
                            </asp:TemplateField>
                        </Columns>
                    </asp:GridView>
                </ItemTemplate>
            </asp:TemplateField>


            <asp:TemplateField HeaderText="Service Group" ItemStyle-VerticalAlign="Top">
                <ItemTemplate>
                    <asp:GridView ID="gvServiceObject" runat="server" ShowHeader="false">
                    </asp:GridView>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>  

Now I want to get the Row index of gvTest gridview on imgExpandSource link button's click event. So I tried with this code -

 protected void imgExpandSource_Click(object sender, ImageClickEventArgs e)
    {

        ImageButton imgExpandSource = sender as ImageButton;
        GridViewRow gvrow = (GridViewRow)imgExpandSource.NamingContainer;
        int x = gvrow.RowIndex;
    }

But this is giving me gvSource gridviews row index. How can I get gvTest gridviews row index. Please some one help me.

Thanks Gulrej

You need to add some more levels to get the right naming contrainer:

protected void imgExpandSource_Click(object sender, ImageClickEventArgs e)
{

    ImageButton imgExpandSource = sender as ImageButton;
    GridViewRow gvrow = (GridViewRow)imgExpandSource
                             // GridViewRowRow of gvSource
                             .NamingContainer
                             // gvSource
                             .NamingContainer
                             // GridViewRow of gvTest
                             .NamingContainer;
    int x = gvrow.RowIndex;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM