0

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?

1
  • What is your aspx ? is there some control in the listview, may be a label, for which you want text ? Commented Apr 19, 2012 at 19:03

4 Answers 4

3

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.

Sign up to request clarification or add additional context in comments.

Comments

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

isn't it that simple.

Comments

1
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

2 Comments

+1 for using StringBuilder.
0
   string.Join(" ", ListView1.Items.Cast<ListItem>().Select(i => i.Text).ToArray());

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.