Results 1 to 14 of 14

Thread: ListView Sorting on Clicked Column

  1. #1

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

    ListView Sorting on Clicked Column

    Hi,

    I have implemented the code from the following url:
    Link to Sorting Code at Microsoft
    to sort listview columns dynamically.

    The pertinant piece of code is:
    ' Determine whther the type being comapared is a date type
    Try
    ' Parse the two objects into Datetime.
    Dim firstdate As System.DateTime = DateTime.Parse(CType(x, ListViewItem).SubItems(col).Text)
    Dim seconddate As System.DateTime = DateTime.Parse(CType(y, ListViewItem).SubItems(col).Text)

    ' Compare the two dates
    returnval = DateTime.Compare(firstdate, seconddate)
    ' If neither compared objects has a valid date format, compare it as a string
    Catch
    ' String Compare the two objects
    returnval = [String].Compare(CType(x, ListViewItem).SubItems(col).Text, CType(y, ListViewItem).SubItems(col).Text)
    End Try

    Trouble is, the data is some of my columns is null and causes the error:

    An unhandled exception of type 'System.ArgumentOutOfRangeException' occurred in system.windows.forms.dll

    Additional information: Specified argument was out of the range of valid values.

    Does anyone know how to make it work when there are nulls in the columns?

  2. #2

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

    I now believe the problem does not lie with nulls in the columns.

    I modified the example and nulls work ok.

    The problem I believe is due to the indexes of the items/columns not working/being set up correctly.

    I add items from the database in the following way:
    connPiMSDatabase.Open()
    reader = commSelectLocks.ExecuteReader

    Dim i As Integer = 0

    While reader.Read()

    Me.listTransactions.Items.Add(reader.GetValue(0))
    Me.listTransactions.Items(i).SubItems.Add(reader.GetValue(1))
    Me.listTransactions.Items(i).SubItems.Add(reader.GetValue(2))
    Me.listTransactions.Items(i).SubItems.Add(reader.GetDateTime(3))
    Me.listTransactions.Items(i).SubItems.Add(reader.GetValue(4))

    i = i + 1
    End While

    When I sort by the following column, it works ok, but when I click on any column I recieve the afore mentioned error.

    Any ideas?

  3. #3

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

  4. #4
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Whats the stacktrace when you get the error? What lines does it give the error on?

  5. #5

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

    Im running it in debug mode, there is no stacktrace, only the error that I have list previously. The messagebox has Break, Continue, Ignore (grayed out) and Help buttons.

    Of course the Help button does not provide anything useful.

  6. #6
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Put the code in a Try..catch block and then catch the exception and show the stacktrace. Maybe that will tell use which line is causing the trouble.

    VB Code:
    1. Try
    2.    'code here
    3. Catch ex as Exception
    4.    Msgbox(ex.StackTrace, ,ex.Message)
    5. End Try

  7. #7

    Thread Starter
    Member
    Join Date
    Dec 2002
    Location
    Newcastle, Australia
    Posts
    42
    Please see attached message box screen shot
    Attached Images Attached Images  

  8. #8
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Whats the code for line 319 of frmUnlockTool? You can turn line numbering on in the settings: Tools->Options->Text Editor->Basic->Line Numbers

  9. #9

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

    returnval = [String].Compare(CType(x, ListViewItem).SubItems(col).Text, CType(y, ListViewItem).SubItems(col).Text)

  10. #10
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    You should put a breakpoint there and then use Quick Watches to see which of those doesn't ahve any items or at least doesn't have the index you are looking for.

  11. #11

    Thread Starter
    Member
    Join Date
    Dec 2002
    Location
    Newcastle, Australia
    Posts
    42
    I have placed the line:

    MessageBox.Show(CType(x, ListViewItem).SubItems(col).Text)

    into the code.

    The error:" 1 is not valid value for 'index'", could indicate that the Subitems(1) does not exist?

    But the listbox has 5 columns.!!

    I am not sure how to proceed.

  12. #12

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

    The error is caused not by the fact that there is no data in the subitem, it is caused by the fact that there is NO subitem at all.

    Do you know where I can find code for creating the listview with 5 columns of data?

  13. #13
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    VB Code:
    1. Dim itm As New ListViewItem("1st Col or Item Text")
    2.         itm.SubItems.Add("2nd Col or 1st SubItem")
    3.         itm.SubItems.Add("3rd Col")
    4.         itm.SubItems.Add("4th Col")
    5.         itm.SubItems.Add("5th Col")
    6.         ListView1.Items.Add(itm)

  14. #14

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

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