.NET newbie. BindingList/BindingSource issues

szurlo

New member
Joined
Jan 30, 2008
Messages
2
Programming Experience
5-10
Let me preface this by saying that I have several years of VB experience prior to .NET, which I'm rapidly learning doesn't mean squat in the .NET world. :(
One of the really cool things I see in .NET is the ability to bind a data aware control to just about anything. However, I can't make it work right to save my life.
I don't get any errors, but the control is being populated with the name of the structure I created the BindingList on instead of the contents of the Binding list.
The test code below demonstrates the issue. When the below code runs, listbox1 populates with "one thing" and "another thing" as it should. Listbox2 however populates with "test.Form1+simplestruct" and "test.Form1+simplestruct".
I know the BindingList contains the data because I can access it direclty using "?ctype(binder.Current,simplestruct).someting" from the immidiate window while paused and it returns "one thing".
What the heck am I doing wrong??



Steve Z



Public Structure simplestruct
Dim someting As String
End Structure

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim mylist As New System.ComponentModel.BindingList(Of simplestruct)
Dim tmpstruct As New simplestruct

tmpstruct.someting = "one thing"
mylist.Add(tmpstruct)
tmpstruct.someting = "another thing"
mylist.Add(tmpstruct)

For Each tmpstruct In mylist
ListBox1.Items.Add(tmpstruct.someting)
Next

Dim binder As New BindingSource
binder.DataSource = mylist
ListBox2.DataSource = binder
ListBox2.DisplayMember = "something"
End Sub
 
DisplayMember must be a Property, not a Field. So in your structure write "property" and press Enter, fill in the 'blanks' (property name and type).
(addendum: when DisplayMember isn't valid a default ToString is used to display the item, since your structure also doesn't have a ToString method the inherited Object.ToString method is used which only disply the object type name)
 
DisplayMember must be a Property, not a Field. So in your structure write "property" and press Enter, fill in the 'blanks' (property name and type).
(addendum: when DisplayMember isn't valid a default ToString is used to display the item, since your structure also doesn't have a ToString method the inherited Object.ToString method is used which only disply the object type name)

Thanks so much! I beat my brains out yesterday trying to get this to work. I was stuck in the databound control mindset where I assumed I was selecting a table field to be displayed by the control. Additionally, I didn't even know you could have properties in structures (or what I'm familiar with as UDTs). :eek:
What I wound up doing, since it made more sense to me after I read your post, was I replaced the structure with a class and that worked perfectly.
Thanks much for your help!

Steve Z.
 
Back
Top