Reputation: 11662
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
Upvotes: 1
Views: 7062
Reputation: 1
int i = 0;
//i = LVPurchase.Items.Count + 1;
LVPurchase.Items.Add(cmbService.SelectedValue.ToString(), i);
LVPurchase.Items[LVPurchase.Items.Count - 1].SubItems.Add(cmbService.Text);
LVPurchase.Items[LVPurchase.Items.Count - 1].SubItems.Add(cmbItem.SelectedValue.ToString());
LVPurchase.Items[LVPurchase.Items.Count - 1].SubItems.Add(cmbItem.Text);
LVPurchase.Items[LVPurchase.Items.Count - 1].SubItems.Add(txtPurchaseQty.Text);
LVPurchase.Items[LVPurchase.Items.Count - 1].SubItems.Add(txtUnitCostUSD.Text);
LVPurchase.Items[LVPurchase.Items.Count - 1].SubItems.Add(txtConvRate.Text);
LVPurchase.Items[LVPurchase.Items.Count - 1].SubItems.Add(txtUnitCostBDT.Text);
SubTotUSD = Convert.ToDouble(txtPurchaseQty.Text) * Convert.ToDouble(txtUnitCostUSD.Text);
txtSubTotUSD.Text = SubTotUSD.ToString();
SubTotBDT = Convert.ToDouble(txtPurchaseQty.Text) * Convert.ToDouble(txtUnitCostBDT.Text);
txtSubTotBDT.Text = SubTotBDT.ToString();
LVPurchase.Items[LVPurchase.Items.Count - 1].SubItems.Add(txtSubTotUSD.Text);
LVPurchase.Items[LVPurchase.Items.Count - 1].SubItems.Add(txtSubTotBDT.Text);
LVPurchase.Items[LVPurchase.Items.Count - 1].SubItems.Add(txtBatch.Text);
LVPurchase.Items[LVPurchase.Items.Count - 1].SubItems.Add(dtpExpiryDate.Value.ToString());
TotalUSD = TotalUSD + SubTotUSD;
txtTotalUSD.Text = TotalUSD.ToString();
TotalBDT = TotalBDT + SubTotBDT;
txtTotalBDT.Text = TotalBDT.ToString();
Upvotes: -1
Reputation: 6250
Try this code:
string[] filePaths = Directory.GetFiles("c:\\MyDir\\");
foreach (string str in filePaths)
{
ListViewItem lvi = new ListViewItem(str);
ListView1.Items.Add(lvi)
}
Upvotes: 3
Reputation: 18820
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).
Upvotes: 3
Reputation: 6882
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.
Upvotes: 0
Reputation: 3304
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.
Upvotes: 0
Reputation: 36547
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);
}
Upvotes: 0