Remove selected rows from multi-column listView - c#

I have a listview with two columns and I'm using a context menu to allow users to remove selected rows. To remove the selected rows, I've tried with the following code but it doesn't work:
private void toolStripMenuItem1_Click(object sender, EventArgs e)
{
listView1.SelectedItems.Clear();
}
I suspect this is because the listview has two columns, but I can't figure out a solution to remove selected rows. Removing all rows works with: listView1.Items.Clear();.

The latest example of BeefTurkey looks correct, but he should decrement the variable i after removing a selected item:
for (int i = 0; i < listView1.Items.Count; i++ )
{
if (listView1.Items[i].Selected)
{
listView1.Items[i].Remove();
i--;
}
}
The index of items larger as i is decremented by 1 after the removal. So you should reposition i to match the next not tested item.

while (listBox1.SelectedItems.Count > 0)
{
listBox1.Items.Remove(listBox1.SelectedItem);
}

foreach(ListViewItem lvItem in lvDocument.SelectedItems)
{
lvDocument.Items.Remove(lvItem);
}

This seems to work:
for (int i = 0; i < listView1.Items.Count; i++ )
{
if (listView1.Items[i].Selected)
{
listView1.Items[i].SubItems.Clear();
}
}
Is there any way to remove items and re-order the listView so that there are no empty rows in the middle of other rows?

This seems to be a better solution:
for (int i = 0; i < listView1.Items.Count; i++ )
{
if (listView1.Items[i].Selected)
{
listView1.Items[i].Remove();
}
}

What you can do:
foreach (ListViewItem Item in LstvClients.Items)
{
if (item.Selected)
{
LstvClients.Items.Remove(Item);
}
}
(Yours is better, item.Remove())

I have been using something slightly different then the others to remove all the selected items from a ListView control:
foreach (ListViewItem listViewItem in listView1.SelectedItems)
{
listView1.Items.Remove(listViewItem);
}
I'm not sure how this would match up performance-wise to the other posted methods on large lists, but I think it is a little cleaner looking in cases where that isn't an issue.

This is the correct way to remove all selected items. The method is to always access fist selected item with an index 0 and loop until no more selected items left. You cannot refer to other items inside collection with an absolute index safely since indexes will change as soon as you delete one of the items.
while( listView1.SelectedItems.Count > 0)
{
listView1.Items.Remove(lvFiles.SelectedItems[0]);
}

do
{
this.listView1.CheckedItems[0].Remove();
} while (this.listView1.CheckedItems.Count > 0);
This works better

Related

removing multiple list items from a listview

