Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > Databind a collection and get parent property
 

Databind a collection and get parent property

Hi at all,

I've a GridView bound to my generics Collection Students (a name for example).

Student is a custom class and it has the "Parent" property which is my custom class ClassRoom.

I bind che students collection to a gridview and it works correctly if I display fields from students object.

But When I try to set in DataField property of the BoundColumn : Parent.ClassNumber asp.net cannot find the property. In debug mode I see that collection are correctly created and every student has a Parent property which is a ClassRoom object with its properties.

What have I forgot?
i don't want to replicate the classroom properties in the student object... is it possibile to mantain a hierarchical structure in binding right?

Do i miss some interface in my custom classes?

I'm using asp.net 2.0, c# and a Gridview with simply bound columns.

Thanks in advance for the help :)

Bob

BobSpeaking  Saturday, August 12, 2006 10:57 AM

Hi Bob,

I know this is a bit late, but i stumbled across your post googling on the same problem.

I found a soultion, andI'm posting it here so it may help others with the same problem.

Solution:

For nested Repeaters:

<%# DataBinder.Eval(((System.Web.UI.WebControls.RepeaterItem)Container.Parent.Pðarent).DataItem, "FieldName") %>

For nested Repeater/Gridview

<%# DataBinder.Eval(((RepeaterItem)((GridViewRow)Container).Parent.Parent.Parent).DataItem, "FieldName")%>

For Repeater/Repeater/Gridview

<%# DataBinder.Eval(((RepeaterItem)((GridViewRow)Container).Parent.Parent.Parent.Parent.Parent).DataItem, "FieldName")%>

And so on.

Mattias P

MattiasP  Wednesday, June 11, 2008 2:21 PM

You can use google to search for other answers

Custom Search

More Threads

• set a Datagridview datasource to datareader
• Winforms Tabcontrol issue
• refreshing datagrid
• Use of table in Windows Form
• how to get difference between two dates in number of day,month,year format
• Newbie: Displaying names in a Combobox rather than their corresponding Id values
• Master Detail binding problem
• TableAdapter.Update doesn't update Database
• Where are the icons kept that show up in the ToolKit
• I want to delete the data from datagridview by using check box which should also affect in data base.