简体   繁体   English

获取datakey嵌套的gridview

[英]Get datakey multinested gridview

I have 3 nested gridview , below is html code, and code behind of rowdataboun in second gridview. 我有3个嵌套的gridview,下面是html代码,第二个gridview中rowdataboun后面的代码。 In this code rowdatabound of second gridview i want to get datakey of second gridview and get datakey of first gridview , this information is needed for fill third nested gridview What is wrong? 在此代码中,第二个gridview的rowdatabound我想获取第二个gridview的数据键并获取第一个gridview的数据键,该信息对于填充第三个嵌套的gridview是必需的。

<div>
    <table style="width:100%;height:100%;">
        <tr>
            <td>
                <asp:GridView ID="grdOuterGridView" runat="server" AllowPaging="True"
            AutoGenerateColumns="False" DataKeyNames="nitter"
            onrowdatabound="grdOuterGridView_RowDataBound" CellPadding="4"
            ForeColor="#333333" GridLines="None">
            <AlternatingRowStyle BackColor="White" />
            <Columns>
                <asp:TemplateField>
                    <ItemTemplate>
                        <a href="javascript:switchViews('div<%# Eval("nitter") %>');">
                            <img id='imgdiv<%# Eval("nitter") %>' title="Click to show/hide orders" border="0" src="images/plus.png" width="15px" />
                        </a>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:BoundField HeaderText="trazonsocial" DataField="trazonsocial" />
                <asp:BoundField HeaderText="nitter" DataField="nitter"/>
                <asp:TemplateField>
                    <ItemTemplate>
                        </td></tr>
                        <tr>
                          <td colspan="100%">
                            <div id='div<%# Eval("nitter") %>' style="display:none;position:relative;left:25px;" >
                               <asp:GridView ID="grdInnerGridView" runat="server" Width="80%"
                               AutoGenerateColumns="false" DataKeyNames="id1"
                               onrowdatabound="grdInnerGridView_RowDataBound" CellPadding="4"
                               EmptyDataText="No register 2 ..." >
                                  <Columns>
                                     <asp:TemplateField>
                                        <ItemTemplate>
                                           <a href="javascript:switchViews('div<%# Eval("id1") %>');">
                                              <img id='imgdiv<%# Eval("id1") %>' title="Click to show/hide orders" border="0" src="images/plus.png" width="15px" />
                                           </a>
                                        </ItemTemplate>
                                     </asp:TemplateField>
                                     <asp:BoundField HeaderText="tipo1" DataField="tipo1" />
                                     <asp:BoundField HeaderText="tipo" DataField="tipo" />
                                     <asp:BoundField HeaderText="numero" DataField="numero" />
                                     <asp:TemplateField>
                                        <ItemTemplate>
                                           </td></tr>
                                             <tr>
                                               <td colspan="100%">
                                                 <div id='div<%# Eval("id1") %>' style="display:none;position:relative;left:25px;" >
                                                   <asp:GridView ID="grdInnerGridView2" runat="server" Width="80%"
                                                   AutoGenerateColumns="false" DataKeyNames="id1diariop"
                                                   EmptyDataText="No register 3..." >
                                                     <Columns>
                                                       <asp:BoundField HeaderText="tipo1" DataField="tipo1" />
                                                       <asp:BoundField HeaderText="tipo" DataField="tipo" />
                                                       <asp:BoundField HeaderText="Numero" DataField="numero" />
                                                       <asp:BoundField HeaderText="Cuenta" DataField="dcodigo" />
                                                       <asp:BoundField HeaderText="Debito" DataField="dvalordeb" />
                                                       <asp:BoundField HeaderText="Credito" DataField="dvalorcre" />
                                                     </Columns>
                                                   </asp:GridView>
                                                 </div>
                                              </td>
                                            </tr>
                                         </ItemTemplate>
                                     </asp:TemplateField>
                                  </Columns>
                               </asp:GridView>
                            </div>
                          </td>
                        </tr>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
            <EditRowStyle BackColor="#2461BF" />
            <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
            <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
            <RowStyle BackColor="#EFF3FB" />
            <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
            <SortedAscendingCellStyle BackColor="#F5F7FB" />
            <SortedAscendingHeaderStyle BackColor="#6D95E1" />
            <SortedDescendingCellStyle BackColor="#E9EBEF" />
            <SortedDescendingHeaderStyle BackColor="#4870BE" />
        </asp:GridView>
            </td>
        </tr>
    </table>
</div>
protected void grdInnerGridView_RowDataBound(object sender, GridViewRowEventArgs e)
{

    if (e.Row.RowType == DataControlRowType.DataRow)
    {

        // this NOT WORKS  ????????? I need get datakey first gridview
        string cID = (string)grdOuterGridView.DataKeys[e.Row.RowIndex].Values["nitter"];

        //this NOT WORK   ?????????? I need get datakey second gridview (this)
        int iID = (int)grdInnerGridView.DataKeys[e.Row.RowIndex].Values["id1"];


        //I need "cID" and "iID" data for fill last gridview 
        GridView innerGridView2 = (GridView)e.Row.FindControl("grdInnerGridView2");
        FillInnerGrid2(cID, iID, innerGridView2);


    }
}

The way to get datakey of child gridview in RowDataBound is : 在RowDataBound中获取子gridview的datakey的方法是:

protected void grdInnerGridView_RowDataBound(object sender, GridViewRowEventArgs e)
{

    //Accessing GridView from Sender object
    GridView childGrid = (GridView)sender;

    if (e.Row.RowType == DataControlRowType.DataRow)
    {

        //Retreiving the GridView DataKey Value
        string cID = childGrid.DataKeys[e.Row.RowIndex].Value.ToString();


    }
}

The RowDataBound event is fired while data is bind to the gridview. 将数据绑定到gridview时将触发RowDataBound事件。 The DataKeys property is not yet populated. DataKeys属性尚未填充。 If you want to retrieve the data on the currently binded row you can use the DataItem property of the GridViewRowEventArgs 如果要检索当前绑定行上的数据,则可以使用GridViewRowEventArgsDataItem属性。

To access the DataItem of the parent row you will have to go up on the control tree. 要访问父行的DataItem ,您将必须进入控件树。 We can use the NamingContainer property to go up more quickly. 我们可以使用NamingContainer属性更快地上升。

This kind of code should works : 这种代码应该可以工作:

protected void grdInnerGridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        Control outerRow = e.Row.Parent.NamingContainer;
        while(!(outerRow is IDataItemContainer))
        {
            outerRow = outerRow.NamingContainer; 
        }
        string cID = ((OuterDataType)((IDataItemContainer)outerRow)).nitter;
        int iID = ((InnerDataType)e.DataItem).Id;


        GridView innerGridView2 = (GridView)e.Row.FindControl("grdInnerGridView2");
        FillInnerGrid2(cID, iID, innerGridView2);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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