Populating a comboBox with an ArrayList

newbjohny

Member
Joined
Jul 10, 2006
Messages
11
Programming Experience
Beginner
Hi, I am trying to populate a ComboBox with the contents obtained within an ArrayList, but instead of putting the names held within the ArrayList, it writes "System.Collections.ArrayList" in the place of the desired names.

Could you take a look at my code and see where I am going wrong.

I am using vb.net 2002 version.

Any help is greatly appreciated.

Many thanks

John

This is the code for the function selectFieldNames :-

VB.NET:
Public fieldNames As New ArrayList()

    Public Function selectFieldNames(ByVal strSQL As String) As ArrayList
        ' select all field names from the selected table
        sqlite_cmd.CommandText = (strSQL)
        ' Now the SQLiteCommand object can give us a DataReader-Object:
        sqlite_datareader = sqlite_cmd.ExecuteReader()
        While sqlite_datareader.Read()
            Try
                fieldNames.Add(sqlite_datareader("name"))
                MessageBox.Show(sqlite_datareader("name"))
            Catch es As Exception
                MessageBox.Show(es.Message)
            End Try
        End While
        Return fieldNames
    End Function
This is the code for populating the ComboBox :-

VB.NET:
Private Sub frmCreateIndex_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
       dbConn.openExistingDatabse("Data Source=" & getDBName() & ";Version=3;New=False;Compress=True;")
        dbConn.createSQLCommand()

        Dim a As Integer
        
        Try
            dbConn.selectFieldNames("pragma table_info(test)")
            For a = 0 To (dbConn.fieldNames.Count) - 1
                cmbFieldIndex.Items.Add(dbConn.fieldNames)
            Next a
        Catch es As Exception
            MessageBox.Show(es.Message)
        End Try
    End Sub
 
You could one of two:
1) do the loop as you do, but instead of adding the ArrayList as item many times you add the items of arraylist: (included For Each example so you see how to do that too)
VB.NET:
For a = 0 To (dbConn.fieldNames.Count) - 1
  cmbFieldIndex.Items.Add(dbConn.fieldNames(a))
Next a
 
'or use For Each
 
For Each item As String In dbConn.fieldNames
  cmbFieldIndex.Items.Add(item)
Next

2) point the Datasource property of ComboBox to the ArrayList
VB.NET:
cmbFieldIndex.DataSource = dbConn.fieldNames
 
As a further point of note.. For all the objects in the Arraylist, the combobox will call ToString() on them all. Whatever is returned by the ToString() call is what is shown in the list.

By default, ToString() returns the class type name such as "system.collections.arraylist". Some objects such as strings, dates etc, have overridden ToString() methods to return a different value, and this is what is shown..


Incidentally, this is why your original code was going wrong:

cmbFieldIndex.Items.Add(dbConn.fieldNames)


dbConn.Fieldnames is an ArrayList object. Your loop repeatedly added the arraylist itself (not an item out of the arraylist) into the combo. When the combo ran through the items and called ToString on them all, it was calling ToString() on an object of type ArrayList. Because ArrayList has no overridden toString() it uses the default implementation of ToString present in Object, which is merely to provide the class name.

A suitable analogy would be to have an icecream tub (Arraylist; a container object) with toys inside (whatever objects are inside your arraylist). Instead of taking a picture of each toy in turn (calling ToString and getting a descriptive reference for the toy) and putting them in a list on the wall(the combo), you took a picture of the ice cream tub itself and repeatedly put it on the wall..

the foillowing article talks a little more about ToString:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemobjectclasstostringtopic.asp


also given that you seem to be using a combo to show what is in a database you might want to show one thing (like Mr, Mrs, Miss) but store another (like an ID number 1, 2, 3)
To do this, modify your program so you get a datatable rather than an arraylist out of your method, and use the DisplayMember and ValueMember accordingly. Suppose you change it to return a datatable with 2 columns, "DispCol" and "valCol":

cmbFieldIndex.DataSource = dbConn.SomeDataTable
cmbFieldIndex.DisplayMember = "DispCol"
cmbFieldIndex.ValueMember = "ValCol"


now use the .SelectedValue property to get what the user selected. If your combo is showing Mr, and your datatable maps this to 1.. then SelectedValue will return 1, not Mr
 
Hiya, sorry for the delay in replying, hope internet been cut off. Many thanks for your help, works fine now.

Thanks again

John
 
Back
Top