Bramble
Bramble

Reputation: 1395

How to add item from ListView to string?

Im trying to get all the items from a listview into a string like so:

foreach(ListViewItem item in ListView1.Items)
{
     thisstring += item...?
}

item.Text is not a property of item...can seem to figure this out. Any suggestions?

Upvotes: 0

Views: 1054

Answers (4)

Habib
Habib

Reputation: 223247

StringBuilder sb = new StringBuilder();
foreach(ListViewItem item in ListView1.Items)
{
     sb.Append(item.Text);
     sb.Append(',');
}

Console.WriteLine(sb.ToString().TrimEnd(','));

EDIT: As Tim and Guest said, there is not Text property for ListViewItem in ASP.Net, Windows Forms has ListViewItem and it has the text property. ASP.Net ListView does not have Text property

Upvotes: 1

Tim Schmelter
Tim Schmelter

Reputation: 460108

You could use LINQ to select all items' Text.

var allItems = ListView1.Items.Cast<ListItem>().Select(i => i.Text);
var allItemText = String.Join(",", allItems);

Note that you need to add the System.LINQ namespace.

Edit: I've read ListBox, a ListView does not have a Text property and i'm not sure what text you actually want to concat.

Upvotes: 3

Imran Rizvi
Imran Rizvi

Reputation: 7438

foreach(ListViewItem item in ListView1.Items)
{
     thisstring += item.Text+",";
}
    thisstring.TrimEnd(',');

isn't it that simple.

Upvotes: 1

Robin Maben
Robin Maben

Reputation: 23054

   string.Join(" ", ListView1.Items.Cast<ListItem>().Select(i => i.Text).ToArray());

Upvotes: 0

Related Questions