Why is my ListView not showing correctly?

So, I have a list as indicated in the title of the question. I have two columns installed: the name and date are changed. They were added to the constructor, here is the code emitted by the designer for reference:

// lstFiles this.lstFiles.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] { this.clmName, this.clmDate}); // ... // clmName this.clmName.Text = "Name"; this.clmName.Width = 105; // clmDate this.clmDate.Text = "Modified"; this.clmDate.Width = 128; 

In the designer, it looks beautiful.

The list items themselves are a tiny subclass of ListViewItem that simply extracts some metadata from the file (in this case, the modified date) and adds the subitem to itself:

 class GalleryItem : ListViewItem { public string File; public DateTime DateModified; public GalleryItem(string file) : base(Path.GetFileNameWithoutExtension(file)) { this.ImageKey = Path.GetExtension(file); File = file; DateModified = System.IO.File.GetLastWriteTime(file); this.SubItems.Add(DateModified.ToString()); } } 

To add items to the list, I just do this:

 lstFiles.BeginUpdate(); lstFiles.Clear(); foreach (String f in files) { ListViewItem lvi = new GalleryItem(f); lvi.Group = lstFiles.Groups["grpFiles"]; //this varries //omitted: check/add icon to list lstFiles.Items.Add(lvi); } lstFiles.EndUpdate(); 

So, all this is great for viewing a large icon, etc .:

Large Icon View

However, it is broken down into a detailed view:

Details view

There are elements in the list (there is a scroll bar). If you click roughly in the column under the red arrow (added with paint), you select the item (the upper right area is a preview of the image), but you will not see anything that would be selected.

So what am I doing wrong?

+4
source share
3 answers

I just whipped a sample to check this:

 using System; using System.Windows.Forms; static class Program { [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); var G1 = new ListViewGroup("Group 1"); var G2 = new ListViewGroup("Group 2"); Application.Run(new Form { Controls = { new ListView { Dock = DockStyle.Fill, Groups = { G1, G2 }, View = View.Details, //Columns = { "First", "Second" }, Items = { new ListViewItem { Text = "One", Group = G1, SubItems = { "1" } }, new ListViewItem { Text = "Two", Group = G2, SubItems = { "2" } }, new ListViewItem { Text = "Three", Group = G2, SubItems = { "3" } }, }, }, }, }); } } 

You will notice that it duplicates the problem. If you uncomment the row that creates the columns, it works. This suggests that your columns do not exist.

And typing this answer, he jumped into the head:

You call ListView.Clear instead of ListView.Items.Clear to remove the columns in the code.

+7
source

I understand that you cannot use Group in Details. I can’t check it right now, so I'm leaving direct memory. However, try filling out your list without groups and see what happens. I strongly suspect that the cause of this problem is part of the group.

0
source

I had exactly the same problem identified by Tergiver, but there is another problem - when you add columns, the ListView should be in View.Details mode.

0
source

Source: https://habr.com/ru/post/1341444/


All Articles