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;
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
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
CODE
=======================================
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
[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.