PC Review


Reply
Thread Tools Rate Thread

Binding to property of property of object collection

 
 
TS
Guest
Posts: n/a
 
      30th Aug 2006
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


 
Reply With Quote
 
 
 
 
KJ
Guest
Posts: n/a
 
      30th Aug 2006
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


 
Reply With Quote
 
Walter Wang [MSFT]
Guest
Posts: n/a
 
      31st Aug 2006
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 ((E-Mail Removed), 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.

 
Reply With Quote
 
TS
Guest
Posts: n/a
 
      31st Aug 2006
thanks KJ!!!
I thought i had seen it done but have had issues with it before.

"KJ" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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

>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Binding TextBox.Text to property in nested (multi levels) object withbindingsource, Error if grandchild object is NULL Efy Microsoft Dot NET 0 14th Oct 2009 11:06 AM
Change caption of property while binding a business collection to a datagrid Rajat Microsoft Dot NET Framework Forms 0 16th Feb 2006 07:56 PM
Property of object to binding Boniek Microsoft C# .NET 2 6th Apr 2004 05:01 PM
Property of object to Binding Boniek Microsoft C# .NET 1 30th Mar 2004 10:00 AM
binding to a property within ad AD object for setting permissions? Bob Weiner Microsoft Windows 2000 Developer 3 4th Jul 2003 08:46 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 06:55 AM.