Results 1 to 8 of 8

Thread: Listview Problem - Sort then Re-Populate

  1. #1

    Thread Starter
    Member
    Join Date
    Dec 2002
    Location
    Newcastle, Australia
    Posts
    42

    Listview Problem - Sort then Re-Populate

    Hi,

    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

    .EndUpdate()
    End With
    Attached Images Attached Images  

  2. #2
    Member EagleEye's Avatar
    Join Date
    May 2002
    Location
    South Carolina, USA
    Posts
    43
    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:
    1. Dim i As Integer = 0
    2. With listTransactions
    3. ' If the box is already populated, remove the items
    4. If .Items.Count > 0 Then
    5. Dim j As Integer = .Items.Count - 1
    6. While j >= 0
    7. .Items.RemoveAt(j)
    8. j -= 1
    9. End While
    10. End If
    11. .BeginUpdate()
    could be accompliehed with:
    VB Code:
    1. 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.
    Eagle Eye

    "Programming is easy ... when you are done."

  3. #3

    Thread Starter
    Member
    Join Date
    Dec 2002
    Location
    Newcastle, Australia
    Posts
    42
    Hi,

    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.

  4. #4
    Member EagleEye's Avatar
    Join Date
    May 2002
    Location
    South Carolina, USA
    Posts
    43
    Sorry ... I misunderstood.
    Eagle Eye

    "Programming is easy ... when you are done."

  5. #5

  6. #6

    Thread Starter
    Member
    Join Date
    Dec 2002
    Location
    Newcastle, Australia
    Posts
    42
    Hmmm,

    As far as I can tell the problem lies in the addition of the items to the list view. The addition of the second item after sorting is the problem.

    But I don't know why.

  7. #7

    Thread Starter
    Member
    Join Date
    Dec 2002
    Location
    Newcastle, Australia
    Posts
    42
    Error Message
    Attached Images Attached Images  

  8. #8

    Thread Starter
    Member
    Join Date
    Dec 2002
    Location
    Newcastle, Australia
    Posts
    42
    Problem Resolved.

    I changed the list view item add code to be:


    While reader.Read()

    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

    .Items.Add(listviewitem)

    End While

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width