过滤listview的重复项, 
如: id    name                  id    name 
    1      abc                  1      abc 
    2      edf                  2      edf 
    3      abc        ==>>      4      hhh 
    4      hhh                  
    5      abc 
    6      edf 我做的是 WINFORM 程序 具体代码如何编写!找了3天了,没有正确答案,难道这个实现起来这么困难啊!! 

解决方案 »

  1.   

    1、填充listview之前,一条一条的判断
    2、填充好之后,先按name排序再删除
    3、填充之前就过滤掉name重复的项,然后再填充listview(个人倾向于)
      

  2.   


    delete from table where id not in (select min(id) from table group by name)
      

  3.   

    最简单的办法。先过滤。在填充。可以在数据库里过滤。也可以在程序里过滤。
    select id,name from 表 where 1=1 group by id,name在程序中。先获得一个DataTable。然后FOREACH这个DT。检索重复的。
    检索的代码就不写了。这个问题可以有很多种方法去实现它
      

  4.   

    用HashTable处理  循环添加到HashTable,然后再从Hashtable中添加到新的dataView中
      

  5.   


    select   *   from   emp where name   in
    (select   name   from   emp group   by   name having count(*)>1)
    这样直接查出重复的数据
    也就是你上面的:
            id     name 
           1      abc 
           2      edf 
           4      hhh 
                   
      

  6.   

    楼主如果想在程序里排出的话
    就一边判断一边加载数据到listview里  如果名字有重复的话不在添加就OK
      

  7.   

    SELECT * FROM TABLE1 WHERE NOT EXISTS(SELECT * FROM TABLE1 T WHERE TABLE1.ID>T.ID AND TABLE1.NAME=T.NAME) 
    直接数据库操作!!
      

  8.   


     public partial class Form1 : Form
        {
            int[] saveNum = new int[5] { 1, 2, 3, 4, 5 };
            string[] saveString = new string[5] { "abc","edf","abc","hhh","abc" };        public Form1()
            {
                InitializeComponent();            for (int i = 0; i < saveNum.Length; i++)
                {
                    string[] savedItem = new string[2];
                    savedItem[0] = saveNum[i].ToString();
                    savedItem[1] = saveString[i];
                    //将所有数据存储在列表视图项中
                    ListViewItem item = new ListViewItem(savedItem);
                    listView1.Items.Add(item);
                }
                SearchDunplication();
            }        public void SearchDunplication()
            {
                for (int i = 0; i < listView1.Items.Count; i++)
                {
                    for (int j = i + 1; j < listView1.Items.Count; j++)
                    {
                        if (listView1.Items[i].SubItems[1].Text == listView1.Items[j].SubItems[1].Text)
                        {
                            MessageBox.Show("There found the same text ."+i+":"+j+"   "+listView1.Items[i].SubItems[1].Text);
                            //and then delete the same items from these,but save only one item.
                        }
                    }
                }        }
        }做了1个模型,你拿去瞅瞅
      

  9.   

    你的ListView里绑的是么噢?
    直接拿那个源来进行过滤再绑就行了嘛
      

  10.   


    我是这么做的 但是我只会 listbox 的 contains 方法 
    用到listview上不行 才来求教的 很多朋友很热心提供了一些思路 我谢谢大家了 思路不是问题的 问题我是新手 我实现不了  所以望高人们可以给出代码 这样直观点,我也可以学习下!还有根多朋友给出的 SQL语句 我开头说了 我做的是 WINFORM 
    接下来继续等待 高人的出现!!!
      

  11.   

    你的数据是从的数据库里得到绑定的吧,取数据的时候加上Distinct关键字:select distinct  *   from   emp 
      

  12.   

                    string[] z = new string[] { a, b, c, d, e, f, g, h };
                    z1 = z[s.Next(0, 8)];
                    for (int i = 1; i <= s.Next(1,8); ++i)
                    {
                        if (!listBox1.Items.Contains(z1)) //如果不包含这个项则添加
                        {
                            listBox1.Items.Add(z1);
                        }
                        else
                        {
                            while (listBox1.Items.Contains(z1))//如果包含则重新取
                            {
                                z1 = z[s.Next(0, 8)];
                            }
                            listBox1.Items.Add(z1);//直到不包含在内,则添加进去
                        }
                    } 
    这个是listbox实现的代码 简单说 我想用listview 控件 代替 listbox 代码该如何改!!
      

  13.   

      listView1.Items.ContainsKey()
      

  14.   

      从数据来源的源头解决,如果是数据库的查询结果,则先 去掉重复的 id 再绑定到listView 
      
      

  15.   

    用linq distinct一下不知道OK不?