Visual Studio 2005 ComboBox - Limiting the items displayed in one ComboBox if a certain item is selected elsewhere

Hi

Is there a way of displaying only certain of the items in a ComboBox at runtimr based on other selections?

Thanks
Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAsked:
Who is Participating?
 
willbdmanConnect With a Mentor Commented:
In this example, I use two listboxes, as these can be easily filled with database data.
For this example I have filled the listboxes using a method, which is executed at load time.


Private Sub FillArray()

        Me.listbox1.Items.Add("Item1")
        Me.listbox1.Items.Add("Item2")
        Me.ListBox1.Items.Add("Item3")

        Me.ListBox2.Items.Add("Item4")
        Me.ListBox2.Items.Add("Item5")
        Me.ListBox2.Items.Add("Item6")

    End Sub


    Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged

        If ComboBox1.SelectedIndex = 0 Then

            ' Clear existing text
            ComboBox2.Items.Clear()

            ' Add new text
            For i As Integer = 0 To Me.ListBox1.Items.Count - 1

                Me.ListBox2.SelectedIndex = i
                ComboBox2.Items.Add(Me.ListBox1.SelectedItem)

            Next

        ElseIf ComboBox1.SelectedIndex = 1 Then

            ' Clear existing text
            ComboBox2.Items.Clear()

            ' Add new text
            For i As Integer = 0 To Me.ListBox2.Items.Count - 1

                Me.ListBox2.SelectedIndex = i
                ComboBox2.Items.Add(Me.ListBox2.SelectedItem)

            Next

        End If

    End Sub
0
 
willbdmanCommented:
When you say other selections - other comboboxes, listboxes?

Why not populate a combobox based upon other selections?
0
 
Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAuthor Commented:
Other ComboBoxes.

Its actually in a DataGridView and I have posted the question several times.

Its a DataGridView with two ComboBox columns. So if the user makes their selection in the first row
what happens when he/she tries the second row.

Lets say the items in the comboBox in the second column are A, B & C

If B was selected in the first row  second column and then I wanted to
limit the ComboBox selection to A and C in the sercond row based on on the first column
selection.

Not sure where to begin?

Any advice would be really helpful
0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

 
willbdmanCommented:
I assume the coboboxes are filled from a database?

Are the combox data linked in anyway?
0
 
Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAuthor Commented:
Hi

Thanks for the help but I think I have answered my own question.

There is no data source.

I have found that you can add items at a cellular level

So if you have a DataGridView called DGV

eg Dim DGVCel As DataGridViewComboBoxCell
    DGVCell = DGV.CurrentCell
    DGVCell.Items.clear
    DGVCell.Items.Add("hat")
0
 
Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAuthor Commented:
I now want to come up with a way to add items to a specific ComboBox Cell in a DataGridView based on a
selection in another ComboBox cell. I will awards the points on this answer
0
 
willbdmanCommented:
Why not have arrays for each item, so if combobox 1 SelectedIndex = 1 fill combobox2 with items from array1 etc.

0
 
Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAuthor Commented:
Hi

Would you mind giving me an example.

Thanks
0
 
Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAuthor Commented:
Great

Thanks very much for the code
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.