从嵌套gridview获取Gridview Datakey

时间:2015-11-27 14:41:56

标签: c# asp.net

我有一个嵌套在另一个gridview内的gridview。我需要的是在内部gridview上从外部gridview获取DataKey。 我怎么能通过呢?

感谢。

更新

<asp:GridView ID="gvPeople" runat="server" AutoGenerateColumns="false" CssClass="Grid" DataKeyNames="person_id" OnRowDataBound="gvPeople_RowDataBound">
            <Columns>
                <asp:TemplateField HeaderStyle-Width="10px" ItemStyle-HorizontalAlign="Center">
                    <ItemTemplate>
                        <img alt = "" style="cursor: pointer" src="images/glyphicons/png/glyphicons_236_zoom_in.png" />
                        <asp:Panel ID="pnlOrders" runat="server" Style="display: none">
                            <asp:GridView ID="gvDocuments" runat="server" AutoGenerateColumns="false" CssClass = "ChildGrid" DataKeyNames="document_id" ShowFooter="true" 
                                OnRowCommand="gvDocuments_RowCommand" OnRowDataBound="gvDocuments_RowDataBound" OnRowDeleted="gvDocuments_RowDeleted" OnRowDeleting="gvDocuments_RowDeleting"
                                GridLines="Horizontal" >
                                <Columns>
                                    <asp:BoundField ItemStyle-Width="150px" DataField="document_date" HeaderText="Document Date"  />
                                    <asp:BoundField ItemStyle-Width="100px" DataField="value" HeaderText="Value" DataFormatString="{0:#,##.00 €}" />
                                    <asp:TemplateField HeaderText="">
                                        <ItemTemplate>
                                            <asp:Button runat="server" ID="delbutton" CommandArgument='<%# Eval("document_id") %>' CommandName="Delete" Text="Delete" />
                                            &nbsp;
                                            <asp:Button runat="server" ID="editbutton" CommandArgument='<%# Eval("document_id") %>' Text="Edit" UseSubmitBehavior="false" />
                                        </ItemTemplate>
                                        <FooterTemplate>
                                            <asp:Button runat="server" ID="newdoc" CommandName="New" Text="New Document" UseSubmitBehavior="false" />
                                        </FooterTemplate>
                                    </asp:TemplateField>
                                </Columns>
                            </asp:GridView>
                        </asp:Panel>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:BoundField ItemStyle-Width="150px" DataField="name" HeaderText="Name" />
                <asp:BoundField ItemStyle-Width="150px" DataField="ndocs" HeaderText="Nº of Documents" />
                <asp:BoundField ItemStyle-Width="150px" DataField="total_cost" HeaderText="Total Cost" DataFormatString="{0:#,#0.00 €}" />
            </Columns>
        </asp:GridView>

这是我的aspx代码,它完全正常,新文档按钮的激活,运行一个javascript按钮,作为编辑按钮,但在这一个我需要传递person_id,从行的gvPeople,所以当我创建一个新的gvDocuments行时,它将使它成为那个人的身份。

感谢。

2 个答案:

答案 0 :(得分:0)

把它投入使用。

在子gridview之前添加一个隐藏字段,其中包含我想要的值。 在子gridview的RowDataBound上,添加了:

string str= ((HiddenField)e.Row.Parent.Parent.Parent.FindControl("hf")).Value.ToString();

现在将str值用于我想要的任何内容。

答案 1 :(得分:0)

您需要先访问每个父gridview行,然后访问子gridview行,然后访问datakey。

示例:

foreach (GridViewRow rowPeople in gvPeople.Rows)
{
    GridView gvDocuments = (GridView)rowPeople.FindControl("gvDocuments");
    //this will get you the outer/parent gridview datakeys
    gvDocuments.DataKeys[rowPeople.RowIndex].Values[0].ToString();
    foreach (GridViewRow rowDocuments in gvDocuments.Rows)
    {
        //this will get you the inner/child gridview datakeys
        gvDocuments.DataKeys[rowDocuments.RowIndex].Values[0].ToString();
    }
}

请告诉我这是否适合您!