Fred
Fred

Reputation: 5808

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:

enter image description here

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.

enter image description here

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!

Upvotes: 5

Views: 5768

Answers (2)

Fred
Fred

Reputation: 5808

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. :(

Upvotes: 21

Angelo
Angelo

Reputation: 335

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

Upvotes: 1

Related Questions