简体   繁体   中英

ListView Items not showing in ListView and columns disappear

I have a ListView in a tab control on a WinForm. When the form first loads I can see all the headers as expected:

在此输入图像描述

I populate the ListView with details of files:

string[] fileEntries = Directory.GetFiles(trNode.Tag.ToString(), "*.*", SearchOption.TopDirectoryOnly);
int fileNo = 1;
foreach (string fileName in fileEntries)
{
    FileInfo oFileInfo = new FileInfo(fileName);
    ListViewItem lvi = new ListViewItem(new string[] { fileNo.ToString(), oFileInfo.Name, oFileInfo.Extension, oFileInfo.Length.ToString(), oFileInfo.CreationTime.ToString() });
    lvFiles.Items.Add(lvi);
    fileNo++;
}

Once the form has loaded the column headers have disappeared and no items show.

在此输入图像描述

I have no code to change the visibity of the ListView or any containers. As you can see after the load there is a scroll bar for the ListView which I'm guessing suggests its not hidden.

Any suggestions would be most welcome!

UPDATE

The listview Items.Count tells me there are item in the listview. Even stranger is that if I change the view to SmallIcons I see them, in Details view they disappear! If I break the code and look at the items they look fine with the right data in all the right places!

The answer to this problem was something very embarrassing! You may say a school boy error.

A line of code in a routine that clears the form I had missed a vital element.

lvFiles.Clear(); which obviously clears everything including the headers, I changed it to lvFiles.Items.Clear(); It was one of the first things I looked for and cannot believe I missed it. :(

how about something like this

string[] fileEntries = Directory.GetFiles(trNode.Tag.ToString(), "*.*",      SearchOption.TopDirectoryOnly);
int fileNo = 1;
int ctr = 0
foreach (string fileName in fileEntries)
{
    FileInfo oFileInfo = new FileInfo(fileName);

    lvFiles.Items.Add(fileNo.ToString());
    lvFiles.Items[ctr].SubItems.Add(oFileInfo.Name);
    lvFiles.Items[ctr].SubItems.Add(oFileInfo.Extension);
    lvFiles.Items[ctr].SubItems.Add(oFileInfo.Length.ToString());
    lvFiles.Items[ctr].SubItems.Add(oFileInfo.CreationTime.ToString());
    fileNo++;
    ctr++;
}

hope it helps

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