The code below populates a listview from a recordset.
I can refresh the listview quite happily, but when I sort the code and then refresh the data, the first item is added to the listview, but an error is produced when the second loop is executed.
The sort has been implemented from code from MSDN. The article is entitled "Sorting Listview Items by Column Using Windows Forms" by Shannon Dunn
The error is shown in the attached gif.
Please can someone suggest what the problem is, hot to fix it, or recommend a better way of doing this?
Dim i As Integer = 0
With listTransactions
' If the box is already populated, remove the items
If .Items.Count > 0 Then
Dim j As Integer = .Items.Count - 1
While j >= 0
.Items.RemoveAt(j)
j -= 1
End While
End If
.BeginUpdate()
While reader.Read()
' aplok refno
.Items.Add(reader.GetValue(0))
' mrn
If IsDBNull(reader.GetValue(1)) Then
.Items(i).SubItems.Add("")
Else
.Items(i).SubItems.Add(reader.GetString(1))
End If
' patient surname
If IsDBNull(reader.GetValue(2)) Then
.Items(i).SubItems.Add("")
Else
.Items(i).SubItems.Add(reader.GetString(2))
End If
' user create
If IsDBNull(reader.GetValue(3)) Then
.Items(i).SubItems.Add("")
Else
.Items(i).SubItems.Add(reader.GetString(3))
End If
' user department
If IsDBNull(reader.GetValue(4)) Then
.Items(i).SubItems.Add("")
Else
.Items(i).SubItems.Add(reader.GetValue(4))
End If
' create dttm
If IsDBNull(reader.GetValue(5)) Then
.Items(i).SubItems.Add("")
Else
.Items(i).SubItems.Add(reader.GetDateTime(5))
End If
' table name
If IsDBNull(reader.GetValue(6)) Then
.Items(i).SubItems.Add("")
Else
.Items(i).SubItems.Add(reader.GetString(6))
End If
i = i + 1
End While