How to clear combobox item collection

tsh

Member
Joined
Jan 5, 2006
Messages
12
Programming Experience
Beginner
Hi....I want to link 2 combobox, thats means when i select 'A' from combobox1,combobox2 will show all the 'A' items in dataset, while i select 'B' it's will show all 'B' item 'B' in combobox2.
The problem is i cant clear the previous items in combobox2 with: combobox.items.clear. Why?My code is as below:

ComboBox2.Items.Clear()
If ComboBox1.SelectedIndex = 0 Then
mydataadpater.SelectCommand = New OleDbCommand
mydataadpater.SelectCommand.Connection = myconnection
mydataadpater.SelectCommand.CommandText = "SELECT Field1 FROM Product WHERE CategoriesP='1'"
mydataadpater.Fill(mydataset, "Product")
ComboBox2.DataSource = mydataset.Tables("Product")
ComboBox2.DisplayMember = "Field1"
ElseIf ComboBox1.SelectedIndex = 1 Then
mydataadpater.SelectCommand = New OleDbCommand
mydataadpater.SelectCommand.Connection = myconnection
mydataadpater.SelectCommand.CommandText = "SELECT Field1 FROM Product WHERE CategoriesP='2'"
mydataadpater.Fill(mydataset, "Product")
ComboBox2.DataSource = mydataset.Tables("Product")
ComboBox2.DisplayMember = "Field1"
End
 
You cannot, nor should you ever want to, clear the items in a bound control. The control is supposed to reflect what is in the data source. If you want to change the data then either modify the data source itself or assign a new data source to the control. If you want the contents of one ComboBox to be filtered based on the SelectedItem in another ComboBox you have two choices.

1. You can set the DefaultView.RowFilter of the second DataTable based on the selection in the first ComboBox.
2. You can create a DataRelation and bind the second ComboBox to the result of GetChildRows each time a new selection is made.
 
I try to use DataRelation, but after i create relation, few error occur and i cant solve it. Before i create relation, the program can run without error.
The error are:
1. custom tool error: Unable to convert input XML file content to a Dataset
2. Overload resolution failed because no accessible 'Fill' can be called with these arguments
3. The designer cannot process the code: Me.ComboBox1.DataSource = Me.Dataset1
4. Type 'Relation2.Dataset1' is not defined.

What should i do with all of these error?

Thanks
 
The error was solved after delete the nested hierarchy...
i add the code below in ComboBox1, so that when i select Product Type in combobox1, all the name of product(Field1) will show in combobox2.
but one error occur: specified casts is not valid....Can i know what's the problem with the code?

Dim selectType As String
selectType = ComboBox1.SelectedValue.ToString
Dim drselectType As DataRow
drselectType = DataSet11.ProductID(selectType)
Dim drProduct As DataRow()
drProduct = drselectType.GetChildRows("ProductIDproduct")
ComboBox2.Items.Clear()
Dim drtype As DataRow
For Each drtype In drProduct
ComboBox2.Items.AddRange(drtype("Field1"))
Next
End Sub
 
I want to do something similar...
I have 2 combobox, in one database i have 2 columns ("id" and "name"), each one binded to its respective combobox(cboid and cboname); what i want is when i select one id from the cboid, automatically make the cboname show that id's name...

I have no idea how to do it, any help?

Thanx
 
Strange Questions

If you are binding the display members to a field of the dataset, which it appears you are, as long as the comboboxes are bound to the same dataset combobox2 should automatically display the correct value. Binding to a dataset means that the item of the combobox correspond to a field in a row of the underlying dataset. Think of switching the combobox's selected item as moving the row pointer of the dataset. If the boxes are correctly bound, when you move the row pointer in one box the value of the other box will change to the value in the new row. Works both ways, changing combobox1 or combobox2 selected index - it is the nature of a databound combobox.
 
sorry i didn't explain well, when i bind it so it shows the data of my table i am adding records ("table1"), it has no problem showing the data combobox 1 and 2, also i can navigate and it changes automatically as you say; now, when I want to add a new record, I clear the data so I can add a new record, but, in the add items of the 2 comboboxes i fill them (items.add) with another table that contrains the full data of what i want to choose from ("table2"), not from the table I'm adding/modifing records.

For Each datarow In mydataset.Tables("table2").Rows
cboid.Items.Add(datarow("id").ToString())
Next
For Each datarow In mydataset.Tables("table2").Rows
cboname.Items.Add(datarow("name").ToString())

so when I'm adding a record, I can choose from the data on table2 to add to my table1 input form, but is kind of annoying to, for example in combo box select the tenth record of the cboid("id"), and then have to make the selection on the cboname("name") again, I want to know how can I select the data in cboid and automatically fill the cboname in the same position as I choose on the cboid...

I hope it's well explained
Thanx
 
set up a public variable to keep the index value:

Public counterIndex As Integer = 0

VB.NET:
    Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
        counterIndex = ComboBox1.SelectedIndex
        ComboBox2.SelectedIndex = counterIndex
    End Sub

    Private Sub ComboBox2_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox2.SelectedIndexChanged
        counterIndex = ComboBox2.SelectedIndex
        ComboBox1.SelectedIndex = counterIndex
    End Sub
Sorry, I misunderstood.
 
Great, that's exactly what i was looking for, thanx a lot, a variable to contain the index value, that's what I wasn't considering...

thank you!!
 
Back
Top