I'm trying to get listView items into an array..
in listBox
listBox1.SelectedItems
would do the trick.
But it didn't work in listView...
any Ideas???
Do like this,
var myList = new List<string>();
foreach(ListViewItem Item in ListView.SelectedItems)
{
myList.add(Item.Text.ToString());
}
var myArray = myList.ToArray();
Related
Is it possible to declare a ListViewItem array? E.g.
ListViewItem[] arrayItems;
If it's possible, how can I populate the array?
Is it possible to have the array which is not fixed size?
Yes ! It is possible to declare a List as an array.
For Specified length use below:
ListViewItem[] arrayItems = new ListViewItem[5];
arrayItems[0] = new ListViewItem("Text1");
arrayItems[1] = new ListViewItem("Text2");
// so on
For Unspecified length use below:
List<ListViewItem> arrayItems = new List<ListViewItem>();
arrayItems.Add(new ListViewItem("Text1"));
arrayItems.Add(new ListViewItem("Text2"));
// When you want to pass it as array, use arrayItems.ToArray();
Or if you have some list of objects with some text Property:
List<ListViewItem> arrayItems = dataSourceObject.Select(x => new
ListViewItem(x.TextProperty)).ToList();
It seems that you're looking for List<ListViewItem>, not for array (ListViewItem[]):
List<ListViewItem> myItems = new List<ListViewItem>();
// Just add items when required;
// have a look at Remove, RemoveAt, Clear as well
myItems.Add(new ListViewItem("Text 1"));
// When you want read/write the item do as if you have an array
ListViewItem myItem = myItems[0];
You can use Linq to obtain items from existing ListView:
myItems = listView1.Items
.OfType<ListViewItem>()
.ToList();
or append existing list:
List<ListViewItem> myItems = new List<ListViewItem>();
...
myItems.AddRange(listView1.Items.OfType<ListViewItem>());
Hi all i have a list string like
List<string> numbers=new List<string>();
now i want to convert it on observable collection i have converted it successfully like
ObservableCollection<string> myCollection = new ObservableCollection<string>(numbers);
but when i am deleting a item from list box like
myCollection.Remove(listBox1.SelectedItem.ToString());
listBox1.ItemsSource = myCollection;
the above code is deleted all the item in list box but i want to delete specific item in list box.
Try this
Initialize collection and lsitbox
List<string> numbers=new List<string>();
//numbers.Add("test"); //populate list
ObservableCollection<string> myCollection = new ObservableCollection<string>(numbers);
listBox1.ItemsSource = myCollection;
now use the below code to remove the selected item from the list
var selectedItem =listbox1.SelectedItem as string;
if(myCollection.Contains(selectedItem)
{
myCollection.Remove(selectedItem);
}
Instead of binding ObservableCollection you can directly bind List<string> to your listBox1.ItemsSource. See this example to bind List<string>
windows phone data binding listpicker to List of Strings
To remove items from list try this
listBox1.Items.Remove(listBox1.SelectedItem);
Answer_:
numbers.RemoveAt(listBox1.SelectedIndex);
listBox1.ItemsSource = null;
listBox1.ItemsSource = numbers;
I got 3 List (L1,L2,L3) and i want to show them on ListView.But i cant add or convert, List data's to ListViewItem.Something like this
List<string> for96 = new List<string>();
List<string> for97 = new List<string>();
List<string> for98 = new List<string>();
ListView lv1=new ListView();
ListViewItem lvitem = new ListViewItem();
lvitem.Text=for96;
lvitem.items.add(97);
lvitem.items.add(98);
lv1.items.add(lvitem);
You could take a variety of routes with this, but here's one possibility.
Join the three lists of strings together and then set that as the data source for your ListView:
var combinedLists = for96.Union(for97).Union(for98).ToList();
listView.ItemsSource = combinedLists;
Is there any easy way to sort listbox items by DateTime values?
In my listbox1 I have items formatted like this: "2013.01.08 19:29:52" so it's just someDateTimeValue.ToString()
if you have 2 ListBoxes you could do something like the following
ArrayList arList = new ArrayList();
foreach (object obj in listBox1.Items)
{
arList.Add(obj);
}
arList.Sort();
listBox2.Items.Clear();
foreach(object obj in arList)
{
listBox2.Items.Add(obj);
}
Try this
List<ListItem> myList = new List<ListItem>(ListBox1.Items.Cast<ListItem>());
myList = myList.OrderByDescending(li => li.Value).ToList<ListItem>();
ListBox1.Items.Clear();
ListBox1.Items.AddRange(myList.ToArray<ListItem>());
try to sort a list of dates and after that put in the listbox.
dateList.Sort();
var items = new SelectList(dateList);
I'm working on one of my first projects. I have a listbox where I select multiple values and I would like to add each selection (selectedItem.Text) to a list of strings.
so far what I was working on is something like ..
selectedItem = new List<string>();
var value = lstpdfList.SelectedItem.Text;
for (int i = 0; i < lstpdfList.SelectedValue.Count(); i++)
{
selectedItem.Add(value);
}
I would really appreciate any advice.
Iterate each item from ListBox.Items collection
foreach (ListItem item in ListBox1.Items)
{
if (item.Selected)
{
selectedItem.Add(item.Text); // selectedImte.Add(item.Value);
}
}
There is SelectedItems property of ListBox, try to iterate throught it. For example, if there is strings in your ListBox, then your code might look like this:
selectedItem = new List<string>();
foreach (string value in lstpdfList.SelectedValues)
selectedItem.Add(value);
You can just cast them to strings:
var selectedItems = listBox1.SelectedItems
.Cast<string>()
.ToList();
If you have populated your ListBox with something other than just strings, just cast to whichever type you need, like so:
var selectedItems = listBox1.SelectedItems
.Cast<WhateverYourTypeIs>()
.Select(item => item.ToString())
.ToList();