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 16 5277
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
"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/>
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
"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.
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.
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.
"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/>
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/>
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 ///
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 ///
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 ///
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/>
JL,
I never saw Herfried spontanisly use structures in his samples, and he does
that in this case as well not.
Cor
"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/>
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.
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. >> >> >> This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Jason |
last post by:
Here is an odd issue. I am trying to shed some light on why this is causing
a problem.
I have an ArrayList. I am binding it to a ListBox control with has its Sort
property set to True. If the...
|
by: Jim Bancroft |
last post by:
Hi everyone,
I'm binding an ArrayList to a DataGrid for the first time (I'm used to
binding DataSets and DataTables) and I was wondering if I could somehow
"name" the ArrayList, so that I can...
|
by: Alan Silver |
last post by:
Hello,
I'm trying to use an ArrayList to do data binding, but am getting an
error I don't understand. I posted this in another thread, but that was
all confused with various other problems,...
|
by: guy |
last post by:
I have a winforms datagrid bound to an arraylist which works correctly,
however if I add a row to the arraylist i cannot get the datagrid to display
the updated data other than by:-
clearing the...
|
by: lgbjr |
last post by:
hi All,
I've decided to use Options Strict ON in one of my apps and now I'm trying
to fix a late binding issue. I have 5 integer arrays:
dim IA1(500), IA2(500), IA3(500), IA4(500), IA5(500) as...
|
by: Dean Slindee |
last post by:
Can someone explain how to remedy the two late binding errors below when
Option Strict On:
Public Function ControlDataRowArrayListPaint(ByRef frm As Form, _
ByRef ctl
As Control, _
ByRef...
|
by: Dean Slindee |
last post by:
Can someone explain how to remedy the two late binding errors below when
Option Strict On:
Public Function ControlDataRowArrayListPaint(ByRef frm As Form, _
ByRef ctl As Control, _
ByRef dr...
|
by: Miro |
last post by:
VB 2003 and Im still new to vb, so i hope i can explain this as best I can.
I have a variable defined as such: ( simple example )
Dim AVariableOfSorts(,) As Object = _
{ _
{"Last", "String",...
|
by: kevinwolfe |
last post by:
Hi all.
I'd like any suggestions on how I can get my data set (not a DataSet)
bound to a couple of controls on a form. Let me start by describing
what my data looks like. Each entry correlates...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
| |