2012-02-21 42 views
1

我試圖用一個ListView從數據集中顯示的記錄。在這些記錄中,我有4列:EmployeeID,EmployeeName,CustomerID和CustomerName。員工和客戶都可以多次列出,但可以使用不同的組合。VB.Net的ListView

例如,如果我們有員工1,2,3和顧客A,B,C,我們可以得到: (1A) (1B) (1C) (2A) (2B) 等

什麼事與我是我想補充說(1A)到ListView,然後當它試圖增加(1B)我得到一個錯誤說,它不能添加相同的項目。

這裏是我到目前爲止的代碼:

With list 
    .Clear() 
    .Columns.Add("Employee ID") 
    .Columns.Add("Employee Name") 
    .Columns.Add("Customer ID") 
    .Columns.Add("Customer Name") 
    For Each row As UpFromCostExceptions.dtExceptionsRow In _dsExceptions.dtExceptions 
     Dim lvItem As ListViewItem = .Items.Add(row.EmpID.ToString) 
     lvItem.SubItems.Add(row.EmployeeName) 
     lvItem.SubItems.Add(row.CustomerID) 
     lvItem.SubItems.Add(row.CustomerName) 
    Next 
    .Refresh() 
End With 

任何人都看到發生了什麼事?先謝謝您的幫助!

+0

我不知道你在哪裏使用從合併字段中的數據。如果CustomerID是數字,則需要像使用EmpID一樣包含.ToString()。 – 2012-02-21 16:53:11

+0

我沒有在單個字段中使用組合數據。但我有兩個listView的項目。這兩個項目對於EmpID具有相同的值,但對於CustomerID具有不同的值。當我嘗試添加第二項時,它會收到錯誤,因爲它無法添加相同的EmpID。 – 2012-02-23 16:06:35

回答

1

我想通了,如果我去通過,並創建項目並添加一個ListViewItem到ListView控件之前添加子項,我可以有重複的值。下面是我使用的代碼:

With listExceptions 
      .Clear() 
      .Columns.Add("Employee ID") 
      .Columns.Add("Employee Name") 
      .Columns.Add("Customer ID") 
      .Columns.Add("Customer Name") 
      For Each row As UpFromCostExceptions.dtExceptionsRow In _dsExceptions.dtExceptions 
       Dim lvItem As New ListViewItem(row.EmpID.ToString) 
       lvItem.SubItems.Add(row.EmployeeName) 
       lvItem.SubItems.Add(row.CustomerID) 
       lvItem.SubItems.Add(row.CustomerName) 
       listExceptions.Items.Add(lvItem) 
      Next 
      .Refresh() 
     End With 
0

嘗試使row.EmpID.ToString()唯一的,看看它是否仍然會拋出異常。

Dim lvItem As ListViewItem = .Items.Add(row.EmpID.ToString) 
+0

我不確定你的意思。該列表需要能夠以某種方式接受重複項,但只要整行不匹配另一行即可。只要CustomerID不同,一個EmployeeID應該能夠在列表中多次出現。如果你指的是其他事情,請澄清。 – 2012-02-23 16:04:32

+0

是的,我明白了,我不清楚你輸入到Items.Add中的初始字符串.Add需要是唯一的,子項目中的任何內容都不會因爲項目存儲在散列 – 2012-02-23 16:32:18

3

試試這個,我有簡單的類有兩個字段,標識,像這樣的函數名稱,我填充的列表。

'lstv = is the actual ListView Control 
Dim i As Integer 
Dim listitem As New ListViewItem 
If lstv.Items.Count > 0 Then 
    lstv.Items.Clear() 
End If 
For i = 0 To PropertyList.Count - 1 
    listitem = New ListViewItem(PropertyList(i).Idefntifier) 
    listitem.SubItems.Add(PropertyList(i).Title)   
    lstv.Items.Add(listitem) 
Next i