Binding to Arraylist

M

Michael C#

I'm binding a Combobox to an Arraylist, and I'd like to set the ValueMember
and DisplayMember properties of the Combobox. Is this possible, or do I
need to bind to a DataTable?

Thanks
 
K

Ken Tucker [MVP]

Hi,

Yes you can bind to any property contained in a class stored in the
arraylist.

Ken
----------------------
I'm binding a Combobox to an Arraylist, and I'd like to set the ValueMember
and DisplayMember properties of the Combobox. Is this possible, or do I
need to bind to a DataTable?

Thanks
 
H

Herfried K. Wagner [MVP]

Michael C# said:
I'm binding a Combobox to an Arraylist, and I'd like to set the
ValueMember and DisplayMember properties of the Combobox. Is this
possible

Yes.
 
G

Guest

I believe that you have to set the DataSource name to "ArrayList" whereas for
a Datatable, you set this to the DataTable's name. At least this is what you
have to for binding the DataGrid to an ArrayList so I would suppose it's the
same for a combo box. The DataMember has no meaning when binding to an
arraylist.
 
M

Michael C#

So you can't set the Value and a separate distince DisplayMember? For
instance:

1, Yes
2, No
3, Maybe

If I display and choose "No" in the Combobox, there is no way to retrieve
the value 2 which is related to that option? It seems like that's a bit of
important functionality to be missing.
 
H

Herfried K. Wagner [MVP]

Michael C# said:
Well as long as it's possible.

???

Sample:

\\\
Private Sub Form1_Load( _
ByVal sender As Object, _
ByVal e As EventArgs _
) Handles MyBase.Load
Dim al As New ArrayList
For i As Integer = 1 To 20
Dim p As New Person
p.Name = "Name " & CStr(i)
p.Age = 70 - i
al.Add(p)
Next i
With Me.ListBox1
.DisplayMember = "Name"
.ValueMember = "Age"
.DataSource = al
End With
End Sub

Private Sub ListBox1_SelectedIndexChanged( _
ByVal sender As Object, _
ByVal e As EventArgs _
) Handles ListBox1.SelectedIndexChanged
MsgBox("Selected value: " & CStr(Me.ListBox1.SelectedValue))
End Sub
..
..
..
Public Class Person
Private m_Name As String
Private m_Age As Integer

Public Property Name() As String
Get
Return m_Name
End Get
Set(ByVal Value As String)
m_Name = Value
End Set
End Property

Public Property Age() As Integer
Get
Return m_Age
End Get
Set(ByVal Value As Integer)
m_Age = Value
End Set
End Property

Public Overrides Function ToString() As String
Return Me.Name & " (" & Me.Age.ToString() & ")"
End Function
End Class
///
 
J

J L

Why wont this work with a structure and combo box

Public Structure myStructure
Public Name As String
Public Title As String
End Structure

Dim arList As New ArrayList
Dim i As Integer

For i = 0 To 10
Dim ms As New myStructure
ms.Name = "Name " & i.ToString
ms.Title = "Title " & i.ToString
arList.Add(ms)
Next
'
With cmboMyTest
.DisplayMember = "Name"
.ValueMember = "Title"
.DataSource = arList
End With

When I run this, the combo box is filled with the fully qualified name
of the form and "+myStructure"

That is: "ObjectIntro.FormSecondTest+myStructure" is displayed in the
10 items of the combo box.

Thanks for all the help, always.

John
 
J

J L

Follow on question/observation:

When I create a class as you suggest, I can get the binding for the
listbox to work but not for the combobox. Neither work if the
arraylist contains a structure. So I am confused on the issue of
using a class vs structure as a datasource and why the combobox does
not work with either.

Secondly, when I display the results in the SelectedIndexChanged
event, it works as you have posted for the selectedvalue but there is
no simple way to retrieve the string that is displayed. SelectedItem
returns the reference to the person class. I think that makes sense,
but just want to verify.

From all of this, I am sensing that when you fill a listbox (and
combobox?) directly with strings (that is no binding), the display and
value members are the same...that is, the string.

Thanks again for all the help and clarification,
John
 
J

J L

OK...now I see what is happening...you need to create properties in
the structure. When I did that, both the class and the structure work
with both the combo box and the list box.

So what are the merits of using a structure vs a class in this case?

John
 
J

J L

Well, while I am talking to myself LOL...

Very interesting...if I put both a listbox and combobox on the form
and bind them to the same arraylist, they become synchronized! Not
sure how I would use that...but it is interesting.

John

Thank you for expounding on your initial 4-character response. I appreciate
it.
 
C

Cor Ligthert

JL,

I never saw Herfried spontanisly use structures in his samples, and he does
that in this case as well not.

Cor
 
H

Herfried K. Wagner [MVP]

J L said:
OK...now I see what is happening...you need to create properties in
the structure. When I did that, both the class and the structure work
with both the combo box and the list box.

So what are the merits of using a structure vs a class in this case?

The decision whether or not to use a structure should not be based on
setting a data source. You will find some information on when to choose
structures here:

<URL:http://groups.google.de/groups?threadm=8z%[email protected]>
 
G

Guest

I mistyped, I meant to say that you can't set the DataMember, not Display
Member. You should be able to set the Display member OK.
 
M

Michael C#

OK, that does make a little more sense taken with Herfried K's code. Thanks
for the clarification.
 

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

Top