I've written some code to populate a listview using 3 fields from a database. When creating the listview, I named the individual columns for appearance purposes, (Account No, Company Name and Name). Those columns are being populated appropriately as expected. However data column names are being shown in column 4 5 and 6, but with no data. What in my code is causing this? Any suggestions on how to fix this?

My code is as follows:

Private Sub tbxCompanyName_Leave(sender As Object, e As EventArgs) Handles tbxCompanyName.Leave
    lvwSearchCustomer.Visible = True
    Me.lvwSearchCustomer.View = View.Details
    Me.lvwSearchCustomer.GridLines = True
    Dim strQ As String = String.Empty
    Dim datasource As String = "Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\Don\Documents\Visual Studio 2019\SalesForm\DWHRPT.mdf;Integrated Security=True"
    conn = New SqlConnection(datasource)
    Dim searchID As String = tbxCompanyName.Text

            FROM CUSTREC 
            WHERE CUST_COMPANY_NAME LIKE '" & searchID & "%' OR CUST_LAST_NAME LIKE '" & searchID & "%'"

    cmd = New SqlCommand(strQ, conn)
    da = New SqlDataAdapter(cmd)
    ds = New DataSet
    da.Fill(ds, "Tables")
    Dim i As Integer = 0
    Dim j As Integer = 0
    ' adding the columns in ListView
    For i = 0 To ds.Tables(0).Columns.Count - 1
    'Now adding the Items in Listview
    For i = 0 To ds.Tables(0).Rows.Count - 1
        For j = 0 To ds.Tables(0).Columns.Count - 1
            itemcoll(j) = ds.Tables(0).Rows(i)(j).ToString()
        Dim lvi As New ListViewItem(itemcoll)

End Sub

In advance, thanks for your assistance.


Recommended Answers

One question: where do you define itemcoll?

Jump to Post

All 2 Replies

FYI..... In attempting to fix this, I commented out a few lines to make it more visual. I failed to remove the comment marks before posting this.

I hope this clears any questions you might have.


One question: where do you define itemcoll?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.20 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.