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
There may be a better way than this but here is what I would do.
First of all, unless I am not understanding the top part right, you want to make sure your listbox is empty before populating it.
So ...
VB Code:
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()
could be accompliehed with:
VB Code:
listTransactions.Items.Clear()
As far as the second part of your code, which seems to be the sorting part, I am not sure I understand what you are trying to sort. Are you taking the items and sorting them alphabetically? If so, you could just change the SQL statement to include an ORDER BY at the end of it.
And just as a side note, be sure to use Try...Catch...End Try in your code so that you don't get those unhandled errors. The error you showed was not much help from the part that I could see.
Clear() removes my columns too. I was hoping to avoid that.
I sort the columns of the listview. E.g. Just like Windows Explorer will sort columns by Name, Type and Size by clicking on the columns, by Ascending or Descending Order. I sort by the username at first (via the SQL), but the user may need to sort by Date or by Department or by ........
The sort changes something about listview.... and I do not know what.
Dim listviewitem As ListViewItem ' Used for creating ListView items.
' Create some ListView items consisting of first and last names.
listviewitem = New ListViewItem(CType(reader.GetValue(0), String))
' mrn
If IsDBNull(reader.GetValue(1)) Then
listviewitem.SubItems.Add("")
Else
listviewitem.SubItems.Add(reader.GetString(1))
End If
' patient surname
If IsDBNull(reader.GetValue(2)) Then
listviewitem.SubItems.Add("")
Else
listviewitem.SubItems.Add(reader.GetString(2))
End If
' user create
If IsDBNull(reader.GetValue(3)) Then
listviewitem.SubItems.Add("")
Else
listviewitem.SubItems.Add(reader.GetString(3))
End If
' user department
If IsDBNull(reader.GetValue(4)) Then
listviewitem.SubItems.Add("")
Else
listviewitem.SubItems.Add(reader.GetValue(4))
End If
' crate dttm
If IsDBNull(reader.GetValue(5)) Then
listviewitem.SubItems.Add("")
Else
listviewitem.SubItems.Add(reader.GetDateTime(5))
End If
' table name
If IsDBNull(reader.GetValue(6)) Then
listviewitem.SubItems.Add("")
Else
listviewitem.SubItems.Add(reader.GetString(6))
End If