I am trying to parse the data in the first column of one ListView called raw and then if the data is correct, add that item to a second ListView called result.
However, when I go to run my program I get
the error:
"Cannot add or insert the item 'Collected' in more than one place.".
My Code
ListView result = new ListView();
for (int i = 0; i < raw.Items.Count; i++)
{
if (raw.Items[i].SubItems[0].Text.ToUpper() == "COLLECTED")
{
MessageBox.Show("confirm");
result.Items.Add(raw.Items[i]); // generating erros
}
}
printUsingLView(result);
clone original item:
result.Items.Add((ListViewItem)raw.Items[i].Clone());
or, if you want to make some adjustments
ListViewItem newItem = new ListViewItem();
newItem.Text = raw.Items[i].Text;
//enter other properties here and then add it to new listView
result.Items.Add(newItem);
Related
I am trying to have 2 listviews that in the end will act as mirrors of each other. In the first listview I am able to populate it from a DB with no issues. I have another list view that when a row is checked in the first listview(listVariants) the user can move the entire row to the second listview (addFCList). With the code below it is working, sort of. The issue I am having is when another item is checked from listVariants, the way it is added to addFCList is inline and it just wraps. each item and subitem is being added to the listview as an individual item.
I have both listview cloumns setup the same. My question is how to I get the second listview(addFCList) to be the same as the the first listview(listVariants)? Am I missing something obvious...
EDIT I have solved this but will leave it here to help someone out in the future.
for (int i = 0; i < listVariants.Items.Count; i++)
{
if (listVariants.Items[i].Checked)
{
string UNI = listVariants.Items[i].SubItems[9].Text;
bool nameInsert = true;
for (int t = 0; t < addFCList.Items.Count; t++)
{
if (addFCList.Items[t].SubItems[9].Text == UNI)
{
nameInsert = false;
break;
}
}
if (nameInsert)
{
addFCList.Items.Add((ListViewItem)listVariants.Items[i].Clone());
}
}
}
Thanks for any help and suggestions.
listView1.Columns.AddRange((from ColumnHeader item in listView2.Columns
select (ColumnHeader)item.Clone()).ToArray());
I have a listview in which the columns are dynamically created depending on the number of available subjects. When I create a new ListViewItem it automatically adds a blank subitem to the listview. Since the columns are dynamically created, the subitems are displaced. This is how I have done it.
foreach(int id in stud)
{
ListViewItem gtb = new ListViewItem();
foreach(int sname in subj)
{
gtb.SubItems.Add(value1);
gtb.SubItems.Add(value2);
gtb.SubItems.Add(value3);
}
gtb.SubItems.Add(value4);
listView1.Items.AddRange(new ListViewItem[] { gtb });
}
I know that adding the value1 to new ListViewItem(value1) would fix this but i have another subitem which is outside of the foreach loop. How can I fix this?
Add an 'if' statement that will check the length of each value then if the length is greater than zero you add the item to 'gtb'.
Create a String[], use it to collect your subitems, then create a new ListViewItem using that:
String[] subItems = new String[subj.Count + 1]; //add however many extras you need in place of 1
for (int j = 0; j < subj.Count; j++)
subItems[j] = subj[j];
//add whatever else to the array you want...
listView1.Items.Add(new ListViewItem(subItems));
I tried manually adding a new column which has a 0 width and it works just fine.
How to Add an item to a specific index in a PagedCollectionView?
We can add any item to a paged collection view but the problem is it adds it to the last..
I want to add it at any given index..!
Can any one pls help.
Thanks in advance.
Not possible to add item at given position or index in PagedCollectionView... To add item at given index you need to add item to underlying source. Check below example:
List<String> itemList = new List<String>();
// Generate some items to add to the list.
for (int i = 1; i <= 33; i++)
{
System.Text.StringBuilder sb = new System.Text.StringBuilder("Item ");
sb.Append(i.ToString());
itemList.Add(sb.ToString());
}
// Add at given index
itemList.Insert(10, "Extra item");
// Wrap the itemList in a PagedCollectionView for paging functionality
var itemListView = new PagedCollectionView(itemList);
I have two list boxes in my form. I bound some data in the first list box. Now I have to select the items from the first lis tbox and should bind those selected items to the second list box when I press the button which is between these two list boxes. I am able to bind a single item at a time but I am having problem binding multiple selected items.
I am using the following code:
Hashtable ht = new Hashtable();
ht.Add(lbCATallSubcat.SelectedValue.ToString(),lbCATallSubcat.Text.ToString());
int i = 0;
foreach (string ent in ht.Values) {
string[] name = new string[lbCATallSubcat.Items.Count];
for (i = 0; i < lbCATallSubcat.SelectedItems.Count; i++) {
name[i] = lbCATallSubcat.Text;
this.lbCATSelectedSubcat.Items.Add(name[i]);
}
lbCATSelectedSubcat.DisplayMember = ht.Values.ToString();
lbCATSelectedSubcat.ValueMember = ht.Keys.ToString();
}
I've just started to use ListView in C#.net.
I got to know how to add items and subitems. Going through the listview I wanted to fetch all the data from a whole column with multiple rows.
I want to know how to do this.
I found this code to list a specific selected data from a row:
ListView.SelectedIndexCollection sel = listView1.SelectedIndices;
if (sel.Count == 1)
{
ListViewItem selItem = listView1.Items[sel[0]];
MessageBox.Show(selItem.SubItems[2].Text);
}
That was helpful but i want to list all the items in a row, may be i want to add all the column items in array?
private string[] GetListViewItemColumns(ListViewItem item) {
var columns = new string[item.SubItems.Count];
for (int column = 0; column < columns.Length; column++) {
columns[column] = item.SubItems[column].Text;
}
return columns;
}
I would recommend some caution against doing this. A ListView is really meant to display information, it is not a great collection class. Getting the data out of it is slow and crummy, it can only store strings. Keep the data in your program in its original form, maybe a List<Foo>. Now it is simple and fast.
foreach (ListViewItem item in listView1.Items) {
// Do something with item
}
you could do this by
foreach(ListViewItem item in listView1.Items)
{
foreach(var subtem in item.SubItems)
{
// Do what ever you want to do with the items.
}
}