0

I am trying to nest one gridview within another, but I cannot get the data to populate in the second grid view. I am getting an error when trying to set the data source of the second data grid (saying it is null). Can anyone help? Here is the aspx page:

<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>

and then here is the code behind:

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

    }
}
2
  • Can you show us also the code for db.GetDateCodes ? I suspect that you are not linking in your nested data Commented Jan 26, 2012 at 23:42
  • Also, your GV population code is strange. It looks like you are not populating the gridview, but only the first nested gridview inside the first row? The outside gridview needs to be databound first. Commented Jan 26, 2012 at 23:44

1 Answer 1

3

You would need to find the nested grid view in the row that is being data bound:

GridViewRow row = e.Row;

You need to make sure you are only doing this for data rows, not header or footer rows:

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
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.