views:

592

answers:

5

I've never used the ListView control before and am trying to programmatically insert items at run-time.

I have a column named Title. And once the user has selected a path from the FolderBrowserDialog. I need to be able to grab all the names of the files inside the SelectedPath and add the names of files as items inside the Title column. Can anybody please help me to do this?

Thank you

+3  A: 

Hi, try this code

string[] filePaths = Directory.GetFiles("c:\\MyDir\\");

foreach (string str in filePaths)
{
    ListViewItem lvi = new ListViewItem(str);
    ListView1.Items.Add(lvi)
}

Best Regards, Iordan

IordanTanev
Excellent. Thank you very much Iordan, much appreciated. :o)
baeltazor
A: 

Are you using View.Details mode? It sounds like it, since you mentioned columns. If so, it will look like this:

string[] files = Directory.GetFiles(folderBrowser.SelectedPath);

foreach (string path in paths) {
    ListViewItem row = new ListViewItem();
    row.SubItems.Add(path);
    YourListView.Items.Add(row);
}
John Calsbeek
A: 

One of the options you have is using a binded data grid. However, it depends on the amount of data you want to handle.

This is how is done:

On your view create a DataGrid with a 'title' column and then bind the specific column to a DataField (also title).

When loading the DataGrid you could then create a new DataTable with the column 'title' populated with the files names.


DataTable datatable = new DataTable();
DataColumn titleCol = new DataColumn("title", Type.GetType("System.String"));
datatable.Columns.Add(titleCol);
foreach(name in names)
{
   DataRow newRow = new DataRow();
   newRow["title"] = name;
   /*
    * Add the rows you want into your data table
    */

   datatable.Rows.Add(newRow);
}

Then when rendering your datagrid you simply said:


dagagrid.DataSource = datatable;
datagrid.DataBind();

That will do the trick. But if this solution will make more sense with your data grid has more than one simple column.

Freddy
A: 

If you just starting out with .NET ListView, you can save yourself a lot of hassle by using ObjectListView (an open source wrapper around .NET WinForms ListView). It solves most of the problems and frustrations you are going to encounter.

Most trivially, it automatically handles sorting rows when you click on the headers, something you have to write for yourself on a standard ListView.

Grammarian
+1  A: 

I think the best way of doing this would be to use FileInfo as rather than getting the FilePaths just as strings. This way, you can display more information about the file in the ListView if required (say for instance you set the View to detailed, then you could add groups for FileInfo (Size etc.)).

You would do this by adding groups to the list view then adding the items with SubItems:

DirectoryInfo directoryInfo = new DirectoryInfo(@"C:\myDir");

FileInfo[] files = directoryInfo.GetFiles();

foreach(FileInfo fileInfo in files)
  {
  ListViewItem newItem = new ListViewItem();
  newItem.Text = fileInfo.Name;
  newItem.SubItems.Add(fileInfo.Length); //Must have a group added to the ListView (called File Size in this example)
  listView1.Items.Add(newItem);
  }

Obviously you don't have to use groups and SubItems, this would still work fine without them (just remove the SubItems part).

ThePower
ooh thanks for that! this one's a keeper :D
baeltazor