Help: Data Binding to Custom Collection

S

Shrage Smilowitz

I try to create a custom collection which i will then use to databind and
its not working.

Class Book
Public BookName As String
Sub New(ByVal lBookName As String)
Me.BookName = BookName
End Sub
End Class

Public Class books
Inherits Collections.CollectionBase
Default Property Item(ByVal index As Integer) As Book
Get
Return CType(innerlist.Item(index), Book)
End Get
Set(ByVal Value As Book)
innerlist.Item(index) = Value
End Set
End Property

Sub add(ByVal value As Book)
innerlist.Add(value)
End Sub
End Class


I then create a form, add a dropdownlist
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim objBooks As books = New books()
objBooks.add(New Book("Book #1"))
objBooks.add(New Book("Book #2"))
objBooks.add(New Book("Book #3"))
objBooks.add(New Book("Book #4"))
objBooks.add(New Book("Book #5"))
DropDownList1.DataSource = objBooks
DropDownList1.DataTextField = "BookName"
DropDownList1.DataBind()
End Sub

And i get this error

DataBinder.Eval: 'Website.Products+Book' does not contain a property with
the name BookName.
When i access the object manually it works
textbox1.text = objBooks.Item(0).BookName: Does work
textbox1.text = objBooks(0).BookName: Does work
 
L

L. L.

The error message tells you what it is looking for. You have to make
BookName a property.

L.L.
 
S

Shrage Smilowitz

:) Thanks a million! I have ben working on it a whole day, i didn't realize
that, i tought a public variable is the same think as a property. Thanks
Again!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top