Selecting ListView in C# - c#

i have ListView with items
one ListView with items another one is empty
i need to copy from first ListView selected item to another ListView at the same time
i have to remove the selected item in first ListView in C#

Rather than removing items from the collection that you're enumerating (as per Wael's answer), which is a "bad idea", use a temporary collection, in this instance a List to store them in before removing them:
List<ListViewItem> itemsToMove = new List<ListViewItem>();
foreach (ListViewItem item in listView1.SelectedItems)
{
itemsToMove.Add(item);
}
foreach (ListViewItem item in itemsToMove)
{
listView1.Items.Remove(item);
listView2.Items.Add(item);
}
Where listView1 is the list with the selected items and listView2 is the list to move them to.

this code will copy from the first listview1 to listview2 all the selected items and delete it from listview1
foreach (ListViewItem itm in ListView1.SelectedItems)
{
ListView1.Items.Remove(itm);
listView2.Items.Add(itm);
}

Related

How to add items on listview (each column)

I tried using this code to add items on ListView but clearly I only add one column on each rows although I have 10 columns. Here's my code:
ListView1.Items.Add(firstname.Text)
ListView1.Items.Add(middlename.Text)
ListView1.Items.Add(lastname.Text)
ListView1.Items.Add(gender.Text)
ListView1.Items.Add(age.Text)
ListView1.Items.Add(address.Text)
ListView1.Items.Add(lrnNumber.Text)
ListView1.Items.Add(formerschool.Text)
ListView1.Items.Add(strandcourse.Text)
ListView1.Items.Add(contact.Text)
ListView1.Items.Add(birthdate.Text)
You should create the object ListViewItem at first:
ListViewItem item = new ListViewItem(new []{"1","2","3","4"});
listView1.Items.Add(item);

move the selected items from checkedlistbox to listbox C#

I'm trying to move the selected items from checkedlistbox to listbox.But I can't...
for (int x = 0; x<=checkedListBox1.Items.Count;x++ )
{
if (checkedListBox1.GetItemChecked(x))
{
listBox1.Items.Add(checkedListBox1.SelectedItem + "\r\n");
}
}
Please help me
Below code will add selected items to listBox1 and remove it from checkedListBox1, but SelectedItems will be one item each time because Multi-selection is not supported on CheckedListBox you can use CheckedItems instead of SelectedItems to select more than one item.
// Add Selected Items to ListBox
listBox1.Items.AddRange(checkedListBox1.SelectedItems.OfType<object>().ToArray());
// Remove from CheckedListBox
foreach (var item in checkedListBox1.SelectedItems.OfType<object>().ToList())
{
checkedListBox1.Items.Remove(item);
}

How to get selected index when used to Ctrl+A key to select rows in grid

How do I get the row index or record Position of all items in gridData.SelectItems property.
SelectedItems Contains actual data (ViewModel) from the collection bound to datagrid's ItemSource. Therefore you can obtain the index in that collection for every item selected.
if its a datagridview on winforms then this would work
List<int> rowIndexs = new List<int>();
foreach (DataGridViewRow item in gridData.SelectedRows)
{
rowIndexs.Add(item.Index);
}

Delete selected items in a GridView

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.

List Box control in asp.net

I have two listbox controls Listbox1 and Listbox2. I want to get the count of items of Listbox2 which are selected from Listbox1 in c#? Suppose i have total 7 items in Listbox1 and from those i have selected only 3 items in Listbox2 control. I want to get the count of items of Listbox2 in C#?
Wonder why nobody used Linq.
#Riya: I understand your requirement as, you want the Count of SelectedItems in ListBox1 that are present in ListBox2 Items. If so do this.
var filteredListCount = ListBox2.Items
.Cast<ListItem>()
.Where(li =>
ListBox1.Items
.Cast<ListItem>()
.Where(item => item.Selected)
.Select(item => item.Text).Contains(li.Text))
.Count();
Loop thru the selected items when the selection is changed
Something like this:
int count = 0;
foreach(string itemListbox2 in listBox2.Items)
{
if (itemListbox2.Selected)
{
foreach(string itemListbox1 in listbox1.Items)
{
if (itemListbox1.Selected)
{
if(itemListbox1.Equals(itemListbox2))
{
count++;
break;
}
}
}
}
}
You can loop on all selected items in ListBox1 and inside the loop you search for an item with the same value in ListBox2 and if it's selected you increment a counter.
A Listbox in asp.net doesn't have SelectedItems. Therefor loop through the Items and check if they are selected. If so, find an item in the other list with the same value. If you find a corresponding item, count it. Like this:
int count = 0;
foreach (ListItem item in secondListBox.Items)
{
if (item.Selected)
{
ListItem itemWithSameValue = firstListBox.Items.FindByValue(item.Value);
if (itemWithSameValue != null)
{
count++;
}
}
}

Categories

Resources