Reputation: 23615
i'm stuck....
this my code to add items to my listview:
ListViewItem item = new ListViewItem(ProjectDomainName);
item.Tag = relatedProject.ProjectId;
lvwSelectedProjects.Items.Add(item);
when i choose 'View.List'
as viewmode, i see all items.
When i choose 'View.Details'
(which is the setting that i want) i see.... nothing. Well, nothing, i DO get a vertical scrollbar, but no items. And i can scroll too, but no items....
I also added a column in the listview (didn't change the add items code), but that also didn't work
i must be overlooking something?
Upvotes: 16
Views: 23821
Reputation: 1
ListViewItem item = new ListViewItem("item1");
item.SubItems.Add("subitem"); //add subitem if applicable
listview1.Items.Add(item);
this Result can be sold your problem
Upvotes: -1
Reputation: 21243
Another possible cause of blank items when listview.View = View.Details, is if you don't add any columns to the listview.
For example:
ListView lv = new ListView();
lv.View = View.Details;
lv.Items.Add("Test");
.. will result in a blank ListView.
Adding a column will correct:
...
lv.View = View.Details;
// Add one auto-sized column, to show Text field of each item.
lv.Columns.Add("YourColumnTitle", -2);
...
Upvotes: 3
Reputation: 4492
This happened to me as well (listview not showing items in details view) I just put the following into the code (previously was only in the design) after adding items to the listview and it started showing the items.
lv.View = View.Details;
Upvotes: -1
Reputation: 89
Because, you should be using a ListViewDataItem instead of a ListViewItem, observe ...
for (int i = 0; i < AudioCdWriter.FileCount; ++i) {
var item = new ListViewDataItem(i.ToString());
item.SubItems.Add(AudioCdWriter.TrackLength((short)i).ToString());
item.SubItems.Add(AudioCdWriter.file[(short)i]);
lvwAudioFiles.Items.Add(item);
}
Upvotes: 0
Reputation: 9492
This code works for me:
using System;
using System.Windows.Forms;
public class LVTest : Form {
public LVTest() {
ListView lv = new ListView();
lv.Columns.Add("Header", 100);
lv.Columns.Add("Details", 100);
lv.Dock = DockStyle.Fill;
lv.Items.Add(new ListViewItem(new string[] { "Alpha", "Some details" }));
lv.Items.Add(new ListViewItem(new string[] { "Bravo", "More details" }));
lv.View = View.Details;
Controls.Add(lv);
}
}
public static class Program {
[STAThread] public static void Main() {
Application.Run(new LVTest());
}
}
Try this code for yourself in an empty project. Then, focus on adapting it to your application: compare how your program is different from this code, and work on changing it to more closely match mine. It's OK if you lose functionality in your program; just try to get a basic version working. Then, add functionality back bit by bit so you can be sure that the program still works every step of the way.
If you're still stuck, post more code from your project and we might have a better idea of why you're having trouble.
Upvotes: 16