Results 1 to 3 of 3

Thread: ComboBox and collections

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2002
    Location
    Norway
    Posts
    8

    ComboBox and collections

    I try to make a collection in a combobox. I want the elements in the colloection to come from a database (Access). How can I make this collection??

  2. #2

    Thread Starter
    New Member
    Join Date
    Nov 2002
    Location
    Norway
    Posts
    8

    solution with a static array

    I found a way to solve this problem, but only with a static array wich means you`ll get a problem the if the database got further elements than you allow in your collection.
    So if anyone knows how it solve this with a dynamic array, you`re welcome.

    Here is my solution:

    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
    Dim cs As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\test.mdb"

    Dim sql = "select * from tableName"
    Dim row As System.Data.DataRow
    Dim s
    Dim i = 0
    Dim arrlist(100)
    Dim dbconn As New OleDbConnection(cs)
    Dim command As New OleDbCommand(sql, dbconn)
    Dim da As New OleDbDataAdapter(command)
    Dim ds As New System.Data.DataSet("DataSetName")

    Try
    dbconn.Open()
    Catch
    Label1.Text = ""
    Label1.Text = "couldn´t connect to database!" & vbCrLf
    End Try

    da.Fill(ds, "tableName")
    Label1.Text = ""

    For Each rad In ds.Tables("tableName").Rows
    s = rad.Item("fieldname")
    arrlist(i) = s
    ComboBox1.Items.Add(arrlist(i))
    i += 1
    Next

    dbconn.Close()

    End Sub

  3. #3
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    You could also set the datasource property of the combo to the datasource after it is filled, instead of looping. Make sure to set the DisplayMember property to the field you want to be displayed.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width