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:
- Copy the full test code to Visual Studio (see Steps to reproduce).
- Add a breakpoint to this line.
(sender as Button).Enabled = false;
Picture 1. Run the test code, click this button to start test.
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;
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.
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:
- Created 8 months ago
- Comments:13 (13 by maintainers)
Top GitHub Comments
It is not a regression, it repro on .NET 5.0, 6.0, 7.0 and 8.0.
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 andListView.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 theAdd
method.