Binding a Gridview to a child object using an ObjectDataSource
I tend to do all data access using an ObjectDataSource (ODS) with custom
business objects, which generally all works fine. But for some reason I
have not until now had the need to have a business object containing a
list of child objects. The following is a simplified example:
Public Class Matches
Public Property MatchCount() As Integer
Public Property Artists() as List(Of ArtistInfo)
Public Sub New(count As Integer, artists As List(Of ArtistInfo))
Me.MatchCount = count
Me.Artists = artists
End Sub
Public Sub New()
End Sub
End Class
Public Class ArtistInfo
Public Property ID() As Integer
Public Property Name() As String
Public Sub New(id As Integer, name As String)
Me.ID = id
Me.Name = name
End Sub
Public Sub New()
End Sub
End Class
I can return a Matches object from my data access layer (DAL). I can also
programmatically call my DAL function and bind a GridView to the Artists
collection. However, I then lose automated paging. But if I use an ODS to
retrieve the Matches object, then I cannot see how to reference the
properties within the ArtistInfo child object. I have seen a post which
suggests that I can refer (e.g. in an Eval) to Artists.ID and
Artists.Name, but I get a data binding runtime error that object
ArtistInfo does not contain such a property. It does not seem to make any
difference whether I specify DataObjectTypeName (which would have to be
Matches as I will need to access other properties of this object) on the
ODS or DataKeyNames on the GridView.
Hope this makes sense.
No comments:
Post a Comment