How to add item from ListView to string? - c#

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?

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.

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

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

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

Related

FindItemWithText in WPF

I want to find a text in list view then remove it I could achieve it with winforms but looks difficult with WPF here's my code :
listView1.FindItemWithText("my text", true, 0).Remove();
thanks in advance
The best way will be to write such function. Just iterate ListViewItems and delete it.
var str = "my text";
foreach (ListViewItem item in listView1.Items)
{
if (item.Content.Equals(str))
{
//... do your stuff
}
}
You could use linq
ListViewItem item = listView1.Items.Where(x => x.Content.ToString() == str).ToList()[0];

How to retrieve selected values for selected items in a ListBox?

I'm populating a ListBox in a WinForms application, this way:
listBoxUsers.DataSource = ctx.Users.ToList();
listBoxUsers.DisplayMember = "Name";
listBoxUsers.ValueMember = "Id";
how to retrieve the selected Ids when I'm setting the SelectionMode to MultiSimple
I want to do a foreach loop on them, like this:
foreach(var itemId in listBoxUsers.SelectedValues)//unfortunately not exist
{
int id = int.Parse(itemId);
// . . .
}
Since you know the type of items, you can use such code:
var selectedValues = listBox1.SelectedItems.Cast<User>().Select(x=>x.Id).ToList();
Side Note: The ListBox control lacks a GetItemValue method. A method which should work like GetItemText, but for getting values. In the linked post I shared an extension method to get the value from an item. Using that extension method you can get selected values independent from type of items:
var selectedValues = listBox1.SelectedItems.Cast<object>()
.Select(x => listBox1.GetItemValue(x)).ToList();
If for some reason you are interested to have a text representation for selected values:
var txt = string.Join(",", selectedValues);
Have you tried with the SelectedItems property?
foreach (var item in listBoxUsers.SelectedItems)
{
}
try this:
foreach (DataRowView item in listBoxUsers.SelectedItems)
{
int id=int.parse(item[0].ToString());
}

ListBox multiple Selection get all selected values

I'm having a problem since a while now an just can't find any solution that works for me. I have a ListBox which is filled up with a DataTable like
listbox.DataSource = table;
listbox.Displaymember = "Name";
listbox.ValueMember = "ID";
If I now select an item in my listbox I can get it out like:
listbox.SelectedValue.toString();
My Problem:
What can I do if I would like to have ALL selected Values from a ListBox where multiple selection is enabled and save them all in an array or something like that?!
I can't use SelectedItems cause that is not giving me the information I need.
Try this:
var lst = listBox1.SelectedItems.Cast<DataRowView>();
foreach (var item in lst)
{
MessageBox.Show(item.Row[0].ToString());// Or Row[1]...
}
Or if you want only iterate over the selected items you can use SelectedIndices property:
foreach (int i in listbox.SelectedIndices)
{
// listbox.Items[i].ToString() ...
}
Or:
foreach (var item in listbox.SelectedItems)
{
MessageBox.Show(item.ToString());
}

add a ListViewItem to two or more ListView Items Collection?

List<Profile> listProf = new List<Profile>();
...
...
foreach (Profile p in listProf)
{
ListViewItem Item = new ListViewItem();
Item.Text = p.Name;
Item.Tag = p;
ListView1.Items.Add(Item);
ListView2.Items.Add(Item);
}
In this instance how would I get this Item into both ListViews? I just get the error that I need to clone it. how can I do this? I'm not quite sure even the reason why a ListView would want to be so picky either.
How can I add an item to more than one ListViewCollection?
Try this:
foreach(var p in listProf)
{
var item = new ListViewItem{Text = p.Name, Tag = p};
ListView1.Items.Add(item);
ListView2.Items.Add((ListViewItem)item.Clone());
}
The reason the ListView is so "picky" is because the IsSelected flag in particular is kept at the ListViewItem level... so if you added it to multiple ListViews then selected in one would be selected in all.
Fortunately ListViewItem has a .Clone() method.

How to clear all Groups And Items in Listview Control

How to clear all Groups And all Items in that Groups in Listview Control
Probably ListView.Clear() will work for you. And to clear groups in ListView call ListViewGroupCollection.Clear()
if you are filling your group items with datasource then you could try something like this..
How about
DataSource = null;
DataBind();
If you want to remove only the listViewItems which are grouped, you can do following:
foreach (var group in listView.Groups)
{
var listViewItemsToDelete = listView.Items.Cast<ListViewItem>().Where(item => Equals(item.Group, group));
foreach (var itemToRemove in listViewItemsToDelete)
{
listView.Items.Remove(itemToRemove);
}
}
listView.Groups.Clear();

Categories

Resources