当向列表视图添加项目时,如何使用哈希表来避免列表视图中的重复条目?

发布于 2024-08-23 23:32:07 字数 193 浏览 5 评论 0原文

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

what is the way to avoid the redundancy in the listview when the items are added to it..
im using winforms c#.net..
i mean how can i compare between the items in listview1 and items in listview2 so that while adding the items from one listview to another it could not enter the items that are already entered in the target listview..
im able to add items from one listview to other but it is adding auplicate items also what is the way to get rid of it..???

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

三寸金莲 2024-08-30 23:32:08

你可以这样想:

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);
}

你也可以在 如何在 C# 中单击按钮时将所选项目从一个列表视图复制到另一个列表视图网?

You can think of something like:

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"]);
}

Now to meet the changes in the problem after edit, you can do it like this:

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

You can also refer a similar problem in How to copy the selected items from one listview to another on button click in c#net?

奶茶白久 2024-08-30 23:32:08

正如所建议的,哈希表是防止这种冗余的好方法。

As suggested, hash table is a good way to deter such redundancy.

无法回应 2024-08-30 23:32:08

字典...任何数组..所有可能的列表,只需循环抛出项目/子项目,将它们添加到“数组”,然后循环抛出数组以根据其他列表检查它...

这是我使用的一个示例单击按钮即可删除重复项,但您可以轻松更改代码以满足您的需求。

我使用下面的方法在单击按钮时删除列表视图中的“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
    }

祝你好运!

A dictonary ... Any array.. a list all possible, just loop threw the items/subitems, add them to the "array" then loop threw the array to check it against the otehr list...

Here is a example i use to remove dups on a buttonclick, but you can easily change to the code to suit your needs.

I used the below to remove "Dups" in a listview on a button click, i am searching subitem you can edit code for your own use...

uses dictionary and a little easy "update" class i wrote.

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);

    }

and using a little updateList() class...

   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
    }

Good luck!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文