So I have created a button that when pressed it deletes the multiple items in the listview. However, I can only seem to make it delete one list item at a time. I know this is badly worded so my code should help explain what I am doing:
private void button2_Click(object sender, EventArgs e) //remove
{
try
{
foreach ( ListViewItem eachItem in listView1.SelectedItems)
{
listView1.Items.Remove(eachItem);
}
task.RemoveAt(listView1.SelectedItems[0].Index); // Remove task from the list "task"
I am trying to make it delete all selected items from my listview as well as my list named "task" However, the line of code above only allows me to delete one selected item from the list at a time.
Any help? Thanks
Try This,
for (int i = task.SelectedItems.Count - 1; i >= 0; i++)
{
if (task.Items[i].Selected)
{
task.Items.Remove(listView2.Items[i]);
}
}
I'm going to assume that your list of tasks is equal, in item count, to your ListView, and that the object in task[0] is the same as ListView.Items[0]. If that is true, remove the item from the task first then remove the item from the ListView
// Removing items from the bottom of the selected items and working your way up
for (int i = listView1.SelectedIndices.Count - 1; i >= 0; i--)
{
task.RemoveAt(listView1.SelectedIndices[i]);
listView1.Items.RemoveAt(listView1.SelectedIndices[i];
}
foreach (ListViewItem eachItem in listView1.Items)
{
listView1.Items.Remove(eachItem);
}
you can only remove one item per line,
you can use the ".ToArray" extension method, if you are enumerating over a changing enumerable.
you should also use the BeginUpdate / EndUpdate function, to speed it up
https://msdn.microsoft.com/library/system.windows.forms.listview.beginupdate%28v=vs.110%29.aspx

Using Loop To Select/Remove Options From Listbox

So basically I am using a FOR loop to add and remove options from a listbox.
It functions correctly when selected 1 option (from either remove or select) and it functions correctly when I select two separate options (For example, item[0] and item[4]).
However, I am having trouble when I try to select all options or when I select two items that are side by side ([2],[3].. etc)
Here is my loop for the select function:
protected void btnSelect_Click(object sender, EventArgs e)
{
for (int intCounter = 0; intCounter < lbSnacks.Items.Count; intCounter++)
{
if (lbSnacks.Items[intCounter].Selected) // if the snack is selected
{ // add the listitem to the lbSelected listbox
lbSelected.Items.Add(lbSnacks.Items[intCounter]);
}
}
for (int intCounter = 0; intCounter < lbSnacks.Items.Count; intCounter++)
{
if (lbSnacks.Items[intCounter].Selected) // if the snack is selected
{ // add the listitem to the lbSelected listbox
lbSnacks.Items.Remove(lbSnacks.Items[intCounter]);
}
}
}
The error is basically taking the item and putting it into the "selected" listbox but it is leaving behind one of the two options in the original "snacks" box.
Any ideas?
The problem is, when you remove an item, all of the other items shift downwards - which means the next loop iteration (since it increments your index) "skips" one item.
There are various ways to handle this. The simplest is to just loop backwards:
for (int intCounter = lbSnacks.Items.Count-1; intCounter >= 0; intCounter--)
{
if (lbSnacks.Items[intCounter].Selected) // if the snack is selected
{ // add the listitem to the lbSelected listbox
lbSelected.Items.Add(lbSnacks.Items[intCounter]);
lbSnacks.Items.Remove(lbSnacks.Items[intCounter]);
}
}
This way, when the items "shift", it doesn't matter, since you've already dealt with those items.

How to remove more items from combobox?

I am trying to remove more items from a combobox but the application is only removing one item at a time.
The combobox has a list of email addresses. I want to remove empty items (""), and those that don't have # inside of text.
Code below only removes one item at a time.
for (int i = 0; i < cmbTo.Items.Count; i++)
{
string st = cmbTo.Items[i].ToString();
if (st == "" || st.IndexOf("#") == -1)
{
cmbTo.Items.RemoveAt(i);
}
}
How can I rewrite this?
Hint: Think about what happens to the i variable when you remove an item
...
When you RemoveAt an item, the item is removed, and every subsequent item moves up one index. Your loop then hits the bottom, where it goes back to the top, increments i, and moves on.
Result? You just skipped an item. If this is the last item in the list, then the loop exists.
Instead, manually decrement i to offset your removal, so that everything works:
for (int i = 0; i < cmbTo.Items.Count; i++)
{
string st = cmbTo.Items[i].ToString();
if (st == "" || st.IndexOf("#") == -1)
{
cmbTo.Items.RemoveAt(i);
i--;
}
}
Your code doesn't work because the moment you remove an item from the collection, the Count() decreases and the for loop exits before going through all the list of items.
You need to first create a list of elements to remove (put them in a temp list) and then iterate through the newly created list calling cmbTo.Items.Remove(currentElement);
When you remove an item from a combobox, the indices of the following items change and your item count will change. Could that account for the behavior you're seeing?
Just do the removal in the opposite direction (i.e. from the end to the front), and you won't need to worry about adjusting i1 when the item is removed:
var items = cmbTo.Items;
int i = items.Count;
while (i > 0) {
--i;
string st = items[i].ToString();
if (st == "" || st.IndexOf("#") < 0)
items.RemoveAt(i);
}
1 Which you currently don't do, so some items that should potentially be removed are skipped, which causes your problem.

how to change an item index in listview?

I have a listView and two buttons (UP , DOWN) and i want to move the selected item up or down.
I've thought about swapping between the selected item and the upper one.. but the code i tried .. doesn't make sense because index is readonly.
also mines or sum doesn't owrk .. i can't mess with index at all.
private void btnDown_Click(object sender, EventArgs e)
{
listView1.SelectedItems[0].Index--; // It's ReadOnly.
}
So .. how do i let the user the ability to change a ListViewItem index like how VB let us to change these item index [like in the pic]
thanks in advance ...
You have to remove the selected item first, then re-add it at the new position.
E.g to move the item up one position:
var currentIndex = listView1.SelectedItems[0].Index;
var item = listView1.Items[index];
if (currentIndex > 0)
{
listView1.Items.RemoveAt(currentIndex);
listView1.Items.Insert(currentIndex-1, item);
}
Following is an improvement to M4N answer to handle the re order of an item in the top of the list and make it in the bottom of the list
int currentIndex = listView1.SelectedItems[0].Index;
ListViewItem item = listView1.Items[currentIndex];
if (currentIndex > 0)
{
listView1.Items.RemoveAt(currentIndex);
listView1.Items.Insert(currentIndex - 1, item);
}
else
{
/*If the item is the top item make it the last*/
listView1.Items.RemoveAt(currentIndex);
listView1.Items.Insert(listView1.Items.Count, item);
}
In case of observable collections you are also able to call: .Move(currentindex, newindex);
MSDN

WPF DataGrid Remove SelectedItems

Recently I've been working on a project which imports data programmicaly into a WPF DataGrid.
I'm almost done with the project but the thing that I left out was a button to remove selected cells and this is where I'm stuck!
I wrote this code using my basic knowledge of DataGrids:
var grid = dataGrid1;
if (grid.SelectedIndex >= 0)
{
for (int i = 0; i <= grid.SelectedItems.Count; i++)
{
grid.Items.Remove(grid.SelectedItems[i]);
};
}
Works fine on removing only the item selected just like CurrentItem but it doesn't remove anymore than 2 selected items!
The DataGrid I have should at least contain a minimum of 100 items. I've added a remove all option but this is also necessary.
I'll be thankful if anyone gives me the solution.
By removing selected item you are changing SelectedItems collection. You should copy it first and then start removing.
This also worked well for me.
while (dataGrid1.SelectedItems.Count > 0){
dataGrid1_item_source.Rows.RemoveAt(dataGrid1.SelectedIndex);
}
The mistake you are doing here you are removing items during loop whaich is messing with loop count so make a copy grid and remove selecteditem from it and then equlize it by the orignal one..
Check this out
var grid = dataGrid1;
var mygrid = dataGrid1
if (grid.SelectedIndex >= 0)
{
for (int i = 0; i <= grid.SelectedItems.Count; i++)
{
mygrid .Items.Remove(grid.SelectedItems[i]);
};
}
grid = mygrid;
This worked for me...
while (dataGrid1.SelectedItems.Count > 0){
dataGrid1_item_source.Rows.RemoveAt(dataGrid1.SelectedIndex);
}
This worked for me...
if (DataGrid1.SelectedItem != null)
{
((DataRowView)(DataGrid1.SelectedItem)).Row.Delete();
}
A while loop using the SelectedItem instead of the SelectedIndex
while (dataGrid1.SelectedItems.Count > 0){
if (dataGrid1.SelectedItem == CollectionView.NewItemPlaceholder)
dataGrid1.SelectedItems.Remove(grid.SelectedItem);
else
dataGrid1.Items.Remove(dataGrid1.SelectedItem);
}
I have stack with the same problem as an author. And found quite beautiful (I think) solution.
And so the main problem is that the SelectedItems dynamic, and when you delete one row, it is recalculated again.
And so my code looks like this:
for (int i = -datagrid1.SelectedItems.Count; i < datagrid1.SelectedItems.Count; i++)
{
datagrid1.SelectedItems.RemoveAt(datagrid1.SelectedIndex);
}
So, every time the for loop is doing step 1, datagrid1.SelectedItems.Count is decreased by 1, and the variable i increases.
Do While dgData.SelectedItems.Count > 0
dgData.SelectedItem.Row.Delete()
Loop
My solution (if you are using adonet with autogeneratecolumns=true);
for (int i = dgv.SelectedItems.Count-1; i >= 0; i--)
{
DataRowView dataRow = (DataRowView)dgv.SelectedItems[i];
dataRow.Delete();
}

Categories