2017-02-17 77 views
1

我需要遍歷DataView中的行,以便從行中獲取數據或對象以添加到列表框。這是我的DataView代碼。如果用戶輸入的ID與「CustomerID」字段匹配,則RowFilter設置爲僅抽取一行的數據。如何循環遍歷已添加的行以爲行中的數據創建臨時對象?如何循環訪問vb.net中的數據視圖中的行

'create new table for storing incident data 
     Dim incidentTable As DataView = CType(incidentsDataSource.Select(
     DataSourceSelectArguments.Empty), DataView) 
     incidentTable.RowFilter = ("CustomerID = '" & custIDTextBox.Text & "'") 
     Dim incidentRow As DataRowView = incidentTable(0) 

     'declare temporary incident and store all data in corresponding fields 
     Dim incident As New Incident 
     With incident 
      .IncidentID = incidentRow("IncidentID").ToString 
      .CustomerID = incidentRow("CustomerID").ToString 
      .ProductCode = incidentRow("ProductCode").ToString 
      .TechID = incidentRow("TechID").ToString 
      .DateOpened = incidentRow("DateOpened").ToString 
      .DateClosed = incidentRow("DateClosed").ToString 
      .Title = incidentRow("Title").ToString 
     End With 

回答

1

DataView本身是一個列表;精確的對象清單DataRowView。您只需循環穿過DataView本身,例如

For Each row As DataRowView In myDataView 
    Dim name = CStr(row("Name")) 
    '... 
Next