|
-
Nov 11th, 2002, 07:09 PM
#1
Thread Starter
New Member
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??
-
Nov 13th, 2002, 09:04 AM
#2
Thread Starter
New Member
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
-
Nov 13th, 2002, 11:20 AM
#3
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|