2015-11-27 45 views
0

我有一个嵌套在另一个gridview的gridview。我需要的是在内部gridview从外部gridview获取DataKey。 我怎么能通过它?从嵌套的gridview获取Gridview的数据键

谢谢。

UPDATE

<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按钮,编辑按钮,但在这一个我需要从gvPeople的行中传递person_id,所以当我创建一个新的gvDocuments行时,它会将其转换为该人的id。

谢谢。

+0

相关代码在哪里? –

回答

0

只是把它的工作。

在子gridview之前添加我想要的值的隐藏字段。 在孩子的GridView的的RowDataBound,添加了这个:

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

而现在使用STR值什么,我想要的。

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(); 
    } 
} 

让我知道,如果你是这样的!