Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Nested collection in DataSource

Status
Not open for further replies.

dragonwell

Programmer
Oct 21, 2002
863
US
My DataList has an interesting DataSource - it's an ArrayList of objects. Each of the objects in my DataSource ArrayList has some string properties and a property that is a collection (another ArrayList).

On my page, I have my DataList bound to the ArrayList. No problem. Within the ItemTemplate, I have a DataGrid which I want to bind to the collection that is part of the current object.

Here's what the Datalist looks like:
Code:
<asp:DataList runat=&quot;server&quot; id=&quot;DataList1&quot;>
   <ItemTemplate>
      <%# DataBinder.Eval(Container.DataItem, &quot;MyString&quot;)%>
        
      <asp:DataGrid runat=&quot;server&quot; id=&quot;DataGrid1&quot;
         datasource=&quot;???????????????????????&quot;>
      </asp:DataGrid>
   </ItemTemplate
</asp:DataList>

I thought of replacing the ????? with a call to a helper method that returns a collection. Something like:

Code:
datasource=&quot;<%# GetCollection() %>&quot;

but I would need to pass in the object I am currently binding to in the master DataList, right? How would I reference it? I've been trying:
Code:
datasource = &quot;<%# GetCollection(Container.DataItem) %>&quot;
but that doesn't work.

Anyway, the GetCollection() method if have so far is this:

Code:
public ArrayList GetCollection(DataListItem item)
		{
			DataList list = (DataList)item.Parent;
			MyObject detail = (MyObject)list.DataSource;
			
			return detail.MyCollection;
		}

Anyone have any idea on how I could accomplish this? Thanks!
 
Two things:

First, use the OnItemDataBound event of the datalist to bind that nested list

Second, you need an indexer on your object if it doesn't already expose one via the underlying arraylist.

Once you have those two, then I'll assume that your property which is the inner collection is called .InnerCollection, and then your OnItemDataBound might look like this:
Code:
protected void HandleItemDataBound(object o, DataListItemEventArgs e){
  switch(e.Item.ItemType)
  {
    //you're only interested in your main items
    case ListItemType.Item:
    case ListItemType.AlternatingItem:
      //get a handle on your inner datalist
      DataList inner = ((DataList)e.Item.FindControl(&quot;IdOfYourInnerList&quot;));
      //grab the InnerCollection from your main datasource
      inner.DataSource = YourOverallObject[e.Item.ItemIndex].InnerCollection;
      //bind the list
      inner.DataBind();
  }
}
I've made a few assumptions here, but hopefully, you get the idea.

Hope it gets you going :)
paul

penny.gif
penny.gif

The answer to getting answered -- faq855-2992
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top