Listview not all the rows are filled

Harcon

Member
Joined
May 8, 2012
Messages
18
Programming Experience
1-3
I finished my application and loaded the data in the database and now when sending a request the data loads in to a listview.
But when it shows not all the rows are filled correct and you see empty rows in the list view.

It looks like uncoupling of the rows

VB.NET:
Private Sub Zoekinstellingen_laden(ByVal SQLString As String)
        Dim lv_item = 0
        Dim rs As New ADODB.Recordset
        Try
            LstKelderboek.Items.Clear()
            rs.Open(SQLString, cn)
            Do While Not rs.EOF = True
                LstKelderboek.Items.Add(rs.Fields.Item("idtblWijn").Value)
                LstKelderboek.Items(lv_item).SubItems.Add(rs.Fields.Item("wijnhuis").Value)
                LstKelderboek.Items(lv_item).SubItems.Add(rs.Fields.Item("Jaar").Value)
                LstKelderboek.Items(lv_item).SubItems.Add(rs.Fields.Item("TblLandnm").Value)
                LstKelderboek.Items(lv_item).SubItems.Add(rs.Fields.Item("tblstreek").Value)
                LstKelderboek.Items(lv_item).SubItems.Add(rs.Fields.Item("tblAppellationnm").Value)
                LstKelderboek.Items(lv_item).SubItems.Add(rs.Fields.Item("Aantal").Value)
                LstKelderboek.Items(lv_item).SubItems.Add(rs.Fields.Item("tblOpslagcol").Value)
                rs.MoveNext()
                lv_item = lv_item + 1
            Loop
            rs.Close()

        Catch ex As Exception
            MsgBox("Kelderboek Foutmelding: " & vbCrLf & vbCrLf & "De streken zijn niet ingeladen." & vbCrLf & vbCrLf & ex.Message)
        Finally
            rs = Nothing
        End Try
    End Sub
 
Firstly, why are you using VB6 data access code? Unless this is an upgraded VB6 app, you should be using ADO.NET, NOT ADO. If you want to use VB6 then you should use VB6 but if you're going to use VB.NET then use VB.NET. You should be using an ADO.NET data reader to query your database. You should also be using a DataGridView to display the data rather than a ListView. The ListView is NOT a grid control.

Regardless, I'm not immediately sure that this is the cause but you should change the code in that loop significantly as it's very inefficient and hard to read as it is. You can get rid of that `lv_item` variable altogether (which is poorly-named anyway, because it's an index and not an item) because you don't need any index. You've already got the item because you just created it so why use an index over and over to get what you've already got? This:
                LstKelderboek.Items.Add(rs.Fields.Item("idtblWijn").Value)
                LstKelderboek.Items(lv_item).SubItems.Add(rs.Fields.Item("wijnhuis").Value)
                LstKelderboek.Items(lv_item).SubItems.Add(rs.Fields.Item("Jaar").Value)
                LstKelderboek.Items(lv_item).SubItems.Add(rs.Fields.Item("TblLandnm").Value)
                LstKelderboek.Items(lv_item).SubItems.Add(rs.Fields.Item("tblstreek").Value)
                LstKelderboek.Items(lv_item).SubItems.Add(rs.Fields.Item("tblAppellationnm").Value)
                LstKelderboek.Items(lv_item).SubItems.Add(rs.Fields.Item("Aantal").Value)
                LstKelderboek.Items(lv_item).SubItems.Add(rs.Fields.Item("tblOpslagcol").Value)
can be simplified all the down to this:
With rs.Fields
    Me.ListView1.Items.Add(.Item("idtblWijn").Value.ToString()).SubItems.AddRange(New String() {.Item("wijnhuis").Value.ToString(),
                                                                                                .Item("Jaar").Value.ToString(),
                                                                                                .Item("TblLandnm").Value.ToString(),
                                                                                                .Item("tblstreek").Value.ToString(),
                                                                                                .Item("tblAppellationnm").Value.ToString(),
                                                                                                .Item("Aantal").Value.ToString(),
                                                                                                .Item("tblOpslagcol").Value.ToString()})
Emd With
Fix that and see what you end up with.
 
Back
Top