简体   繁体   English

删除和插入新列后,ListView 项目不会更改

[英]ListView Items doesn't change after removing and inserting new column

I have a ListBox filled with a few items, in the SelectedIndexChanged event i tend to remove any existent extra Columns (ID 1 and bigger), after that, i create a new Column and add Items all over again, but the previous Items stay intact, it's like the Add() overload is not working, here is my code:我有一个装满一些项目的ListBox ,在SelectedIndexChanged事件中,我倾向于删除任何现有的额外列(ID 1 和更大),之后,我创建一个新列并重新添加项目,但以前的项目保持不变,就像Add()重载不起作用,这是我的代码:

if (listBox1.SelectedItems.Count > 0)
        {
            if (listView2.Columns.Count > 1)
            {
                int bla = listView2.Columns.Count;
                for (int i = 1; i < bla; i++)
                {
                    //MessageBox.Show("Removing Column: " + (bla - i));
                    listView2.Columns.RemoveAt(bla - i);
                }
            }

            if (clmnnum[listBox1.SelectedIndex] == 1)
            {
                listView2.SuspendLayout();
                listView2.Columns.Add("Primary Values", 90, HorizontalAlignment.Left);
                listView2.ResumeLayout();
                listView2.Items[0].SubItems.Add(exp[listBox1.SelectedIndex].ToString());
                listView2.Items[1].SubItems.Add(hpe[listBox1.SelectedIndex].ToString());
                listView2.Items[2].SubItems.Add(lve[listBox1.SelectedIndex].ToString());
                listView2.Items[3].SubItems.Add(stre[listBox1.SelectedIndex].ToString());
                listView2.Items[4].SubItems.Add(powe[listBox1.SelectedIndex].ToString());
                listView2.Items[5].SubItems.Add(ende[listBox1.SelectedIndex].ToString());
            }
        }

The ListBox contains as many items as there are in each Array(clmnnum,exp,hpe,etc...) ListBox 包含与每个 Array 中一样多的项目(clmnnum、exp、hpe 等...)

I'm not entirely sure what the problem is here, you say the Add overload doesn't appear to be working yet you state the problem is the old items are remaining, at least I assume that's what you mean by "the previous items stay intact."我不完全确定这里的问题是什么,你说 Add 重载似乎不起作用,但你说问题是旧项目仍然存在,至少我认为这就是你所说的“以前的项目保持不变”完整。”

If I understand what you are attempting to do, I would encourage you to clear listView2's items, and have a function to build listView2 as you would like it from nothing each time.如果我理解你想要做什么,我会鼓励你清除 listView2 的项目,并有一个函数来构建 listView2,因为你每次都希望它从无到有。

It should at least give you an idea as to what is happening.它至少应该让您了解正在发生的事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM