Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Delete Item from Combobox 1

Status
Not open for further replies.

smchoudhury

Programmer
Feb 18, 2009
10
GB
Hi guys,
hope you can help.
I have four Combobox with the same items i.e. 1,2,3,4. If an item ischoosen on the first one, the same item cannot be repeatedly shown on the next 3 ones.i.e if i choose item 1 on the first combobox then on the next comboboxes the item 1 should not show up.
Can someone direct me with some code on how i could bind my data where repeately the same item cannot be choosen on different combobox.
Hopes this make sense.

Thank You
 
Here's an example. What I have done is bind the first ComboBox directly to the DataTable. I then have three other DataViews which have their filters modified when the selected item is changed in the ComboBoxes. So for example, if you select "2" in the first ComboBox, "2" will be filtered out of the data source for the other three ComboBoxes. Add this code to a test form and try it out. Then when you're ready, make any changes you need and apply the logic to your project.
Code:
Public Class Form7
    Dim ComboBoxItemsDataTable As New DataTable
    Dim DataView2 As New DataView
    Dim DataView3 As New DataView
    Dim DataView4 As New DataView
    Private Sub Form7_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Configure DataTable and add 4 rows
        Me.ComboBoxItemsDataTable.Columns.Add("Column1", System.Type.GetType("System.Int32"))
        Me.ComboBoxItemsDataTable.TableName = "ComboBoxItemsDataTable"
        Dim dr As DataRow = Me.ComboBoxItemsDataTable.NewRow
        dr(0) = 1
        Me.ComboBoxItemsDataTable.Rows.Add(dr)
        dr = Me.ComboBoxItemsDataTable.NewRow
        dr(0) = 2
        Me.ComboBoxItemsDataTable.Rows.Add(dr)
        dr = Me.ComboBoxItemsDataTable.NewRow
        dr(0) = 3
        Me.ComboBoxItemsDataTable.Rows.Add(dr)
        dr = Me.ComboBoxItemsDataTable.NewRow
        dr(0) = 4
        Me.ComboBoxItemsDataTable.Rows.Add(dr)

        'Configure the DataViews
        Me.DataView2.Table = Me.ComboBoxItemsDataTable
        Me.DataView3.Table = Me.ComboBoxItemsDataTable
        Me.DataView4.Table = Me.ComboBoxItemsDataTable

        'Set the first combobox to the DataTable
        Me.ComboBox1.DataSource = Me.ComboBoxItemsDataTable
        Me.ComboBox1.DisplayMember = "Column1"
        Me.ComboBox1.ValueMember = "Column1"

        'Set other comboboxs to DataViews
        Me.ComboBox2.DataSource = Me.DataView2
        Me.ComboBox2.DisplayMember = "Column1"
        Me.ComboBox2.ValueMember = "Column1"

        Me.ComboBox3.DataSource = Me.DataView3
        Me.ComboBox3.DisplayMember = "Column1"
        Me.ComboBox3.ValueMember = "Column1"

        Me.ComboBox4.DataSource = Me.DataView4
        Me.ComboBox4.DisplayMember = "Column1"
        Me.ComboBox4.ValueMember = "Column1"
    End Sub

    Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) _
    Handles ComboBox1.SelectedIndexChanged, ComboBox2.SelectedIndexChanged, ComboBox3.SelectedIndexChanged
        Call Me.SetDataViewFilters()
    End Sub


    Private Sub SetDataViewFilters()
        'Set Row Filters for DataView
        Try
            Me.DataView2.RowFilter = "Column1 <> " & Me.ComboBox1.Items(Me.ComboBox1.SelectedIndex)(0).ToString
        Catch ex As Exception

        End Try
        Try
            Me.DataView3.RowFilter = "Column1 <> " & Me.ComboBox1.Items(Me.ComboBox1.SelectedIndex)(0).ToString & " AND Column1 <> " & Me.ComboBox2.Items(Me.ComboBox2.SelectedIndex)(0).ToString
        Catch ex As Exception

        End Try
        Try
            Me.DataView4.RowFilter = "Column1 <> " & Me.ComboBox1.Items(Me.ComboBox1.SelectedIndex)(0).ToString & " AND Column1 <> " & Me.ComboBox2.Items(Me.ComboBox2.SelectedIndex)(0).ToString & " AND Column1 <> " & Me.ComboBox3.Items(Me.ComboBox3.SelectedIndex)(0).ToString
        Catch ex As Exception

        End Try
    End Sub
End Class
 

What are you trying to achieve?

If you explain what you are trying to accomplish in your program – there may be a better way to do it. Better way for you as a programmer and for your users.

From the top of my head it looks to me like you want the user to make a selection of 4 items from the same list, and you don’t want to have any items repeated. Why not use a CheckedListBox and make sure your user selects 4 items before proceeding?


Have fun.

---- Andy
 
Yes this is what exactly i'm trying to achieve. I need to use combobox for the purpose of this task (Uni project). If a user chooses an item in the first combobox it cannot be repeated again or show up in the other 3 combobox. Same for combobox 2 if a item is selected in that combobox thje item cannot populate in the other. Therefore users can only choose the item once from the four combobox. I have around 30 items inside the combobox.

Thanks,
 

Thanks RiverGuy, and a star for you :)
Not because you answered my question, but because I learned a lot of how to use DataTable and DataView and RowFilter in ADO.NET. A lot better way than by going thru tutorials....

Thanks.

Have fun.

---- Andy
 
RiverGuy this is really good, i've played around with your code - very good. Learnt something new today.

at the moment the items are set to int32, how can i change the items inside the combobox to string format....i want to show around 32 text item inside there instead of numerical data.

Thanks
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top