简体   繁体   中英

Listview items not showing

Is there a reason why when using listview1.View = View.Details my listview will expand (generate scrollbars) when I add items but have them be invisible, but when I switch it over to listview1.View = View.List it works just fine?

Not that I think that it really matters, but here is the code that I use to add items to the listview:

     ListViewItem item1 = new ListViewItem(file[1]);
     listView1.Items.Add(item1);

And the autogenerated designer code:

        // 
        // listView1
        // 
        this.listView1.CheckBoxes = true;
        this.listView1.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
        this.Path});
        this.listView1.Location = new System.Drawing.Point(12, 44);
        this.listView1.Name = "listView1";
        this.listView1.Size = new System.Drawing.Size(457, 354);
        this.listView1.TabIndex = 7;
        this.listView1.UseCompatibleStateImageBehavior = false;
        this.listView1.View = System.Windows.Forms.View.Details;

file is a string array that contains about 50 odd characters in the first element (checked using debugger).

Are you calling "Clear"? If so make sure you are calling lv.Items.Clear() and not lv.Clear() .

The following code should work:

ColumnHeader columnHeader1=new ColumnHeader();
columnHeader1.Text="Column1";
this.listView1.Columns.AddRange(new ColumnHeader[] { columnHeader1 });
ListViewItem item = new ListViewItem("1");
this.listView1.Items.Add(item);
this.listView1.View = View.Details;

If it doesn't I have no clue. Are the characters of the string you are adding visible?

You need to add a column for Details view to work.

I had the same problem, with not visible text in my listview . The error was made by me, when I cloned the item using code; I accidentally renamed only the first part and added text information again to the first items but not the cloned items. Here is what I mean:

Wrong:

ListViewItem item_klon2 = new ListViewItem();
item_klon.Text = System.IO.Path.GetFileName(file_with_path);
item_klon.SubItems.Add(short_date);
item_klon.SubItems.Add(filesize.ToString() + " kb");

Right:

ListViewItem item_klon2 = new ListViewItem();
item_klon2.Text = System.IO.Path.GetFileName(file_with_path);
item_klon2.SubItems.Add(short_date);
item_klon2.SubItems.Add(filesize.ToString() + " kb");

I had the same problem.

I had set the column width to -2 so it would auto size to the length of the text in the column header. When I switched to Details view the column width was automatically reset to 0, thereby hiding the items.

Just add that to your Listview. Change listview1 your Listview name:

listView1.Columns.Add("Item Column", -2, HorizontalAlignment.Left);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM