将项目插入 C# Winforms 列表视图

发布于 2024-12-12 02:17:47 字数 281 浏览 0 评论 0原文

下面的行让我非常头疼:

listView1.Items.Insert(0, new ListViewItem("Test", listView1.Groups[0]));

我想做的就是将一个项目插入到列表视图中。列表视图处于详细模式并启用了组。插入的项目应进入索引 0 处的第一组。 但发生的情况是,该项目始终添加为组中的最后一个项目。好像 Insert(...) 的第一个参数没有效果...

我在这里缺少什么吗?

顺便说一句:列表视图上禁用排序!

The following line gives me a serious headache:

listView1.Items.Insert(0, new ListViewItem("Test", listView1.Groups[0]));

All I want to do is insert an item into a list view. The listview is in detailed mode with groups enabled. The inserted item should go into the first group at index 0.
But what happens is that the item is always added as the LAST item in the group. As if the first parameter of Insert(...) had no effect...

Anything I'm missing here?

BTW: Sorting is disabled on the listview!

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

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

发布评论

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

评论(3

凉墨 2024-12-19 02:17:47

您可以尝试:

ListViewItem item = new ListViewItem("Test");
this.listView1.Items.Insert(0, item);
this.listView1.Groups[0].Items.Insert(0, item);

详细讨论可以参见此处

此示例将三个组添加到列表视图,并在组的第一个位置添加项目:

for (int groupIndex = 0; groupIndex < 3; ++groupIndex) {
   this.listView1.Groups.Add("GroupKey" + groupIndex, "Test" + groupIndex);

   for (int index = 0; index < 5; ++index) {
      ListViewItem item = new ListViewItem("Test " + groupIndex + "/" + index,
                                           this.listView1.Groups[groupIndex]);
      this.listView1.Items.Insert(0, item);
      this.listView1.Groups[groupIndex].Items.Insert(0, item);
    }
 }

 for (int groupIndex = 2; groupIndex >= 0; --groupIndex) {
    for (int index = 0; index < 5; ++index) {
      ListViewItem item = new ListViewItem("Test2 " + groupIndex + "/" + index,
                                           this.listView1.Groups[groupIndex]);
      this.listView1.Items.Insert(0, item);
      this.listView1.Groups[groupIndex].Items.Insert(0, item);
   }
 }

这是结果:
在此处输入图像描述

You can try:

ListViewItem item = new ListViewItem("Test");
this.listView1.Items.Insert(0, item);
this.listView1.Groups[0].Items.Insert(0, item);

A detailed discussion can be found here.

This example adds three groups to a listview and adds items at the first position of the groups:

for (int groupIndex = 0; groupIndex < 3; ++groupIndex) {
   this.listView1.Groups.Add("GroupKey" + groupIndex, "Test" + groupIndex);

   for (int index = 0; index < 5; ++index) {
      ListViewItem item = new ListViewItem("Test " + groupIndex + "/" + index,
                                           this.listView1.Groups[groupIndex]);
      this.listView1.Items.Insert(0, item);
      this.listView1.Groups[groupIndex].Items.Insert(0, item);
    }
 }

 for (int groupIndex = 2; groupIndex >= 0; --groupIndex) {
    for (int index = 0; index < 5; ++index) {
      ListViewItem item = new ListViewItem("Test2 " + groupIndex + "/" + index,
                                           this.listView1.Groups[groupIndex]);
      this.listView1.Items.Insert(0, item);
      this.listView1.Groups[groupIndex].Items.Insert(0, item);
   }
 }

This is the result:
enter image description here

青衫儰鉨ミ守葔 2024-12-19 02:17:47

我也有这个问题。我没有使用任何组或排序。尽管如此,如果我尝试在任何索引处插入,它总是显示在最后。我必须创建一个“排序器”,强制列表视图始终使用与 Items 集合相同的顺序。

class CompareByIndex : IComparer
{
    private readonly ListView _listView;

    public CompareByIndex(ListView listView)
    {
        this._listView = listView;
    }
    public int Compare(object x, object y)
    {
        int i = this._listView.Items.IndexOf((ListViewItem)x);
        int j = this._listView.Items.IndexOf((ListViewItem)y);
        return i - j;
    }
}

为了使用它,

   this.listView1.ListViewItemSorter = new CompareByIndex(this.listView1);

我希望可以使用 lambda 表达式而不是辅助类。但我不知道怎么做。

I had this problem also. I'm not using any groups or sorting. Still, if I try to insert at any index, it always show up LAST. I had to create a "sorter" that forces the listview to always use the same order as the Items collection.

class CompareByIndex : IComparer
{
    private readonly ListView _listView;

    public CompareByIndex(ListView listView)
    {
        this._listView = listView;
    }
    public int Compare(object x, object y)
    {
        int i = this._listView.Items.IndexOf((ListViewItem)x);
        int j = this._listView.Items.IndexOf((ListViewItem)y);
        return i - j;
    }
}

and to use it

   this.listView1.ListViewItemSorter = new CompareByIndex(this.listView1);

I wish that I could use a lambda expression instead of a helper class. But I can't figure out how.

薄情伤 2024-12-19 02:17:47

某种魔法,

如果您将一个项目添加到列表视图中,
并将G组分配给该项目,该项目将显示不正确。

现在,如果你得到持有该项目(G)的组,将其名称更改为某个临时值,然后更改回原始名称,一切都会显示正常。

所以而不是

listView1.Items.Insert(0, new ListViewItem("Test", listView1.Groups[0]));

Dim LVI as new ListViewItem("Test")
listView1.Items.Insert(0, LVI)
LVI.Group = listView1.Groups[0]

Dim TempStr as string = LVI.Group.Header
LVI.Group.Header = "whatever"
LVI.Group.Header = TempStr

Some kind of wizardry,

if you add an item to a list view,
and assign group G to the item, the item will be displayed out of place.

Now, if you get the group holding the item (G), change its name to some temporary value, then change back to original name, everything will be displayed OK.

So instead of

listView1.Items.Insert(0, new ListViewItem("Test", listView1.Groups[0]));

do

Dim LVI as new ListViewItem("Test")
listView1.Items.Insert(0, LVI)
LVI.Group = listView1.Groups[0]

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