473,685 Members | 2,622 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 5299
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***@bellsout h.net> wrote in message
news:%2******** *******@TK2MSFT NGP15.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******** ******@TK2MSFTN GP15.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******** ******@TK2MSFTN GP15.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****@discuss ions.microsoft. com> wrote in message
news:BC******** *************** ***********@mic rosoft.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******** ******@TK2MSFTN GP15.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_Select edIndexChanged( _
ByVal sender As Object, _
ByVal e As EventArgs _
) Handles ListBox1.Select edIndexChanged
MsgBox("Selecte d value: " & CStr(Me.ListBox 1.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******** ******@TK2MSFTN GP10.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_Select edIndexChanged( _
ByVal sender As Object, _
ByVal e As EventArgs _
) Handles ListBox1.Select edIndexChanged
MsgBox("Selecte d value: " & CStr(Me.ListBox 1.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 "+myStructu re"

That is: "ObjectIntro.Fo rmSecondTest+my Structure" 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_Select edIndexChanged( _
ByVal sender As Object, _
ByVal e As EventArgs _
) Handles ListBox1.Select edIndexChanged
MsgBox("Selecte d value: " & CStr(Me.ListBox 1.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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
5900
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 ArrayList only has one element in it everything works ok. But as soon as I have more than one element, I get the following exception when the control loads up: "Cannot modify the Items collection when the DataSource property is set.". Anybody...
3
1579
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 refer to it as a DataField in an asp:BoundColumn? In essence, I'm doing this: ArrayList Arr1 = new ArrayList();
4
1975
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, which could be why no-one answered it!! I've now narrowed the problem down, so am starting a new, more specific, thread. Sorry it's a bit long, but it's reasonably simple stuff. I have a page where I'm showing product details, including any
0
229
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 dg bindings binding the dg to a DIFFERENT arraylist clearing the dg bindings binding the dg to the correct arraylist there must be an easy way, any ideas?
30
2823
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 integer The integers in these arrays are actually pointers to different columns of data in a text file.
1
263
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 As DataRow, _
2
1471
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 As DataRow, _ ByRef arrControl As
9
1996
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", 20}, _ {"First", "String", 20} _ }
3
1367
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 to a piece of equipment and has the following information: Two strings, one for a system name and one for equipment name An integer An ArrayList (which will contain only strings)
0
8577
marktang
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8512
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9048
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8763
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7591
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5793
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4525
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2199
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
1931
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.