Help: Data Binding to Custom Collection

  • Thread starter Thread starter Shrage Smilowitz
  • Start date Start date
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
 
The error message tells you what it is looking for. You have to make
BookName a property.

L.L.
 
:-) 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!
 
Back
Top