question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ListView ListViewGroupCollection.Insert not working properly (incorrect location)

See original GitHub issue

.NET version

.net 7.0

Did it work in .NET Framework?

No

Issue description

ListViewGroupCollection.Insert not working properly.

Users can use ListViewGroupCollection.Insert to insert new data. But currently it doesn’t work properly (incorrect location). https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.listviewgroupcollection.insert

How to test:

  1. Copy the full test code to Visual Studio (see Steps to reproduce).
  2. Add a breakpoint to this line. (sender as Button).Enabled = false;

Picture 1. Run the test code, click this button to start test.

000

Picture 2. If you have set breakpoint, the “Watch 1” dialog shows the position of the inserted object in relation to other elements. If you haven’t set a breakpoint, Add a breakpoint to this line, and then add “view.Groups” to “Watch 1” dialog. (sender as Button).Enabled = false;

001

Picture 3. In the actual running UI, the display effect of ListView is not consistent with the relationship in the previous “Watch 1” dialog box.

0021

Steps to reproduce

Full C# Test code with Visual Studio 2022 and .net 7.0

using System;
using System.Drawing;
using System.Windows.Forms;

namespace ListViewGroupInsertTest
{
    internal static class Program
    {
        [STAThread]
        static void Main()
        {
            ApplicationConfiguration.Initialize();
            Application.Run(CreateForm());
        }

        private static Form CreateForm()
        {
            var form = new Form()
            {
                Width = 600,
                Height = 480,

                StartPosition = FormStartPosition.CenterScreen,
            };

            var control = _listView = CreateListView();
            form.Controls.Add(control);

            var button = new Button()
            {
                Text = "Click to insert Group and Item",
                AutoSize = true,

                MaximumSize = new Size(0, 45),
                Dock = DockStyle.Right
            };

            button.Click += Button_Click;

            form.Controls.Add(button);
            return form;
        }

        private static ListView CreateListView()
        {
            var control = new ListView()
            {
                Dock = DockStyle.Fill,
                View = View.Details,

                FullRowSelect = true,
            };

            CreteListViewContent(control);
            return control;
        }

        private static ListView _listView;

        private static ListViewGroup _group_new;
        private static ListViewItem _item_new;

        private static void CreteListViewContent(ListView view)
        {
            ListViewGroup group_1;
            ListViewGroup group_2;
            ListViewGroup group_3;
            ListViewGroup group_4;

            view.Groups.Add(group_1 = new ListViewGroup() { Header = "Group 1" });
            view.Groups.Add(group_2 = new ListViewGroup() { Header = "Group 2" });
            view.Groups.Add(group_3 = new ListViewGroup() { Header = "Group 3" });
            view.Groups.Add(group_4 = new ListViewGroup() { Header = "Group 4" });

            view.Columns.Add(new ColumnHeader() { Text = "Column 1", Width = 300 });
            view.Columns.Add(new ColumnHeader() { Text = "Column 2", Width = -2 });

            view.Items.Add(new ListViewItem("ListView Item 1") { Group = group_1 });
            view.Items.Add(new ListViewItem("ListView Item 2") { Group = group_2 });
            view.Items.Add(new ListViewItem("ListView Item 3") { Group = group_3 });
            view.Items.Add(new ListViewItem("ListView Item 4") { Group = group_4 });

            _group_new = new ListViewGroup() { Header = "Group (New)" };
            _item_new = new ListViewItem("ListView Item (New)") { Group = _group_new };
        }

        private static void Button_Click(object sender, EventArgs e)
        {
            var view = _listView;

            view.Groups.Insert(3, _group_new);
            view.Items.Add(_item_new);

            (sender as Button).Enabled = false;
        }
    }
}

Issue Analytics

  • State:closed
  • Created 8 months ago
  • Comments:13 (13 by maintainers)

github_iconTop GitHub Comments

1reaction
Amy-Li03commented, Jan 30, 2023

Is it a regression? Does it work in .net 6/7?

It is not a regression, it repro on .NET 5.0, 6.0, 7.0 and 8.0.

0reactions
lonitracommented, Feb 9, 2023

We discussed this as a team and decided that given that this has been the behavior since Framework, it is too risky to fix as it could break existing apps with unwanted UI changes. The workaround here is if the issue is that the group does not show up in the desired position in the UI, this can be worked around by incrementing the index or using the Add method for new groups wanting to be placed at the end. If the issue is that the UI and ListView.Group list do not match up, this can be worked around by removing groups starting from the index user wants to add new group in, then re-adding the groups in correct order using the Add method.

Read more comments on GitHub >

github_iconTop Results From Across the Web

c# - ListViewGroups not working correctly
I'm working with a ListView control in Windows Forms using C# and .NET 3.5. I have several items that I'm adding to this...
Read more >
c# - ListViewItemCollection.Insert broken?
Inserting an item into a specific position in a Listview will possible only in View.List mode. The below code works fine.
Read more >
ListViewGroupCollection.Insert is missing validation ...
public int Add(ListViewGroup group) { if (group == null) { throw new ArgumentNullException(nameof(group)); } if (this.
Read more >
Breaking change: ListViewGroupCollection methods throw ...
Learn about the breaking change in .NET 6 where some ListViewGroupCollection methods throw a new InvalidOperationException if the ListView ...
Read more >
ListViewGroupCollection Class (System.Windows.Forms)
Represents the collection of groups within a ListView control.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found