WPF DataGrid Remove SelectedItems - c#

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

Related

Prevent Certain Items from Being Deleted in ListBox

So here is the code I have the allows me to delete selected items in my listbox.
ListBox.SelectedObjectCollection selectedItems = new ListBox.SelectedObjectCollection(lstOutput);
selectedItems = lstOutput.SelectedItems;
if (lstOutput.SelectedIndex != -1)
{
for (int i = selectedItems.Count - 1; i >= 0; i--)
lstOutput.Items.Remove(selectedItems[i]);
}
else
MessageBox.Show("Debe seleccionar un email");
The problem is that I have labels at the top that show what the output is. I also have statistics at the bottom of the page. The way the code is now, I am able to delete those, which I don't want. I am unsure of how to prevent these from being deleted.
first of all the fire line is unneeded and you can merger the two first line to:
ListBox.SelectedObjectCollection selectedItems = lstOutput.SelectedItems;
now for the remove of the item that you want to keep. you can make a condition.
for (int i = selectedItems.Count - 1; i >= 0; i--)
{
if(selectedItems[i] is /*here is where you come and check if the current selected item is of the type you don't want to delete*/)
lstOutput.Items.Remove(selectedItems[i]);
}
if you'll tell me what's the type of the "labels at the top" and "statistics at the bottom" i'll put it in the answer
EDIT
in similar to what you said you can do:
List<object> fixedItems = new List<object>();
fixedItems.Add(/*Your labels and statistics*/);
and then do
for (int i = selectedItems.Count - 1; i >= 0; i--)
{
if(fixedItems.Contains(selectedItems[i]) == false)
lstOutput.Items.Remove(selectedItems[i]);
}
for the list you'll need to add
using System.Collections.Generic;
at the beginning of the page

listbox selecteditems cannot be removed due to using ItemsSource

I need you to help fix my issues. When I use listbox ItemSource in my code, selected items cannot be allowed to remove. Without using ListBox ItemsSource, the remove operation is working. Why? Please give me your soultion code. I need to include ItemsSource for the listbox. Thanks a million times! Oh yes I am using C# 4.5 and WPF.
public SendEmail(List<string> items, ItemCollection needsItems)
: this()
{
_needList = needsItems;
lstNeeds.ItemsSource = _needList;
}
//Remove selected Items not working
if (lstNeeds.SelectedItem != null)
{
for (int i = lstNeeds.SelectedItems.Count - 1; i >= 0; i--)
{
lstNeeds.Items.Remove(lstNeeds.SelectedItems[i]);
}
}
You're trying to remove an item from the collection you're iterating over.
Try this:
if (lstNeeds.SelectedItem != null)
{
List<Int32> selIdx = new List<Int32>();
foreach (var item in lstNeeds.SelectedItems)
selIdx.Add(lstNeeds.Items.IndexOf(item);
selIdx.Sort(); //necessary?
for (Int32 idx = selIdx.Count - 1; i >= 0; i--)
{
lstNeeds.Items.RemoveAt(selIdx[i]);
}
}

Select next item in ListView

I have a method that removes currently selected item in a ListView
listView1.Items.Remove(listView1.SelectedItems[0]);
How do I select the next in the ListView after removing the selected one?
I tried something like
var index = listView1.SelectedItems[0].Index;
listView1.Items.Remove(listView1.SelectedItems[0]);
listView1.SelectedItems[0].Index = index;
But I get the error
Property or indexer 'System.Windows.Forms.ListViewItem.Index' cannot be
assigned to -- it is read only
Thank you.
I had to add one more line of code to a previous answer above, plus a check to verify the count was not exceeded:
int selectedIndex = listview.SelectedIndices[0];
selectedIndex++;
// Prevents exception on the last element:
if (selectedIndex < listview.Items.Count)
{
listview.Items[selectedIndex].Selected = true;
listview.Items[selectedIndex].Focused = true;
}
ListView doesn't have a SelectedIndex property. It has a SelectedIndices property.
Gets the indexes of the selected items in the control.
ListView.SelectedIndexCollection indexes = this.ListView1.SelectedIndices;
foreach ( int i in indexes )
{
//
}
try use listView1.SelectedIndices property
If you delete an item, the index of the "next" item is the same index as the one you just deleted. So, I would make sure you have listview1.IsSynchroniseDwithCurrentItemTrue = true and then
var index = listView1.SelectedItems[0].Index;
listView1.Items.Remove(listView1.SelectedItems[0]);
CollectionViewSource.GetDefaultView(listview).MoveCurrentTo(index);
I've done this in the following manner:
int selectedIndex = listview.SelectedIndices[0];
selectedIndex++;
listview.Items[selectedIndex].Selected = true;
I actually had to do this:
int[] indicies = new int[listViewCat.SelectedIndices.Count];
listViewCat.SelectedIndices.CopyTo(indicies, 0);
foreach(ListViewItem item in listViewCat.SelectedItems){
listViewCat.Items.Remove(item);
G.Categories.Remove(item.Text);
}
int k = 0;
foreach(int i in indicies)
listViewCat.Items[i+(k--)].Selected = true;
listViewCat.Select();
to get it to work, none of the other solutions was working for me.
Hopefully, a more experienced programmer can give a better solution.

Delete Multiple rows from datagridview

I've a datagridview in which values are inserted.The gridview is like this.
Item PRID
------ ------
Item1 1
Item2 2
Item3 2
I am trying to compare the PRID with a variable which holds the selected row PRID.
What I've done so far.
foreach (DataGridViewRow dgv_r in PurchaseOrder_dgv.Rows)
{
if (dgv_r.Cells[1].Value.ToString() == CurrentSelected_PRID_ForPurchaseOrder.ToString())
{
PurchaseOrder_dgv.Rows.Remove(dgv_r);
}
}
But it deletes the bottom row not the second row.and gives the following error.What I want is if the value of CurrentSelected_PRID_ForPurchaseOrder is equal to 2 then it should delete both the rows.I've tried it using for loop also but it gives me Index out of range error.It is giving the following error.
Object Reference Not set to an instance of object
The are a couple of ways around this. One is to do the following
for (int i = dataGridView1.RowCount - 1; i >= 0; i--)
if (String.Compare(dataGridView1.Rows[i].Cells[1].Value.ToString(), "2") == 0)
dataGridView1.Rows.Remove(dataGridView1.Rows[i]);
This is looping from the bottom end of the DataGridView and avoids the problem with removing rows whilst iterating.
I hope this helps.
As mellamokb pointed out the reason is because your editing the collection during the foreach. One solution would be to store the rows with the same PRID in a list first and then remove them after. Something like this
var rowsToRemove = new List<int>();
foreach (DataGridViewRow dgv_r in PurchaseOrder_dgv.Rows)
{
if (dgv_r.Cells[1].Value.ToString() == CurrentSelected_PRID_ForPurchaseOrder.ToString())
{
rowsToRemove.Add(dgv_r.Index);
}
}
rowsToRemove.Reverse();
rowsToRemove.ForEach(i => PurchaseOrder_dgv.Rows.RemoveAt(i));
Another solution is to use a while loop instead which takes into account the possiblity of the collection size changing.
int i = 0;
while (i < PurchaseOrder_dgv.Rows.Count)
{
if (PurchaseOrder_dgv.Rows[i].Cells[1].Value.ToString() == CurrentSelected_PRID_ForPurchaseOrder.ToString())
{
PurchaseOrder_dgv.Rows.RemoveAt(i);
}
i++;
}

Remove selected rows from multi-column listView

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

Categories