各们高人,在comboBox的Items里有数据aadd,aadd,ccdd,bbcc,bbcc,ccdd,aadd,aadd,ccdd,bbcc请问,如何让每个内容只显示一个,不重复显示?

解决方案 »

  1.   

    Items里的内容是如何添加的 如果是从数据源导入 例如DataSet 那么可以在DataSet里先过滤重复项 否则只好循环读取Items里的内容,再做重复判断可以使用Hashtable来实现
      

  2.   

    for(int i=0; i<this.comboBox1.Items.Count;i++)
    {
    for(int j=1; j<this.comboBox1.Items.Count-1;j++)
    {
    if(this.comboBox1.Items[i].ToString() == this.comboBox1.Items[j].ToString())
    {
    this.comboBox1.Items.Remove(this.comboBox1.Items[j]);
    }
    }
    }
      

  3.   


     
    Hashtable ht = new Hashtable();
    for (int i = 0; i < this.comboBox1.Items.Count; i++)
    {
        if(!ht.ContainsValue(this.comboBox1.Items[i].ToString()))
        {
           ht.Add(i,this.comboBox1.Items[i].ToString());
        }                    
    }  
    this.comboBox1.Items.Clear();
    foreach (DictionaryEntry de in ht)
    {
       this.comboBox1.Items.Add(de.Value);
    }
      

  4.   

    你可以把comboBox的数据放到一个string数组里面或者arraylist里面然后用循环判断是否有重复的