如何找到listBox的选定索引值

时间:2011-10-10 10:01:42

标签: vb.net

我已经将数据表加载到listview.Now当我尝试执行选定的索引并检索要在相应文本框中显示的数据时。我发现一些错误“输入字符串格式不正确”。 但是当我直接从文件夹加载时它工作正常。

当我试图追踪错误---

  1. 从Datatable.Im检索的数据无法找到该行的索引。

  2. 但是从文件夹中列出并列在ListView.Index值中。

  3. 到目前为止:

    Dim breakfast As ListView.SelectedListViewItemCollection = Me.LOV.SelectedItems
     For Each item1 In breakfast
                index += Double.Parse(item1.SubItems(1).Text)
     Next
    

2 个答案:

答案 0 :(得分:0)

“输入字符串格式不正确”表示Double.Parse()方法抛出异常:给定字符串(item1.SubItems(1).Text)不是有效数字,不能转换为double。

使用Double.TryParse来避免例外。

答案 1 :(得分:0)

this post看来,下面的答案将是您的答案

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