Delete selected items in a GridView - c#

IList<object> itemsSelected = MyGrid.SelectedItems;
foreach (object itemSelected in itemsSelected)
{
MyGrid.SelectedItems.Remove(itemSelected);
}
I try remove selected items from a GridView but not all selected items are removed.
Could someone help me?

object[] itemsSelected = MyGrid.SelectedItems.ToArray<object>();
foreach (object item in itemsSelected)
{
MyGrid.Items.Remove(item);
}

I had the exact problem as well. I wrote something like this but it didn't delete all items too, just some of them :
foreach(var item in MyGridView.SelectedItems)
{
MyGridView.Items.Remove(item);
}
But write this which will delete all your selected items for sure :
while (YourGridView.SelectedItems.Count != 0)
{
YourGridView.Items.Remove(YouGridView.SelectedItem);
}

there isn't exception, when you use foreach with SelectedItems? When you remove item, SelectedItems array is modified and foreach throws an exception. (Though I've tried on ListBox control). Try to use for-operator and remove items from last to first by index.

Based on your answers in the comments, I assume you are working on a C# winforms application.
Is it possible that what you are actualy using is a ListBox and not a GridView?
If that is so, you should use the ClearSelected() method which unselects all items in the ListBox.

Related

How to populated C# listbox with string separated comma

I have a string that separated by comma like this:
"test1,test2,test3"
and i want to convert those string to list with folowing code :
private void convertToList()
{
try{
List<string> myList = occ.Split(',').ToList();
listBox1.Items.Add(myList);
}catch(Exception e){
MessageBox.Show(e.Message);
}
}
I think that code will convert the string into a list and add it into a Listbox, instead, it shows only "collection"(yes only show the word "collection", nothing more) not the actual list.
why that's happened? can you tell me what's the right code?
Well, Add adds a single item which is a List<string> in your case. What should ListBox show for this? Collection seems to be a good enough solution. If you want to add entire collection in one go, try AddRange:
listBox1.Items.AddRange(occ.Split(','));
If you insist on Add, you have to loop in order to Add each item of the collection:
// To stop unwanted redrawing after each item addition
listBox1.BeginUpdate();
try {
foreach (var item in occ.Split(','))
listBox1.Items.Add(item);
}
finally {
listBox1.EndUpdate();
}

Checked items in a CheckedListBox

In C#, how can I determine if an item in a CheckedListBox is checked or not if I have the text of the CheckListBoxItem?
I am needing to loop through all CheckedListBoxItems, and retrieve the text and the checked state.
Here is what I have so far:
CheckedListBox.ObjectCollection items = checkedListBoxFileNames.Items;
foreach (var item in items)
{
}
I am not sure on how to determine if an item is checked or not.
Thanks in advance.
You don't need this foreach loop.
Try this:
if(this.m_CheckedListbox.CheckedItems.Contains("Item1")
{
//make an action, if it's checked.
}
if(this.m_CheckedListbox.CheckedItems.Contains("Item2")
{
//make an action, if it's checked.
}
// etc...
// this.m_CheckedListbox should be the name of your checked list box.
You can use like
IEnumerable<int> allChecked = (from item in chkBoxList.Items.Cast<ListItem>()
where item.Selected
select int.Parse(item.Value));
more Details click here
Try like this:
foreach(object itemChecked in checkedListBox1.CheckedItems)
{
//your code
}
Also check CheckedListBox.CheckedItems Property
Collection of checked items in this CheckedListBox.

How to compare if combobox item is equal with listview item or listview already contains combobox item in c#

How to compare or check whether the item in combobox is already existing in listview? Please help....
if (lvCart.Items.ContainsKey(cmbdummy.SelectedItem.ToString()))
{
MessageBox.Show("Duplicate Entry!");
}
You need to check with respective SelectedValue.
Try This:
if (lvCart.Items.ContainsKey(cmbdummy.SelectedValue.ToString()))
{
MessageBox.Show("Duplicate Entry!");
}

How to get the name of the selected items from the ListView

I have a ListView in the itemDetailPage and I would like to share the selected item via e-mail. For which, I need to get the selected item from that listview.
What I tried is
if(listview1.SelectedItems.Count==1)
{
var item = listview1.SelectedItem
}
But I dont get the name, I get only the place it is taken from. Any suggestions?
Use Count instead of count. C# is case sensitive.
if(listview1.SelectedItems.Count == 1)
{
var item = listview1.SelectedItems[0].ToString();
}
Try this. You need to cast SelectedItem with your model class.
if (listview1.SelectedItems.Count==1)
{
var item = listview1.SelectedItems[0] as Node
}

Foreach doesn't go through all items?

I have this code:
foreach (var item in ListView1.Items)
{
ListView1.Items.Remove(item);
ListView21.Items.Add(item);
}
the loop stops at half of the items?
Any idea?
EDIT
Well, maybe it's my mistake, I need to clarify that this is UltraListView control from Infrajistics, where I can't add item to another list unless I remove it or clone it from the original list.
But thanks, most of the comments regarding do not modify the list within the loop were correct, so this code works:
foreach (var item in listView1.Items)
{
var i = item.Clone(true);
listView2.Items.Add(i);
}
listView1.Items.Clear();
Thanks,
You cannot modify iterated collection, it should die with exception (or in undefined behavior).
Try making a copy of the array:
foreach (var item in ListView1.Items.ToArray())
{
ListView1.Items.Remove(item);
ListView21.Items.Add(item);
}
EDIT:
in fact, your example code can be achieved by writing:
ListView21.Items.AddRange(ListView1.Items);
ListView1.Items.Clear();
(which in fact isn't EXACTLY what you are doing, but gives the same result and I think it won't bother you having the same content in both listviews for a moment). The latter is supported since .NET2.0, first solution requires linq, and therefore .NET3.5.
You are modifying the collection you are looping through. Try using a for statement from top to bottom (from the item with the highest index to 0).
for (int i = ListView1.Items.Count - 1; i >= 0; i--)
{
var item = ListView1.Items[i];
ListView1.Items.Remove(item);
ListView21.Items.Insert(0, item);
}
It will cause a runtime exception, complaining that you cannot modify the collection while iterating through it. You have to use for loop instead.
for(int index = Items.Count; index > 0; index--)
{
.......
// use Add and RemoveAt
}
EDIT : As mentioned by others. If you just need to move items from one collection to the other. AddRange and Clear will be better.
Do you get any exception or error message? Looping in a collection and remove items from the same collection is always a bad idea.
This looks like the WinForms list view control, so:
ListViewItem[] items = ListView1.Items.ToArray();
ListView1.Items.Clear();
ListView21.Items.AddRange(items);
Why just not CopyTo() to new list and then Clear() items?
You are looping through all items, removing all of them, then adding them to another list. As others have commented, you cannot remove items from a list in a for-each. Why not looping through all items to add them to your other list, and then remove them all in one go?
foreach (var item in ListView1.Items)
{
ListView21.Items.Add(item);
}
ListView1.Items.Clear(); // remove all
PS: is this an ASP.NET listview or a WinForms listview?
That's because you're changing the collection inside the loop.
Use a normal for loop as follows:
for(int i=0; i < ListView1.Items.Count-1; i++)
{
ListView21.Items.Add(ListView1.Items[i]);
ListView1.Items.RemoveAt(i);
}

Categories

Resources