Vb.net 如何找到listBox的选定索引值
我已将datatable加载到listview。现在,当我尝试执行所选索引并检索数据以显示在相应的文本框中时。我发现一些错误“输入字符串格式不正确”。 但当我直接从文件夹中加载时,效果很好 当我试图追踪错误时---Vb.net 如何找到listBox的选定索引值,vb.net,Vb.net,我已将datatable加载到listview。现在,当我尝试执行所选索引并检索数据以显示在相应的文本框中时。我发现一些错误“输入字符串格式不正确”。 但当我直接从文件夹中加载时,效果很好 当我试图追踪错误时--- 从Datatable.Im检索到的数据找不到该行的索引 但从文件夹中找到,并在ListView中列出。找到索引值 到目前为止: Dim breakfast As ListView.SelectedListViewItemCollection = Me.LOV.SelectedItem
Dim breakfast As ListView.SelectedListViewItemCollection = Me.LOV.SelectedItems
For Each item1 In breakfast
index += Double.Parse(item1.SubItems(1).Text)
Next
“输入字符串格式不正确”表示Double.Parse()方法引发异常:给定字符串(item1.SubItems(1.Text))不是有效数字,无法转换为Double
使用Double.TryParse
避免出现异常情况。下面将是您的答案
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