469,927 Members | 1,798 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,927 developers. It's quick & easy.

Binding to property of property of object collection

TS
when binding to a datagrid for example, is it possible to use databinding
syntax to bind to a property of a property? Say the datasource="Cars" which
is a collection of car classes and the DataValueField = "Driver.Name" which
is the Car.Driver property and then the Driver.Name property.

thanks
Aug 30 '06 #1
3 1959
KJ
Yes, this is possible. In fact, in .NET 2.0, this is one of the main
purposes of the ObjectDataSource control. When binding an object in a
DataGrid (.NET 1.x) using databinding syntax, you can do as follows (in
the example, the datasource is an ArrayList of Chapter objects, of
which WorkItem is a property of each Chapter):

<asp:TemplateColumn HeaderText="Last Change Date">
<ItemTemplate>
<asp:Label id="lblStatusDate" runat="server">
<%# DataBinder.Eval(Container.DataItem,
"WorkItem.Status.LastChangeDate", "{0:d}") %>
</asp:Label>
</ItemTemplate>
</asp:TemplateColumn>
TS wrote:
when binding to a datagrid for example, is it possible to use databinding
syntax to bind to a property of a property? Say the datasource="Cars" which
is a collection of car classes and the DataValueField = "Driver.Name" which
is the Car.Driver property and then the Driver.Name property.

thanks
Aug 30 '06 #2
Hi TS,

Based on the interaction between the data source and the control, you can
identify three distinct forms of data binding and, subsequently, data-bound
controls--simple binding, list controls, and complex binding.

"DataValueField" belongs to the list controls. Internally it uses
DataBinder.GetPropertyValue to get bound data. And currently this function
doesn't support property path, it can only retrieve the property of the
data source.

As KJ pointed out, DataBinder.Eval can evaluate using the property path,
it's used by complex binding which is also demonstrated by KJ's code.

Hope this helps. Please feel free to post here if anything is unclear.
Sincerely,
Walter Wang (wa****@online.microsoft.com, remove 'online.')
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.

Aug 31 '06 #3
TS
thanks KJ!!!
I thought i had seen it done but have had issues with it before.

"KJ" <n_**********@mail.comwrote in message
news:11**********************@p79g2000cwp.googlegr oups.com...
Yes, this is possible. In fact, in .NET 2.0, this is one of the main
purposes of the ObjectDataSource control. When binding an object in a
DataGrid (.NET 1.x) using databinding syntax, you can do as follows (in
the example, the datasource is an ArrayList of Chapter objects, of
which WorkItem is a property of each Chapter):

<asp:TemplateColumn HeaderText="Last Change Date">
<ItemTemplate>
<asp:Label id="lblStatusDate" runat="server">
<%# DataBinder.Eval(Container.DataItem,
"WorkItem.Status.LastChangeDate", "{0:d}") %>
</asp:Label>
</ItemTemplate>
</asp:TemplateColumn>
TS wrote:
>when binding to a datagrid for example, is it possible to use databinding
syntax to bind to a property of a property? Say the datasource="Cars"
which
is a collection of car classes and the DataValueField = "Driver.Name"
which
is the Car.Driver property and then the Driver.Name property.

thanks

Aug 31 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Dot net work | last post: by
1 post views Thread by Demetri | last post: by
reply views Thread by Stephajn Craig | last post: by
1 post views Thread by Ruben | last post: by
3 posts views Thread by cannontrodder | last post: by
9 posts views Thread by Miro | last post: by
1 post views Thread by Suresh | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.