I have an ArrayList that contains some HashTables , and I have a ListView that contains some items.
What I need is to match the array list with the list items.
If an item in the list view and not in the array list, then it should be removed from the list view.
If not in either the list view or the array list, then add it to list view.
This is some code:
ArrayList online_list = users;
foreach (Hashtable i in online_list)
{
ListViewItem item = new ListViewItem();
item.Text = (string)i["u_name"];
item.Name = (string)i["id"];
item.ImageIndex = 0;
lstvUsers.Items.Add(item);
}
This code adds all array list items to the list view without the check!
You could first clear all items of the ListView, then add all in the ArrayList:
lstvUsers.Clear();
foreach (Hashtable i in online_list)
{
ListViewItem item = new ListViewItem();
item.Text = (string)i["u_name"];
item.Name = (string)i["id"];
item.ImageIndex = 0;
lstvUsers.Items.Add(item);
}
You should use ListView.Clear instead of ListView.Items.Clear: https://stackoverflow.com/a/10170049/284240
Edit: Since you're adamant that you only want to remove items from the ListView that are not in the ArrayList and add items that are not in the ListView but in the ArrayList . That approach is significantly less efficient since you need to compare each item in the ListView with each item in the ArrayList (and vice-versa) instead of comparing nothing.
For example:
var addItems = online_list
.Cast<Hashtable>()
.Where(ht => !lstvUsers.Items.ContainsKey((string)ht["id"]));
var removeItems = lstvUsers.Items
.Cast<ListViewItem>()
.Where(lvi => !online_list.Cast<Hashtable>().Any(ht => (string)ht["id"] ==lvi.Name));
foreach (var removeItem in removeItems)
{
lstvUsers.Items.Remove(removeItem);
}
foreach (var addHashTable in addItems)
{
ListViewItem item = new ListViewItem();
item.Text = (string)addHashTable["u_name"];
item.Name = (string)addHashTable["id"];
lstvUsers.Items.Add(item);
}
Related
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());
}
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
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.
Page 1: (sending listbox items into a list and saving the list as a session variable)
List<string> list = new List<string>();
foreach(ListItem item in ListBox1.Items)
{
list.Add(item.ToString());
}
Session["temp"] = list;
Page 2: (copying the session variable into a list and assigning the list to a listbox in this page)
List<string> list = new List<string>();
list = (List<string>)Session["JobRole"];
foreach (ListItem item in list)
{
ListBox2.Items.Add(item.ToString());
}
On doing so, this is the error i m getting : Cannot convert type 'string' to 'System.Web.UI.WebControls.ListItem'
can anyone pls help me with this ??
Thanks
Don't you mean
foreach (string item in list)
{
ListBox2.Items.Add(item);
}
... list is a list of strings after all.
That's because you have defined list as List. If you simply want to add the string value into the listbox you can do:
List<string> list = new List<string>();
list = (List<string>)Session["JobRole"];
foreach (string item in list)
{
ListBox2.Items.Add(item);
}
Also you should check that Session["JobRole"] is not null.
I am trying to get a list of all elements from ListBox if no or one of items is selected or list of selected items if more than 1 are selected. I have written such a code but it doesn't compile :
ListBox.ObjectCollection listBoXElemetsCollection;
//loading of all/selected XMLs to the XPathDocList
if (listBoxXmlFilesReference.SelectedIndices.Count < 2)
{
listBoXElemetsCollection = new ListBox.ObjectCollection(listBoxXmlFilesReference);
}
else
{
listBoXElemetsCollection = new ListBox.SelectedObjectCollection(listBoxXmlFilesReference);
}
So for this piece of code to work I would need to use something like ListBox.SelectedObjectCollection listBoxSelectedElementsCollection; which I do not want because I would like to use it in such an foreach:
foreach (string fileName in listBoXElemetsCollection)
{
//...
}
I'd simply this a bit and not mess with the ListBox ObjectCollections if you don't need to. Since you want to iterate items on your ListBox as strings, why not use a List and load the list how you show:
List<string> listItems;
if (listBoxXmlFilesReference.SelectedIndices.Count < 2) {
listItems = listBoxXmlFilesReference.Items.Cast<string>().ToList();
} else {
listItems = listBoxXmlFilesReference.SelectedItems.Cast<string>().ToList();
}
foreach (string filename in listItems) {
// ..
}
You need to convert SelectedObjectCollection to an array of object[].
ListBox.SelectedObjectCollection sel = new
ListBox.SelectedObjectCollection(listBoxXmlFilesReference);
ListBox.ObjectCollection col = new
ListBox.ObjectCollection(listBoxXmlFilesReference,
sel.OfType<object>().ToArray());
I can see what you're trying to do and it doesnt compile because the type ListBox.ObjectCollection is not the same as ListBox.SelectedObjectCollection - even though in your case they are lists that contain strings the classes themselves are different hence the compile error.
Assuming your items are strings in the listbox you could do:
var items = listBoXElemetsCollection.Items.OfType<string>();
if (listBoXElemetsCollection .SelectedIndices.Count >= 2)
items = listBoXElemetsCollection.SelectedItems.OfType<string>();
foreach(var item in items)
//do stuff