Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Windows Forms - populating a ListView with multiple columns 1

Status
Not open for further replies.

theniteowl

Programmer
May 24, 2005
1,975
US
Hi All,
I am using Visual Studio 12 and have created a Windows Forms application with a ListView control on the page.
I am trying to populate the ListView with two columns of data, File Name and Creation Date from a specified folder.
I keep getting errors when I try to use SubItems.Add to add the second columns data.
Error
'System.Windows.Forms.ListView' does not contain a definition for
SubItems' and no extension method 'SubItems' accepting a first argument of type 'System.Windows.Forms.ListView could be found (are you missing a using directive or an assembly reference?)

I have the following references set.
using System;
using System.Collections.Generic;
using System.IO;
using System.Windows.Forms;

Code:
        private void filllistView1()
        {
            DateTime dt;
            listView1.Columns.Add("Filename", -2, HorizontalAlignment.Left);
            listView1.Columns.Add("Date", -2, HorizontalAlignment.Left);
            DirectoryInfo sourceInfo = new DirectoryInfo(StagingPath);
            FileInfo[] sFiles = sourceInfo.GetFiles("*.*");
            foreach (FileInfo file in sFiles)
            {
                dt = file.LastWriteTime;
                listView1.Items.Add(file.Name);
                listView1.SubItems.Add(file.LastWriteTime.ToString("MM/dd/yyyy H:mm:ss"));
            }
        }

I am a newbie with C# and Visual Studio and I might be missing something very simple.
I do get both column headers but cannot populate the second columns values.
Any help would be appreciated.

Thanks.
Trent

At my age I still learn something new every day, but I forget two others.
 
I have found the best method for this is to add the subitems to the ListViewItem before adding them to the ListView. See below. I also added a couple comments for clarity.

Code:
private void filllistView1()
        {
            listView1.Columns.Add("Filename", -2, HorizontalAlignment.Left);
            listView1.Columns.Add("Date", -2, HorizontalAlignment.Left);
            DirectoryInfo sourceInfo = new DirectoryInfo(StagingPath);
            FileInfo[] sFiles = sourceInfo.GetFiles("*.*");                              //Get a list of all files
            foreach (FileInfo file in sFiles)                                            //Loop through each file, get some info and add the file to the ListView
            {
                ListViewItem item = new ListViewItem(file.Name);                         //Create the item variable and assign the file name as the primary value
                item.SubItems.Add(file.LastWriteTime.ToString("MM/dd/yyyy H:mm:ss"));    //Add the last write time to the item as a subitem value
                //item.SubItems.Add(some value);                                         //Repeat as needed for each subitem - could even build an inner loop if needed
                listView1.Items.Add(item);                                               //Add the item variable to the ListView
            }
        }


=======================================
People think it must be fun to be a super genius, but they don't realize how hard it is to put up with all the idiots in the world. (Calvin from Calvin And Hobbs)

Robert L. Johnson III
CCNA, CCDA, MCSA, CNA, Net+, A+, CHDP
C#.NET Programmer
 
I had found another way to populate the fields using the code below:
Code:
        private void filllvFTPFolder()
        {
            int cnt = 0;
            DirectoryInfo sourceInfo = new DirectoryInfo(FTPPath);
            FileInfo[] sFiles = sourceInfo.GetFiles("*.*");
            foreach (FileInfo file in sFiles)
            {
                lvFTPFolder.Items.Add(file.Name);
                lvFTPFolder.Items[cnt].SubItems.Add(file.LastWriteTime.ToString("MM/dd/yyyy H:mm:ss"));
                lvFTPFolder.Items[cnt].SubItems.Add(file.Length.ToString());
                cnt = cnt + 1;
            } 
        }
[\CODE]

This left me with an odd issue though.  Whenever I would move files into the FTPFolder I would issue the command lvFTPFolder.Items.Clear();  to clear all of the content in that ListView and then I would call the above routine to populate it again.  It would populate all of the filenames and sizes but on ZIP files in the folder it would fail to populate the date.
This might be related to the length of the names of the zip files as they are generally 45 - 52 characters.  I renamed one file to .txt to see if it would populate the date but it would not so it is likely in the length of the name rather than the type of file. 

I tried your code above and it works perfectly without the date issue.  I still wonder what the issue with my code is but yours works so I will go that route.

Thanks.
Trent

[COLOR=blue]At my age I still learn something new every day, but I forget two others.[/color]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top