×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Windows Forms - populating a ListView with multiple columns

Windows Forms - populating a ListView with multiple columns

Windows Forms - populating a ListView with multiple columns

(OP)
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.

RE: Windows Forms - populating a ListView with multiple columns

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

RE: Windows Forms - populating a ListView with multiple columns

(OP)
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

At my age I still learn something new every day, but I forget two others.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close