C# ListView to List<string> - c#

I have a ListView with string items, and i am trying to get these items and store them in List container.
I need the reverse operation of this:
List<string> myList = new List<string> { "Item1", "item2", "item3"};
resultsList.ItemsSource = myList;
So my Question is How to Parse ListView with Items to List?

You need to cast it to IEnumerable<string>
List<string> myList = ((IEnumerable<string>)resultsList.ItemsSource).ToList();

I think you can cast and use ToList to create a list:
myList = resultsList.ItemsSource.Cast<string>().ToList();

Related

C# - If list contains list

I need a condition which is checking for list of string contains all list item of string array.
Example
List<string> list1 = new List<string> { "hello", "it", "is", "an", "example"};
string[] array = { "xa","lo","el","t" };
So, as you can see each items of array exist in list items. It does not matter which item of list contains the which item of array. It should just checking for each item of array is contained by any list's item. In this case I should be true.
array.All(arrayItem => list1.Any(listItem => listItem.Contains(arrayItem)));

How to sort a listBox in C#?

I have typed numbers in a textbox and added them into a listBox. Now I need to order that listbox. This is my try:
int[] array = listBox1.Items.Cast<int>().ToArray<int>();
Array.Sort(array);
listBox1.Items.Clear();
foreach (int item in array)
{
listBox1.Items.Add(item);
}
It throws an 'System.InvalidCastException'. But I can't figure it out HOW to solve it.
You can use a lambda
var array = listBox1.Items.OfType<string>().Select(x => int.Parse(x))
.ToArray();
First, I want to say that it is not a good idea to store data inside a control. Always put your data inside types that can handle them like a List, Dictionary, etc. and then bind that to your listbox object. I guess you are working on windows forms. Then add a property to your form and put all your data in it.
something like this
public partial class Form1 : Form
{
List<string> _items = new List<string>(); // <-- Add this
public Form1()
{
InitializeComponent();
_items.Add("One"); // <-- Add these
_items.Add("Two");
_items.Add("Three");
listBox1.DataSource = _items;
}
public void add()
{
_items.Add("four");
_items.Sort();
}
}
This is as simple as
listBox1.Sorted = true;
UPDATE
var array = new object[listBox1.Items.Count];
listBox1.Items.CopyTo(array, 0);
listBox1.Items.Clear();
var sortedArray = array.Select(n => (object)Convert.ToInt32(n)).OrderBy(n => n).ToArray();
listBox1.Items.AddRange(sortedArray);
ListBox items can cast to string. So, You must cast it to string[], then convert to int[], then sort it and finally add sorted data to ListBox.
string[] strArray = listBox1.Items.Cast<string>().ToArray();
int[] intArray = strArray.Select(x => int.Parse(x)).ToArray();
Array.Sort(intArray);
listBox1.Items.Clear();
foreach (int item in intArray)
{
listBox1.Items.Add(item);
}
I hope this will be useful.

insert All Listbox items into a List

Are there a way to export the items from an Listbox into a List in c#?
i have Listbox1 that Contain some string items and I have This list:
public List<String> MyList = new List<String>();
how to add this items to Mylist?
You could do a casting like this:
public List<String> MyList = listBox1.Items.Cast<string>().ToList();
This way, to change the ListBox ObjectCollection to list of string.
You can use AddRange method:
If you are sure that all elements in Listbox1 are strings:
MyList.AddRange(ListBox1.Items.Cast<string>());
if not:
MyList.AddRange(ListBox1.Items.OfType<string>());

Populating listview with List<List<string>>

I'm coding in C# andI have a listview control with few columns, and I'm wondering, how can I add items to it from List<List<string>>. I am able to add items with
var item3 = new ListViewItem(new[] { table[0][0], table[0][1], table[0][2], table[0][3], table[0][5], table[0][7], table[0][8] });
but that doesn't seem right to me, and neither it would work because the amount of Lists is random.
You can use List<T>.ToArray to convert the list into an array which you then pass to the constructor of ListViewItem. As you are only interested in the first subitem of your table, you can just do it like this:
var item3 = new ListViewItem(table[0].ToArray());
You can use the following code to fill all the List<List<string>> into your ListView:
listView1.Items.AddRange(table.Select(list=>new ListViewItem(list.ToArray())))
.ToArray();
probably you mean like this ?
sample data
item1
List<string> list1 = new List<string>();
list1.Add("item-1.1");
list1.Add("item-1.2");
item2
List<string> list2 = new List<string>();
list2.Add("item-2.1");
list2.Add("item-2.2");
allitem
List<List<string>> listAll = new List<List<string>>();
listAll.Add(list1);
listAll.Add(list2);
string value
string sResult = string.Join(", ", from list in listAll from item in list select item);
list value
List<string> lResult = new List<string>(from list in listAll from item in list select item);
binding
lv.DataSource = lResult;
lv.DataBind();
result
item-1.1 item-1.2 item-2.1 item-2.2

How to select all items in a Listbox and concatenate them in ASP.NET C# Webform?

Right now I have
String myString = listbox1.Text.ToString();
However this only returns only the 1st item, even if I hit ctrl and select all of them.
Thanks for any help
Using an extension method, you can do this:
public static class Extensions
{
public static IEnumerable<ListItem> GetSelectedItems(this ListItemCollection items)
{
return items.OfType<ListItem>().Where(item => item.Selected);
}
}
Usage:
var selected = listbox1.Items.GetSelectedItems();
Now you can take the IEnumerable<ListItem> and convert that to a string array, then finally make it into a single string separated by semicolons, like this:
// Create list to hold the text of each list item
var selectedItemsList = new List<string>();
// Create list to hold the text of each list item
var selectedItemsList = selected.Select(listItem => listItem.Text).ToList();
// Build a string separated by comma
string selectedItemsSeparatedByComma = String.Join(",",
selectedItemsList.ToArray());
You are right, WebForms ListBox doesn't have the SelectedItems property. However, you can do
listBox.Items.OfType<ListItem>().Where(i => i.Selected);
That will give you the items you are looking for.
If you can't use LINQ, just do a foreach over listBox.Items, and do whatever you want when the item is Selected.

Categories

Resources