ComboBox use ListBox as data source

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,

I've got a number of ListBoxes that hold data from an Access database. On
another tab of my form I need the user to use a ComboBox to choose one of the
items from one of the ListBoxes. I have attached the ComboBox to my Access
database and this works fine. However, it seems to me that I should be able
to attach it to the ListBox that has that same information instead. Can I do
this?

Art
 
I haven't tested this but you should be able to do this...

Dim L as new ListBox
Dim C as new ComboBox
''Do Databindings on ListBox
C.DataSource = L.DataSource

Hope this helps
Chris
 
Art,

Yes by creating more dataviews (with sort and/or rowfilter) and use that as
the datasources.

I hope this helps?

Cor
 
Cor,

Thanks -- but I don't know enough yet to really understand. Also in
reference to the response from Chris, I don't have my ListBoxes connected to
a datasource. I fill them up by reading from a database (perhaps this isn't
good).

Unfortunately I don't know about "dataviews" -- could you refer me to
something or give me a hint.

I apologize for starting out with so little knowledge and little experience
in this area.

thanks,

Art
 
Can you post your code that you are using to fill the listbox? It will help
us give you better input.

If you are binding the Listbox to at database then you have a datasource.
Step through your code and look at the value of the datasource. Unless you
are manually using the Listbox.items.add method. Then you aren't using
databindings at all.

Chris
 
Chris,

I'm not using data bindings at all. The code just shows me reading data
from a database and using the "add" method (as you mentioned). I haven't
used data binding yet, although I'm sure it would make my life easier if I
learned how.

Thanks,

Art
 
Art,

This is a little bit crazy sample I made now from a sample I made yesterday.
However I think that everything is in it that you need.
I used a Northwind sample access database, mostly there are a lot on your
computer when you have downloaded Microsoft samples.

You have to make a new project, add a combobox and a listbox.
Paste this in the code, change the dates in row 3 and 4 to your local
formats and run

\\\
Private Sub Form4_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Dim startdate As String = "20-07-1996" 'this in your local standard format
Dim enddate As String = "31 juli 1996" 'this in your local standard format
'Used are Dutch settings
Dim conn As New OleDb.OleDbConnection _
("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\test1\northwind.mdb")
Dim da As New OleDb.OleDbDataAdapter _
("SELECT OrderId, OrderDate FROM Orders WHERE (OrderDate > ?) AND (OrderDate
< ?)", conn)
da.SelectCommand.Parameters.Add _
(New OleDb.OleDbParameter("", OleDb.OleDbType.Date))
da.SelectCommand.Parameters.Add _
(New OleDb.OleDbParameter("", OleDb.OleDbType.Date))
da.SelectCommand.Parameters(0).Value = CDate(startdate)
da.SelectCommand.Parameters(1).Value = CDate(enddate)
Dim ds As New DataSet
da.Fill(ds)
ComboBox1.DataSource = ds.Tables(0)
ComboBox1.DisplayMember = "OrderDate"
Dim dv As New DataView(ds.Tables(0))
dv.RowFilter = "OrderId > 10260"
ListBox1.DataSource = dv
ListBox1.DisplayMember = "OrderId"
End Sub
///

I hope this helps?

Cor
 
Cor,

Thank you very much -- it will take me a little time to go through your
example. I very much appreciate the time it must have taken for you to put
this together, I'm sure I will learn a lot.

Art
 
Gotcha. Well now that I understand that, her is the simpliest solution to
your orginal question.

L is your listbox and C is your combobox.

Dim I as Object
For Each I In L.Items
C.Items.AddRange(I)
Next

Cor has shown you in his post how to do databinding. Mine will get you what
you want I think quickly.
Chris
 
Note: I haven't tested this, but it should work

Dim I as Object
For Each I In L.Items
C.Items.Add(I)
Next
 
Chris,

Thanks -- as with cor's answer this will take me a little time to try out.
I really appreciate your help.

Art
 
Art said:
Hi,

I've got a number of ListBoxes that hold data from an Access database. On
another tab of my form I need the user to use a ComboBox to choose one of the
items from one of the ListBoxes. I have attached the ComboBox to my Access
database and this works fine. However, it seems to me that I should be able
to attach it to the ListBox that has that same information instead. Can I do
this?

You no doubt got a lot of good information from other replies, but the
direct route to what you wanted to do was possible with one line of code:

Combobox1.DataSource = Listbox1.Items

LFS
 
Chris,

In my opinion can you even do it directly with the collection with AddRange.
They both have the same base class and the collection is implementing Ilist.
Than you get the same solution as with your first sample. However I show no
sample because it would only confuse Art in my opinion.

The problem with this solution is, that there is not a selection, while the
dataview solution especially using the value from the first is very good to
use. (As well not showed to make it not to complicated).

Cor
 
Back
Top