2012-01-26 111 views
0

我想在另一個嵌套一個gridview,但我無法獲得數據填充在第二個網格視圖。嘗試設置第二個數據網格的數據源時(說它爲空),出現錯誤。誰能幫忙? 這裏是aspx頁面:嵌套的GridView問題

<div id="divSource" runat="server" align="center"> 
<asp:GridView ID="Source" runat="server" AutoGenerateColumns="False" DataKeyNames="sourceLineItem" CSSClass="viewSourceGrid" OnRowDataBound="PopulateDateCodes"> 
    <Columns> 
     <asp:TemplateField InsertVisible="False" HeaderStyle-Width="70px"> 
      <ItemTemplate> 
       <asp:Label CssClass="sourceHeader" runat="server" Text= '<%# "Source: " + (Container.DataItemIndex + 1).ToString() %>'> </asp:Label> 
      </ItemTemplate> 
     </asp:TemplateField> 
     <asp:BoundField DataField="nfdBroker" HeaderText="NFD/Broker" InsertVisible="False" ReadOnly="True" SortExpression="nfdBroker" /> 
     <asp:BoundField DataField="locationDescription" HeaderText="Material Location" SortExpression="materialLocation" /> 
     <asp:BoundField DataField="origPkg" HeaderText="Original Packaging?" SortExpression="origPkg" /> 
     <asp:BoundField DataField="oemCC" HeaderText="OEM C of C? " InsertVisible="False" ReadOnly="True" SortExpression="oemCC" /> 
     <asp:BoundField DataField="minBuyQty" HeaderText="Minimum Buy Qty" SortExpression="minBuyQty" /> 
     <asp:BoundField DataField="deliveryInfo" HeaderText="Delivery" SortExpression="delUOM" /> 

     <asp:TemplateField InsertVisible="False" HeaderText="Date Codes" > 
      <ItemTemplate>    
       <asp:GridView ID="DateCodeGrid" runat="server" InsertVisible="False" DataKeyNames="dateCode" CSSClass="viewSourceGrid" > 
        <Columns> 
         <asp:BoundField DataField="dateCode" SortExpression="dateCode"> 
          <ItemStyle Width="20%" /> 
         </asp:BoundField> 
        </Columns> 
       </asp:GridView> 
      </ItemTemplate> 
     </asp:TemplateField> 

    </Columns> 
</asp:GridView> 

,然後這裏是後面的代碼:

public partial class Controls_ViewSource : System.Web.UI.UserControl 
{ 
    //Set the Source Line Item 
    public int SourceLineItem { get; set; } 

    protected void Page_Load(object sender, EventArgs e) 
    { 
     this.SourceLineItem = SourceLineItem; 
     RequestDB db = new RequestDB(); 
     DataSet sources = db.GetSource(int.Parse(Request.QueryString["requestNumber"]), SourceLineItem); 
     Source.DataSource = sources; 
     Source.DataBind(); 

    } 

    protected void PopulateDateCodes(object sender, GridViewRowEventArgs e) 
    { 
     RequestDB db = new RequestDB(); 
     int index = e.Row.RowIndex; 
     GridView gv = (GridView)Source.Rows[0].FindControl("DateCodeGrid"); 
     //int sourceLineItem = int.Parse(Source.DataKeyNames[0].ToString()); 
     //Response.Write(Source.DataKeyNames[0].ToString()); 
     DataSet dateCodes = db.GetDateCodes(71); 
     gv.DataSource = dateCodes; 
     gv.DataBind(); 

    } 
} 
+0

您可以向我們展示'db.GetDateCodes'的代碼嗎?我懷疑你沒有鏈接你的嵌套數據 – 2012-01-26 23:42:30

+0

此外,你的GV人口代碼很奇怪。它看起來像你沒有填充gridview,但只有第一行內的第一個嵌套的gridview?外面的gridview需要先被數據綁定。 – 2012-01-26 23:44:33

回答

3

你會需要找到正在數據綁定行中的嵌套網格視圖:

GridViewRow row = e.Row; 

您需要確保您只對數據行執行此操作,而不是頁眉或頁腳行:

if(row.RowType == DataControlRowType.DataRow) 
{ 
    // Find the nested grid view 
    GridView nested = (GridView)row.FindControl("DateCodeGrid"); 

    // The rest of your code for binding the nested grid view follows here 
} 
+0

太棒了,工作。謝謝! – shawleigh17 2012-01-27 00:42:35