combo boxes

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

Guest

I actually have a 3 part question.

1. How can I filter the items in combobox2 based on an item I select in
combobox1.

2. How can I make the start or initial value of the combobox blank or nothing?

3. When I select values in a combobox and close the form, then reopen the
form, the values are exactly the same as when I closed the form. How can I
make it refresh or start from the beginning?

Thanks,

Mike S.
 
Mike,

Assuming that you use dataviews (you did not give any information about this
to us)

Set the result of combobox 1 as rowfilter in the dataview of combobox 2

Cor
 
You should bind both combos to dataviews...the second of which has a foreign
key field linking each row to the primary key of the first combo dataview.

For each combo, after you've set the datasource to the dataviews, set the
DisplayMember to the field you want to be visible, and the ValueMember to the
primary key. When an item in Combo1 is selected, apply the ValueMember for
the item selected to the rowfilter of Combo2's dataview...

dataview2.rowfilter = "ForeignKeyField = " & combo1.selectedvalue.tostring

http://msdn2.microsoft.com/en-us/library/system.data.dataview.rowfilter(vs.71).aspx

You can make the first item blank by creating record in the database table
from which you create your dataview. The Display Field for that record
should be blank. Your select statement should have an ORDER BY that assures
that the blank row is first.

When the user selects the blank row in Combo1, you could set rowfilter = ""
for Combo2. That would restore Combo2 to all records in the dataview. Or
you could use a refresh button that resets Combo1 to the first (blank item)
and resets the Combo2 rowfilter = "".

To have the form remember values, you need a stash point outside the form
that stores and retreives values. I would use a hashtable in a module. The
keys could be a combination of the form name and the combobox name, and the
value could be the selectedvalue of the combobox.
 
Sorry for not giving enough info. Here is the code I use for displaying the
items in the combobox:

strSQL = "Select MachineType From MachType"
Using conn As New SqlClient.SqlConnection _
("Server=" & dbServer & ";Database=" & dbDatabase & ";User ID="
& dbUserName & ";Password=" & dbPassword)
Using com As SqlClient.SqlCommand = conn.CreateCommand
conn.Open()
ds = New DataSet
da = New SqlClient.SqlDataAdapter(strSQL, conn)
da.Fill(ds, "MachType")

Dim ac As New AutoCompleteStringCollection

For Each dr As DataRow In ds.Tables("MachType").Rows
ac.Add(dr.Item("MachineType").ToString)
cboMachType.Items.Add(dr.Item("MachineType").ToString)
Next

cboMachType.AutoCompleteMode = AutoCompleteMode.Append
cboMachType.AutoCompleteSource =
AutoCompleteSource.CustomSource
cboMachType.AutoCompleteCustomSource = ac

End Using
End Using

The problem with this is when I select an item from the drop down, it
duplicates all the items so I have two of each item in the drop down. If I
close the form and reopen it and select the drop down, there will still be
duplicates of each item. And the next time there will be four of each item
and so on.

I tried using the same code for the second combobox and using the
"SelectedItem" as the search criteria in the "SQL Search" statement but I
coulnd't get it to work.

Let me know if you need more information.

Thanks,

Mike S.
 
Just to clarify...The two datasources are separate dataviews (not joined),
each created by passing a datatable to the dataview's constructor. The
foreign key field of dataview2 provides the information necessary to make the
rowfilter for dataview2 work.
 
Also, each time you change the rowfilter, I think you have to rebind the
datasource for Combo2.
 
Back
Top