How can be find selected index value of listBox
I have loaded datatable to listview.Now when i try to do a selected index and retrieve data to display in respective text box. I find some error "input string format incorrect". but when i directly load from folder it works fine.
When i try to trace the error ---
Data that retri开发者_Python百科eved from Datatable.Im not able to find the index of the row.
But from folder and listed in ListView.Index value is found.
So far:
Dim breakfast As ListView.SelectedListViewItemCollection = Me.LOV.SelectedItems
For Each item1 In breakfast
index += Double.Parse(item1.SubItems(1).Text)
Next
"Input string format incorrect" means that the Double.Parse() Method throws an exception: The given string (item1.SubItems(1).Text) is not a valid number and cannot be casted into a double.
Use Double.TryParse
to avoid an exception here.
From this post it seems below will be your answer
Private Sub listView_ItemCreated(sender As Object, e As ListViewItemEventArgs)
' exit if we have already selected an item; This is mainly helpful for
' postbacks, and will also serve to stop processing once we've found our
' key; Optionally we could remove the ItemCreated event from the ListView
' here instead of just returning.
If listView.SelectedIndex > -1 Then
Return
End If
Dim item As ListViewDataItem = TryCast(e.Item, ListViewDataItem)
' check to see if the item is the one we want to select (arbitrary) just return true if you want it selected
If DoSelectDataItem(item) = True Then
' setting the SelectedIndex is all we really need to do unless
' we want to change the template the item will use to render;
listView.SelectedIndex = item.DisplayIndex
If listView.SelectedItemTemplate IsNot Nothing Then
' Unfortunately ListView has already a selected a template to use;
' so clear that out
e.Item.Controls.Clear()
' intantiate the SelectedItemTemplate in our item;
' ListView will DataBind it for us later after ItemCreated has finished!
listView.SelectedItemTemplate.InstantiateIn(e.Item)
End If
End If
End Sub
Private Function DoSelectDataItem(item As ListViewDataItem) As Boolean
Return item.DisplayIndex = 0
' selects the first item in the list (this is just an example after all; keeping it simple :D )
End Function
精彩评论