Avoid Duplicate in Combobox-VBForums
Results 1 to 3 of 3

Thread: Avoid Duplicate in Combobox

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2008

    Question Avoid Duplicate in Combobox

    Hi All,

    I have a combo box control in my application. I have to add the items from the database. I added it from database. But it adding duplicate data. Like by running the combo box second time it adding the same data once again. So i need to check the combo box that the data item is new to the combo box or a old one. If its a old one it should be avoid or else it want to be added to the combo box.

    I used SQL server2000 and SQL query to load the data into the combo box. so let me have a idea on it.

    Thanks all.

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Sydney, Australia

    Re: Avoid Duplicate in Combobox

    The short answer to your question is to call Contains on the control's Items collection to tell you whether a value is already in the list.

    That may not be the best solution though. How exactly are you populating the ComboBox in the first place? How are you getting the new data and how does it come about that it contains duplicates? It would likely be better if you could avoid retrieving duplicates in the first place. It would also be likely better to just bind your data.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    PowerPoster Deepak Sakpal's Avatar
    Join Date
    Mar 2002
    Mumbai, India

    Re: Avoid Duplicate in Combobox

    There are two ways:

    1. Clear all the items from Combo box and add all the items again.
    vb.net Code:
    1. Private Sub Button2_Click( _
    2.     ByVal sender As System.Object, _
    3.     ByVal e As System.EventArgs _
    4. ) Handles Button2.Click
    6.     ComboBox1.Items.Clear()
    7.     ComboBox1.Items.Add("One")
    8.     ComboBox1.Items.Add("Two")
    9.     ComboBox1.Items.Add("Three")
    10.     ComboBox1.Items.Add("Four")
    11. End Sub

    2. Before adding any item to the combo box, check for it's existence in the combo box item list collection. If not found then add it else ignore.
    vb.net Code:
    1. Private Sub Button1_Click( _
    2.     ByVal sender As System.Object, _
    3.     ByVal e As System.EventArgs _
    4. ) Handles Button1.Click
    6.     If ComboBox1.Items.IndexOf(TextBox1.Text) = -1 Then
    7.         ComboBox1.Items.Add(TextBox1.Text)
    8.     End If
    9. End Su

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts


Click Here to Expand Forum to Full Width