【问题标题】:how to use hash table to avoid duplicate entries in a listview when items are added to it?添加项目时如何使用哈希表来避免列表视图中的重复条目?
【发布时间】:2010-03-09 06:54:24
【问题描述】:

当项目被添加到列表视图时,有什么方法可以避免列表视图中的冗余.. 我使用winforms c#.net .. 我的意思是如何比较 listview1 中的项目和 listview2 中的项目,以便在将项目从一个列表视图添加到另一个列表视图时,它无法输入已在目标列表视图中输入的项目。 我能够将一个列表视图中的项目添加到另一个列表视图中,但它正在添加重复项目,还有什么方法可以摆脱它..???

【问题讨论】:

    标签: c# .net listview items redundancy


    【解决方案1】:

    你可以这样想:

    Hashtable openWith = new Hashtable();
    // Add some elements to the hash table. There are no 
    // duplicate keys, but some of the values are duplicates.
    openWith.Add("txt", "notepad.exe");
    openWith.Add("bmp", "paint.exe");
    openWith.Add("dib", "paint.exe");
    openWith.Add("rtf", "wordpad.exe");
    // The Add method throws an exception if the new key is 
    // already in the hash table.
    try
    {
        openWith.Add("txt", "winword.exe");
    }
    catch
    {
        Console.WriteLine("An element with Key = \"txt\" already exists.");
    }
    // ContainsKey can be used to test keys before inserting 
    // them.
    if (!openWith.ContainsKey("ht"))
    {
        openWith.Add("ht", "hypertrm.exe");
        Console.WriteLine("Value added for key = \"ht\": {0}", openWith["ht"]);
    }
    

    现在遇到编辑后问题的变化,可以这样:

    if(!ListView2.Items.Contains(myListItem))
    {
        ListView2.Items.Add(myListItem);
    }
    

    你也可以参考How to copy the selected items from one listview to another on button click in c#net?中的类似问题

    【讨论】:

      【解决方案2】:

      正如建议的那样,哈希表是阻止这种冗余的好方法。

      【讨论】:

      • 我知道哈希表是解决方案,但我是新手,我不知道如何使用它??你能指导我吗...??
      • 为清楚起见,我正在添加另一个答案和一些示例。
      【解决方案3】:

      一个字典......任何数组......一个列表都是可能的,只需循环抛出项目/子项目,将它们添加到“数组”然后循环抛出数组以检查它是否与其他列表...

      这是我用来删除按钮单击时的重复文件的示例,但您可以轻松更改代码以满足您的需求。

      我使用以下内容在单击按钮时删除列表视图中的“Dups”,我正在搜索子项,您可以编辑代码以供自己使用...

      使用字典和我写的一些简单的“更新”类。

      private void removeDupBtn_Click(object sender, EventArgs e)
          {   
      
              Dictionary<string, string> dict = new Dictionary<string, string>();
      
              int num = 0;
              while (num <= listView1.Items.Count)
              {
                  if (num == listView1.Items.Count)
                  {
                      break;
                  }
      
                  if (dict.ContainsKey(listView1.Items[num].SubItems[1].Text).Equals(false))
                  {
                      dict.Add(listView1.Items[num].SubItems[1].Text, ListView1.Items[num].SubItems[0].Text);
                  }     
      
                  num++;
              }
      
              updateList(dict, listView1);
      
          }
      

      并使用一个小的 updateList() 类...

         private void updateList(Dictionary<string, string> dict, ListView list)
          {
              #region Sort
              list.Items.Clear();
      
              string[] arrays = dict.Keys.ToArray();
              int num = 0;
              while (num <= dict.Count)
              {
                  if (num == dict.Count)
                  {
                      break;
                  }
      
                  ListViewItem lvi;
                  ListViewItem.ListViewSubItem lvsi;
      
                  lvi = new ListViewItem();
                  lvi.Text = dict[arrays[num]].ToString();
                  lvi.ImageIndex = 0;
                  lvi.Tag = dict[arrays[num]].ToString();
      
                  lvsi = new ListViewItem.ListViewSubItem();
                  lvsi.Text = arrays[num];
                  lvi.SubItems.Add(lvsi);
      
                  list.Items.Add(lvi);
      
                  list.EndUpdate();
      
                  num++;
              }
              #endregion
          }
      

      祝你好运!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-03-02
        • 1970-01-01
        • 2014-02-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多