Index of inserted ListView item - c#

How can i get the index when adding my item to the ListView?
Im adding the item as follows
flatListView1.Items.Add(name).SubItems.AddRange(row1);

You may need this, as the item added at the end of listitem and will be last. So total items -1 will be index because index starting from zero.
flatListView1.Items.Add(name).SubItems.AddRange(row1);
int index = flatListView1.Items.Count -1;

var lviewItem=flatListView1.Items.Add(name)
var index=flatListView1.Items.IndexOf(lviewItem)
http://msdn.microsoft.com/en-us/library/system.windows.forms.listview.listviewitemcollection.indexof.aspx

Related

Loop doesn't iterate through listview

Could someone help me with this problem I am having? I am trying to search through a listview for occurrences of a string inputted via a textbox and record the location index value. Now although the code could be improved... it works. The problem I am having is the loop doesn't iterate. The code only displays the first index value even if there are more than one occurrences.
Could someone show me how to get this loop to loop?
Thanks in advance...
foreach (ListViewItem item in listView1.Items){
foreach (ListViewItem.ListViewSubItem subItem in item.SubItems){
if (subItem.Text.ToLower().StartsWith(textBox1.Text.ToLower())){
var index = listView1.FindItemWithText(textBox1.Text.ToLower());
MessageBox.Show(listView1.Items.IndexOf(index).ToString());
count++;
}
}
}
You need to change this
var index = listView1.FindItemWithText(textBox1.Text.ToLower());
MessageBox.Show(listView1.Items.IndexOf(index).ToString())
to this
var index = item.Index;
MessageBox.Show(listView1.Items[index].ToString())
#johnny-mopp is correct that the finditemwithtext will only return one item, therefore it makes the loop completely redundant if you only wanted the first item.
Maybe something like this will work?
var index = listView1.Select(x=>x.IndexOf(textBox1.Text.ToLower(), StringComparison.Ordinal));

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

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.

how to get index of the last selected row from a selection of rows in a DataGridView

I have a DataGridView and when I select multiple rows, I want the index of the last selected row. In other words, how to get the maximum most index from a selection of rows.
e.g., if I select row0, row1 and row6, I want the output as "6".
Regards.
if (dataGridView1.SelectedRows.Count > 0)
{
int lastIndex = dataGridView1.SelectedRows[dataGridView1.SelectedRows.Count - 1].Index;
}
var x = dataGridView1.SelectedRows.Cast<DataGridViewRow>().Max(row => row.Index);
is the same to:
var y = dataGridView1.SelectedRows.Cast<DataGridViewRow>().Last().Index;
Sorry, I'm adding answer for myself. There could be other faster ways, but this works.
List<int> lst = new List<int>();
foreach (DataGridViewRow row in dg.SelectedRows)
lst.Add(row.Index);
lst.Sort();
int i = lst[lst.Count - 1];
What this does is add the indexes of all selected rows to an List<> and then do a sort and then give the last item from the sorted List<>.
Note: the problem with Bala R's method is that, it depends on the order the rows were selected (ie, where the selected pointer lies). It fails when selected rows aren't in an order. It gives the row that was selected last, not necessarily the maximum most index from a selection of rows..
Thanks everyone!

Categories