469,290 Members | 1,887 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,290 developers. It's quick & easy.

Binding to Arraylist

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
Nov 21 '05 #1
16 5058
Hi,

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

Ken
----------------------
"Michael C#" <xy*@abcdef.com> wrote in message
news:Xt*****************@fe08.lga...
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

Nov 21 '05 #2
"Michael C#" <xy*@abcdef.com> schrieb:
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.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #3
Thank you

"Ken Tucker [MVP]" <vb***@bellsouth.net> wrote in message
news:%2***************@TK2MSFTNGP15.phx.gbl...
Hi,

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

Ken
----------------------
"Michael C#" <xy*@abcdef.com> wrote in message
news:Xt*****************@fe08.lga...
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

Nov 21 '05 #4

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:eq**************@TK2MSFTNGP15.phx.gbl...
"Michael C#" <xy*@abcdef.com> schrieb:
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.


Well as long as it's possible.
Nov 21 '05 #5
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.

"Michael C#" wrote:

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:eq**************@TK2MSFTNGP15.phx.gbl...
"Michael C#" <xy*@abcdef.com> schrieb:
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.


Well as long as it's possible.

Nov 21 '05 #6
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.

"Dennis" <De****@discussions.microsoft.com> wrote in message
news:BC**********************************@microsof t.com...
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.

"Michael C#" wrote:

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:eq**************@TK2MSFTNGP15.phx.gbl...
> "Michael C#" <xy*@abcdef.com> schrieb:
>> 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.
>


Well as long as it's possible.

Nov 21 '05 #7
"Michael C#" <xy*@abcdef.com> schrieb:
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.


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
///

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>
Nov 21 '05 #8
Thank you for expounding on your initial 4-character response. I appreciate
it.

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:u3**************@TK2MSFTNGP10.phx.gbl...
"Michael C#" <xy*@abcdef.com> schrieb:
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.


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
///

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #9
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

On Mon, 21 Mar 2005 00:45:28 +0100, "Herfried K. Wagner [MVP]"
<hi***************@gmx.at> wrote:
"Michael C#" <xy*@abcdef.com> schrieb:
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.


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
///


Nov 21 '05 #10
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

On Mon, 21 Mar 2005 00:45:28 +0100, "Herfried K. Wagner [MVP]"
<hi***************@gmx.at> wrote:
"Michael C#" <xy*@abcdef.com> schrieb:
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.


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
///


Nov 21 '05 #11
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

On Mon, 21 Mar 2005 00:45:28 +0100, "Herfried K. Wagner [MVP]"
<hi***************@gmx.at> wrote:
"Michael C#" <xy*@abcdef.com> schrieb:
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.


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
///


Nov 21 '05 #12
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

On Sun, 20 Mar 2005 19:05:17 -0500, "Michael C#" <xy*@abcdef.com>
wrote:
Thank you for expounding on your initial 4-character response. I appreciate
it.

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:u3**************@TK2MSFTNGP10.phx.gbl...
"Michael C#" <xy*@abcdef.com> schrieb:
> 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.
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
///

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>


Nov 21 '05 #13
JL,

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

Cor
Nov 21 '05 #14
"J L" <jo**@marymonte.com> schrieb:
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%25Kb.2347%24d27.1979%40news-binary.blueyonder.co.uk>

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #15
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.

"Michael C#" wrote:
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.

"Dennis" <De****@discussions.microsoft.com> wrote in message
news:BC**********************************@microsof t.com...
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.

"Michael C#" wrote:

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:eq**************@TK2MSFTNGP15.phx.gbl...
> "Michael C#" <xy*@abcdef.com> schrieb:
>> 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.
>

Well as long as it's possible.


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

"Dennis" <De****@discussions.microsoft.com> wrote in message
news:BA**********************************@microsof t.com...
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.

"Michael C#" wrote:
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.

"Dennis" <De****@discussions.microsoft.com> wrote in message
news:BC**********************************@microsof t.com...
>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.
>
> "Michael C#" wrote:
>
>>
>> "Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
>> news:eq**************@TK2MSFTNGP15.phx.gbl...
>> > "Michael C#" <xy*@abcdef.com> schrieb:
>> >> 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.
>> >
>>
>> Well as long as it's possible.
>>
>>
>>


Nov 21 '05 #17

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by guy | last post: by
30 posts views Thread by lgbjr | last post: by
1 post views Thread by Dean Slindee | last post: by
2 posts views Thread by Dean Slindee | last post: by
9 posts views Thread by Miro | last post: by
3 posts views Thread by kevinwolfe | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.