Upgrade of ListView in VB6 to VB .Net

ben_ng

Member
Joined
Sep 13, 2007
Messages
18
Location
Singapore
Programming Experience
1-3
Hi,
I have the following code in VB6:
VB.NET:
Dim itmCI As ListItem
    Do While Not resListview.EOF
        Set itmCI = lvwListView.ListItems.Add(, , CStr(resListview!CompanyName))
        If IsNull(resListview!CompanyId) Then
            itmCI.SubItems(1) = ""
        Else
            itmCI.SubItems(1) = CStr(resListview!CompanyId)
        End If
        resListview.MoveNext
    Loop
    Set resListview = Nothing
After conversion to VB .Net, the code is changed to :
VB.NET:
Dim itmCI As New ListViewItem("itmCI", 0)
    Do While Not resListview.EOF
        itmCI = lvwListView.ListItems.Add( ,  , CStr(resListview.Fields("CompanyName").Value))
        If IsDBNull(resListview.Fields("CompanyId").Value) Then
            If itmCI.SubItems.Count > 1 Then
                itmCI.SubItems(1).Text = ""
            Else
                itmCI.SubItems.Insert(1, New System.Windows.Forms.ListViewItem.ListViewSubItem(Nothing, ""))
            End If
        Else
            If itmCI.SubItems.Count > 1 Then
                itmCI.SubItems(1).Text = CStr(resListview.Fields("CompanyId").Value)
            Else
                itmCI.SubItems.Insert(1, New System.Windows.Forms.ListViewItem.ListViewSubItem(Nothing, CStr(resListview.Fields("CompanyId").Value)))
            End If
        End If      
        resListview.MoveNext()  
    Loop
My original ListView control 'lvwListView' has been converted to 'AxComctlLib.AxListView'.
While there is no compilation error,I encountered runtime errors: Unable to cast COM object 'ComctlLib.ListItemClass' to class 'Systems.Windows.Forms.ListViewItem'. Instances of types that represent COM components cannot be cast to types that do not represent COM components;...

Now,anyone can tell me how I can solve this problem?
Thanks to all!
 
What I am attempting to do now:
Use a ListView Control in place of the converted ListView.
However,I find that the columns do not sort themselves when clicked upon. I guess they will only sort with additional coding.
Is this the only solution?
 
Back
Top