2011-10-10 60 views
0

我已经加载数据表到listview.Now当我尝试做一个选定的索引和检索数据显示在各自的文本框中。我发现一些错误“输入字符串格式不正确”。 但是当我直接从文件夹加载它工作正常。如何找到列表框的选定索引值

当我尝试跟踪误差---

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

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

到目前为止:

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

回答

0

“输入字符串格式不正确的” 是指Double.Parse()方法抛出异常:给定的字符串(item1.SubItems(1)。文本)被不是有效的数字,不能被转换成双精度。

使用Double.TryParse可避免此处出现异常。

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 
相关问题