How to Add an item to a specific index in a PagedCollectionView? - c#

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);

Related

How to Add Items From One ListView to Another

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);

How to update ArrayList to match user-ordered TreeView

I have a Gtk TreeView displaying a list of items in an ArrayList. The TreeView is set to reorderable, so the user can drag and drop items in the list to re-order them.
My question is, how do I update the original ArrayList to match the new TreeView ordering once reordering has taken place?
I have managed to come up with a solution - the code below simply prints out all items in the list; hopefully you can see how by doing this you can find the correct index for the items and thus reorder them in the associated ArrayList.
TreeIter iter;
yourNodeView.Model.GetIterFirst (out iter);
for (int i = 0; i < yourNodeView.Model.IterNChildren(); i++)
{
string result = ((TypeOfListItem)sequencerNodeView.Model.GetValue (iter, 0)).ToString();
//Here you would add the TypeOfListItem instance to a new ArrayList, and then after the loop swap out the old ArrayList for the new one.
Console.WriteLine("print the item "+result);
yourNodeView.Model.IterNext(ref iter);
}
The TypeOfListItem could of course be any time. I have written a Class called MyListItem which has parameters such as title and description, so that I can access them with:
string result = ((MyListItem)sequencerNodeView.Model.GetValue (iter, 0)).title;

Prevent listview from adding blank subitem

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 delete many items in listbox?

how to delete from listbox ?
deleting many indexes at the same time
Note that im using list of car types with their details
the objects in the listBox have car type cost rate
Update: If you want to remove all selected items as commented:
foreach (int i in listBox1.SelectedIndices)
listBox1.Items.RemoveAt(i);
If you want to delete all items instead, use Clear:
listBox1.Items.Clear();
if you want to remove at a specific index, use RemoveAt:
listBox1.Items.RemoveAt(0);
or in a loop:
for(int i = 0; i < listBox1.Items.Count; i++)
listBox1.Items.RemoveAt();
if you want to remove a speicific item, use Remove:
Car car = (Car) listBox1.Items[0];
listBox1.Items.Remove(car);
You have to use loop.
Something like this:
List<int> indexesToDelete = new List<int>();
// add items you want to remove to List like this:
indexesToDelete.Add(1);
indexesToDelete.Add(2);
indexesToDelete.Add(4);
// loop will execute code inside inside for all items added to list
foreach (int indexToDelete in indexesToDelete)
{
listbox1.RemoveAt(indexToDelete);
}
Edit: itemsToDelete renamed to indexesToDelete in code.

binding selected items from one list box to another listbox

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();
}

Categories

Resources