Rob Rudloff
asked on
How to select the newest item/row in a listview?
I am new at VB.net, and have inherited the code for an old desktop app in use at my company...
I have a ListView that users can "add" rows to by clicking an "add" button, which calls the code below.
Is there a way to programmatically select the row/item that the user just added? Perhaps there's a way to get the index of the "ListViewItem" immediately after it is Dim'd, then point to that row after the .Items.Add ?
Thanks.
I have a ListView that users can "add" rows to by clicking an "add" button, which calls the code below.
Is there a way to programmatically select the row/item that the user just added? Perhaps there's a way to get the index of the "ListViewItem" immediately after it is Dim'd, then point to that row after the .Items.Add ?
Thanks.
Dim itmCart = New ListViewItem(HeaderID.ToString())
itmCart.SubItems.Add(lsCartNumber)
itmCart.SubItems.Add(lsUserID)
lstCarts.Items.Add(itmCart)
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Kyle --
Using that code, I got an error with the "[i]lstCarts.SelectedIndex =[/i]" part of the line:
[i]Error 'SelectedIndex' is not a member of 'ListView'[/i]
So I looked at some of the other members of lstCarts, and did some reading and found that the ".Selected" property of an item will get or set the "selected" state.
So, using your note that the last item added has the highest index, I changed your line:
[i] lstCarts.SelectedIndex = lstCarts.Items.Count() - 1[/i]
to
[i]lstCarts.Items(lstCarts .Items.Cou nt() - 1).Selected = True[/i]
and it works.
Next, I need to see how this all works when the listview is sorted. I assume the index stays the same as the order it was added in ... Or perhaps the index changes when the items are sorted.
Thanks
Using that code, I got an error with the "[i]lstCarts.SelectedIndex
[i]Error 'SelectedIndex' is not a member of 'ListView'[/i]
So I looked at some of the other members of lstCarts, and did some reading and found that the ".Selected" property of an item will get or set the "selected" state.
So, using your note that the last item added has the highest index, I changed your line:
[i] lstCarts.SelectedIndex = lstCarts.Items.Count() - 1[/i]
to
[i]lstCarts.Items(lstCarts
and it works.
Next, I need to see how this all works when the listview is sorted. I assume the index stays the same as the order it was added in ... Or perhaps the index changes when the items are sorted.
Thanks
I believe you are correct in that the index will stay the same.
You can use a combination of the sorting / sorted event to get what you need:
https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.listview_events(v=vs.110).aspx
sorting you would save the selected item (or value)
sorted you would reset the selected item based on the one found in sorting.
You can use a combination of the sorting / sorted event to get what you need:
https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.listview_events(v=vs.110).aspx
sorting you would save the selected item (or value)
sorted you would reset the selected item based on the one found in sorting.
Form1.vb -
Open in new window
Form1.Designer.vb -Open in new window
Produces the following output --saige-