0

I'm trying to display items from a MS Access database into a listview but for some odd reason which i can't find out why, my listview does show all the records but it leaves some recods blank

enter image description here

Here's the sub i use to load my items:

    Public Sub LoadItems()

    If tcOverzicht.SelectedIndex = 0 Then
        Dim strQuery As String = "SELECT tblItems.itemID, tblItems.Referentie, tblType.Type, tblItems.Stock, tblItems.Barcode, tblItems.Omschrijving, tblItems.Merk, tblItems.Opmerking " & _
                                 "FROM tblItems INNER JOIN tblType ON tblItems.typeID = tblType.typeID WHERE tblItems.typeID=" & intType & ";"
        Dim da As New OleDbDataAdapter
        Dim cmd As New OleDbCommand(strQuery, cnConnectie)
        Dim TABLE As New DataTable

        With da
            .SelectCommand = cmd
            .Fill(TABLE)
        End With

        'Listview leeg maken
        lvItems.Items.Clear()

        'Listview vullen met de gegevens uit de DB
        For i As Integer = 0 To TABLE.Rows.Count - 1
            With lvItems
                .Items.Add(TABLE.Rows(i)("itemID"))
                With .Items(.Items.Count - 1).SubItems
                    .Add(TABLE.Rows(i)("Referentie"))
                    .Add(TABLE.Rows(i)("Type"))
                    .Add(TABLE.Rows(i)("Stock"))
                    .Add(TABLE.Rows(i)("Barcode"))
                    .Add(TABLE.Rows(i)("Omschrijving"))
                    .Add(TABLE.Rows(i)("Merk"))
                    .Add(TABLE.Rows(i)("Opmerking"))
                End With
            End With
        Next
    End If

    If tcItems.SelectedIndex = 0 Then
        Dim strQuery As String = "SELECT tblItems.itemID, tblItems.Referentie, tblType.Type, tblItems.Stock, tblLeveranciers.Leverancier, tblItems.Barcode, tblItems.MinStuks,tblItems.Omschrijving, tblItems.Merk,  tblItems.Opmerking " & _
                                 "FROM (tblItems INNER JOIN tblLeveranciers ON tblItems.leveranciersID = tblLeveranciers.leveranciersID) INNER JOIN tblType ON tblItems.typeID = tblType.typeID;"

        Dim da As New OleDbDataAdapter
        Dim cmd As New OleDbCommand(strQuery, cnConnectie)
        Dim TABLE As New DataTable

        With da
            .SelectCommand = cmd
            .Fill(TABLE)
        End With

        'Listview leeg maken
        lvArtikel.Items.Clear()

        'Listview vullen met de gegevens uit de DB
        For i As Integer = 0 To TABLE.Rows.Count - 1
            With lvArtikel
                .Items.Add(TABLE.Rows(i)("itemID"))
                With .Items(.Items.Count - 1).SubItems
                    .Add(TABLE.Rows(i)("Referentie"))
                    .Add(TABLE.Rows(i)("Type"))
                    .Add(TABLE.Rows(i)("Stock"))
                    .Add(TABLE.Rows(i)("Leverancier"))
                    .Add(TABLE.Rows(i)("Barcode"))
                    .Add(TABLE.Rows(i)("MinStuks"))
                    .Add(TABLE.Rows(i)("Omschrijving"))
                    .Add(TABLE.Rows(i)("Merk"))
                    .Add(TABLE.Rows(i)("Opmerking"))
                End With
            End With
        Next
    End If

End Sub

I'm totally lost.. maybe some of you knows why?

Greetings

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
tooz
  • 53
  • 1
  • 6
  • Are the blank items random, every second line, all together ? – grahamj42 Mar 29 '13 at 10:09
  • Well they are not random but its not every second line either. – tooz Mar 29 '13 at 10:11
  • OK, so the results are repeatable? Are there any null fields in your data which might explain the problem? – grahamj42 Mar 29 '13 at 10:16
  • I also thought that it was something with null fields, but every field in my DB is filled in. I can share the source code if that might help. – tooz Mar 29 '13 at 10:20
  • I suppose you've checked that TABLE is correctly filled. I'm not familiar with the Listview control (I'm more knowledgeable in VBA), so the only other point I can suggest is that instead of `With .Items(.Items.Count - 1).SubItems` you use `With .Items(i).SubItems` - you know which item you've just added. – grahamj42 Mar 29 '13 at 10:26
  • Yes TABLE is correctly filled in and I've tried your suggestion but unfortunately that didn't help. – tooz Mar 29 '13 at 10:30
  • I think, it was normal behavior, please check the lvArtikel.items.count and compare with table row count. May be, It happened because you use gridlines property to true, and the blank grids not always record in this case. It just my opinion. – user11982798 Sep 02 '19 at 04:31

1 Answers1

0

You can use this code after you add columns manually to listView.

For i As Integer = 0 To TABLE.Rows.Count - 1
    Dim str(7) As String
    Dim itm As ListViewItem
    str(0) = TABLE.Rows(i)("itemID")
    str(1) = TABLE.Rows(i)("Referentie")
    str(2) = TABLE.Rows(i)("Type")
    str(3) = TABLE.Rows(i)("Stock")
    str(4) = TABLE.Rows(i)("Barcode")
    str(5) = TABLE.Rows(i)("Omschrijving")
    str(6) = TABLE.Rows(i)("Merk")
    str(7) = TABLE.Rows(i)("Opmerking")
    itm = New ListViewItem(str)
    listView.Items.Add(itm)
Next
Murat
  • 1
  • 2