listview checkbox checked row item - c#

I want to reach checked rows item in ASP.Net Listview control.
For example:
if 3 Lines of Listview checked, I want to reach checked items value in listview.
How can I do this?
Thanks.

You can use listview property 'CheckedItems'.
You can get list of items which are checked using listview.CheckedItems. For the value in that item, based on the subitem index you need to fetch it. For ex.
ListView.CheckedListViewItemCollection checkedItems =
ListView1.CheckedItems;
foreach ( ListViewItem item in checkedItems )
{
value = item.SubItems[1].Text;
}

Related

how to print the checked items from the checked listbox to a label in c#?

I'm Trying to fetch items from checked listbox when the user checks the listbox item it should be displayed in a label on a button click. I tried using this:
foreach (object item in checkedlistbox1.CheckedItems)
{
labelto.Text += checkedlistbox1.SelectedItem.ToString();
}
But I'm getting this exception:
List that this enumerator is bound to has been found, enumerator can only be used if the list does not change.
How to print the checked items from the checked listbox to a label?
This is not a complicated task, why don't you make try with the following:
string displayText = "";
foreach(object item in checkedListBox1.CheckedItems)
{
DataRowView castedItem = item as DataRowView;
displayText += castedItem["boundPropertyNameHere"];
}
labelto.Text = displayText;
Please note:
Where boundPropertyNameHere be the name of property that used to bind the collection.

c# select listview item if two listviews contains it

In my program I have 2 listviews and 1 button.
When I press the button, every listview item in the first listview will be selected in the second listview. The items in the first listview always exist in the second listview, but not the other way around.
I'm having trouble with selecting the items in the second listview. I'm trying to get the index with IndexOf.
foreach (ListViewItem item in firstListView.Items)
{
int index = secondListView.Items.IndexOf(item);
secondListView.Items[index].Selected = true;
}
I always get an error that index is -1 when I click the button. And I don't understand what I'm doing wrong.
SOLUTION
So what I tried to do here finding an index of an item which belongs to a different listview, and it doesn't work like that. Even though the text in both listviews are the same, the listview items are not identically the same because they are reference types.
I was not able to use the Text property because items could have the same text. So the solution I had was putting an unique integer in the Tag property for each ListViewItem. Since integers are value types, I can use that integer to check whether the ListViewItem in the first listview is in the second.
// Loop through each item in the first listview
foreach (ListViewItem item1 in firstListView.Items)
{
// For each item in the first listview, loop through the second listview to find if it's there
foreach (ListViewItem item2 in secondListView.Items)
{
// Check if item1 is identical to item2 by looking at its tag
if (int.Parse(item1.Tag) == int.Parse(item2.Tag))
{
// The item has been found and will be selected!
item2.Selected = true;
}
}
}
You can use such linq query to select items of the second list which exist in the first list too:
var items = from i1 in listView1.Items.Cast<ListViewItem>()
from i2 in listView2.Items.Cast<ListViewItem>()
where i1.SubItems.Cast<ListViewItem.ListViewSubItem>()
.Where((s, i) => s.Text != i2.SubItems[i].Text).Count() == 0
select i2;
items.ToList().ForEach(x => { x.Selected = true; });
Note:
When to try to use secondListView.Items.IndexOf method to find an item which belongs to the firstListView you can not expect it to find the item. The item you are trying to find its index, doesn't exists in second listview's item collection. You should find items using Text property of the item and sub items.
Well the same item obviously cant exist in two different lists.
If the text is the same, then try looking by it:
foreach (ListViewItem listViewItem in l1.Items)
{
var item = l2.Items.Cast<ListViewItem>().Where(lvi => lvi.Text == listViewItem.Text);
item.Selected=true;
}
Or:
foreach (ListViewItem listViewItem in l2.Items.Cast<ListViewItem>()
.Where(lvi => l1.Items.Cast<ListViewItem>().Any(lvi2 => lvi.Text == lvi2.Text))
{
listVieItem.Selected=true;
}
IndexOf returns -1 if it cannot find the object passed to it in the list. My guess is that you have an issue with your equality comparison. Do the objects in the lists implement IComparable? Otherwise they may not be finding the items correctly and are reverting to a bad equality comparison. ListViewItem may not be comparing how you expect.
you can try finding the item by text and then get the index like this
foreach (ListViewItem item in firstListView.Items)
{
var itm = secondListView.FindItemWithText(item.Text);
int index = secondListView.Items.IndexOf(itm);
secondListView.Items[index].Selected = true;
secondListView.Select();
}
what FindItemWithText will do is get the item in secondListView which will be of same name as in firstListView and IndexOf will get the index of item in secondListView
Edit
This solution is good when you have two lists whose items are same and does not include any repetition in name else if you have two items in secondListView with same name FindItemWithText will get the first item only.

Index of a list view item in a group

ListViewItem.Index property gets the zero-based index of the item within the ListView control.
Now I need index of certain item in a ListViewGroup , not the whole control.
Is it possible?
You can find its index within the group as such :
ListViewItem item = /* certain item in a ListviewGroup */;
var index = item.Group.Items.IndexOf(item);

ListView selected items add other ListView

How to add ListView selected items in other ListView?
Example project:
ExpTreeLib
Put this form ListView2 and add lv.selectedItems.
Thanks in advance.
You can use this method to add the items
ListView list = new ListView();
ListViewItem item = new ListViewItem();
// add them
list.Items.Add(item);
This would be enough, now you can use a simple loop for each element to add it to that list.
foreach (ListViewItem item in ListView.SelectedItems) {
// create a new list, or use currently present list
// add each item to it using .Add() method.
}

Unable to select multiple items from a listbox in a Windows Phone 7 application

How can I select multiple items from the listbox in a Windows Phone 7 application?
e.g
listboxName.SelectedIndex = 0;
listboxName.SelectedIndex = 1;
listboxName.SelectedIndex = 2;
The above code selects 2 while I need to select all three of those.
The values I need to preselect are given to me in a array like
{true,true,true,false,false}
So I tried the using IsSelected like shown below... doesn't work.
int i = 0;
foreach (ListBoxItem currentItem in listboxName.SelectedItems)
{
if (tagindexeselected[i])
{
currentItem.IsSelected = true;
}
i++;
}
What would be the proper way to select multiple items in a listbox?
Hard to say there's a single, best way - it depends on how you're populating your list box, etc. First, be sure your list box's Selection Mode is set to Multiple or Extended.
One option is to use the ListBox's SelectedItems collection:
listBox1.SelectedItems.Add(listBox1.Items[0]);
listBox1.SelectedItems.Add(listBox1.Items[1]);
listBox1.SelectedItems.Add(listBox1.Items[2]);
Note also, in your example above, you're iterating over the SelectedItems collection - not the Items collection. If nothing is selected, that's an empty collection. Also, if your list box ItemsSource is not a series of ListBox Items (you can set your itemsSource to almost any enumeration), you will get an InvalidCastException when you go to run your foreach statement.
foreach (DataRowView item in lstServer.SelectedItems)
{
string WebServerIP = item[lstServer.DisplayMember].ToString();
string WebServerUrl = item[lstServer.ValueMember].ToString();
_WebObjIgent.Url = WebServerUrl;
}
Note : lstServer is Listbox of window application . By using Displaymember and valuemember proprty you can access value and text of listbox.

Categories