I seem to have a strange problem where Listview is not displaying all of the data from the db. It displays the first field but everything after that is blank. I think it has something to do with NULL or EMPTY values if certain fields in the db. I would be grateful if someone could help me with this and have posted my code and screenshot for your consideration. Many thanks
![Name: listview_error2.png
Views: 129
Size: 14.4 KB]()
Code:
Sub filllistview()
Try
'creatconn()
oledbCnn.ConnectionString = My.Settings.storageConnectionString
oledbCnn.Open()
'drcount = Convert.ToInt32(dr("RowCount"))
sql = "Select TOP 100 * from Postings WHERE Customer = ? ORDER BY [Date]"
Debug.Print(sql)
Dim oledbCmd As OleDbCommand = New OleDbCommand(sql, oledbCnn)
oledbCmd.Parameters.AddWithValue("@p1", newvar)
Using dr = oledbCmd.ExecuteReader()
'clear items in the list before populating with new values
ListView1.Items.Clear()
While dr.Read()
ListView1.Items.Add((dr(0)).ToString()).UseItemStyleForSubItems = False
ListView1.Items(ListView1.Items.Count - 1).SubItems.Add(CDate(dr(4)).ToShortDateString())
ListView1.Items(ListView1.Items.Count - 1).SubItems.Add(dr(1).ToString())
ListView1.Items(ListView1.Items.Count - 1).SubItems.Add(dr(11).ToString())
ListView1.Items(ListView1.Items.Count - 1).SubItems.Add(dr(7).ToString())
With ListView1.Items(ListView1.Items.Count - 1).SubItems.Add(CDbl(dr(5)).ToString("C"))
If CDbl(dr(5)) < 0 Then
.ForeColor = Color.Red
.BackColor = Color.Gainsboro
'.Font = New Font(Font.FontFamily, Font.Size, FontStyle.Bold)
Else
.ForeColor = tmpColor
End If
End With
ListView1.Items(ListView1.Items.Count - 1).SubItems.Add(dr(14).ToString())
ListView1.Items(ListView1.Items.Count - 1).SubItems.Add(dr(3).ToString())
count += 1
End While
End Using
MessageBox.Show(CStr(ListView1.Items.Count - 1))
MessageBox.Show(count.ToString)
'If (count < 100) Then
' MessageBox.Show("less than 100")
'Else
' MessageBox.Show(count.ToString)
'End If
'autosize each column in listview
For Each col As ColumnHeader In ListView1.Columns
col.Width = -2
Next col
'refresh the list with new data
ListView1.Refresh()
'Enable the posting previous button
'btnNextPostings.Enabled = True
Catch ex As Exception
MessageBox.Show(ex.Message)
Finally
'dr.Close()
oledbCnn.Close()
End Try
'btnNextPostings.Enabled = ListView1.Items.Count > 100
gbxPostings.Text = "Client Postings for: " & "( " & newvar & " )" & " Total Records: " & count
If (ListView1.Items.Count < 100) Then
btnNextPostings.Enabled = False
btnNextPostings.Text = "No data to show"
Else
btnNextPostings.Enabled = True
btnNextPostings.Text = "Next " & CStr(ListView1.Items.Count - 1) & " Postings"
End If
'RESET COUNT TO 0
count = 0
End Sub