开发者

How can be find selected index value of listBox

开发者 https://www.devze.com 2023-04-12 10:59 出处:网络
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\".

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

  1. Data that retri开发者_Python百科eved from Datatable.Im not able to find the index of the row.

  2. 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
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号