13 Replies - 572 Views - Last Post: 09 March 2016 - 02:52 PM Rate Topic: -----

#1 JewishKing69  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 09-March 16

Listview Sorting (Specific Advice)

Posted 09 March 2016 - 02:05 PM

Hello,
I'm both new to programming and this forum, so I apologise for my incompetence, to begin with. I have an issue that I've attempted to solve for numerous hours and have not yet been able to; frustration is rife.

I am attempting to sort a listview (ascending/descending) by clicking on the column headers and, by use of code from a previous topic, have managed to do so for 1/3 columns (StudentName and NOT StudentClass, StudentScore and StudentAverageScore). If it is relevant, the data contained by the listview is sourced from a database. The code is as follows (I sincerely apologise for the verbosity; I don't know what is needed to aid me):

 Public Sub ShowMyRecords()

        Dim View As New DataView
        Dim dt As New DataTable
        Dim ds As New DataSet
        Dim myRow As DataRow
        ds.Tables.Add(dt)
        Dim da As New OleDbDataAdapter("Select * from RCSNT", Connect)
        da.Fill(dt)

        For Each myRow In dt.Rows
            lstvwDatabase.Items.Add(myRow.Item(0))
            lstvwDatabase.Items(lstvwDatabase.Items.Count - 1).SubItems.Add(myRow.Item(1))
            lstvwDatabase.Items(lstvwDatabase.Items.Count - 1).SubItems.Add(myRow.Item(2))
        Next


    End Sub
    Public Class ListViewColumnSorter
        Implements System.Collections.IComparer

        Private _ColumnIndex As Integer
        Private _SortingOrder As SortOrder
        Private ItemComparer As CaseInsensitiveComparer

        Public Sub New()
            _ColumnIndex = 0
            _SortingOrder = SortOrder.None
            ItemComparer = New CaseInsensitiveComparer()
        End Sub

        Public Property ColumnIndex() As Integer
            Get
                Return _ColumnIndex
            End Get
            Set(ByVal Value As Integer)
                _ColumnIndex = Value
            End Set
        End Property

        Public Property SortingOrder() As SortOrder
            Get
                Return _SortingOrder
            End Get
            Set(ByVal Value As SortOrder)
                _SortingOrder = Value
            End Set
        End Property

        Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer Implements IComparer.Compare
            Dim compareResult As Integer
            Dim LviStrX As String = DirectCast(x, ListViewItem).SubItems(_ColumnIndex).Text
            Dim LviStrY As String = DirectCast(y, ListViewItem).SubItems(_ColumnIndex).Text

            Dim numX, numY As Integer
            Dim dtX, dtY As Date

            If Integer.TryParse(LviStrX, numX) AndAlso Integer.TryParse(LviStrY, numY) Then
                compareResult = ItemComparer.Compare(numX, numY)
            ElseIf Date.TryParse(LviStrX, dtX) AndAlso Date.TryParse(LviStrY, dtY) Then
                compareResult = ItemComparer.Compare(dtX, dtY)
            Else
                compareResult = ItemComparer.Compare(LviStrX, LviStrY)
            End If

            If _SortingOrder = SortOrder.Ascending Then
                Return compareResult
            ElseIf _SortingOrder = SortOrder.Descending Then
                Return -compareResult
            Else
                Return 0
            End If
        End Function
    End Class

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    End Sub


    Private Sub lstvwDatabase_ColumnClick(ByVal sender As Object, ByVal e As System.Windows.Forms.ColumnClickEventArgs) Handles lstvwDatabase.ColumnClick
        Dim iSortOrder As SortOrder = CType(lstvwDatabase.Columns(e.Column).Tag, SortOrder)
        Dim lvcs As New ListViewColumnSorter
        If iSortOrder = SortOrder.Ascending Then
            lstvwDatabase.Columns(e.Column).Tag = SortOrder.Descending
            lvcs.SortingOrder = SortOrder.Descending
        Else
            lstvwDatabase.Columns(e.Column).Tag = SortOrder.Ascending
            lvcs.SortingOrder = SortOrder.Ascending
        End If
        lvcs.ColumnIndex = e.Column
        lstvwDatabase.ListViewItemSorter = lvcs
    End Sub

    Private Sub lstvwDatabase_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles lstvwDatabase.SelectedIndexChanged

    End Sub


An answer or solution would be tremendously appreciated!

Many thanks (and sorry),

JewishKing69

Is This A Good Question/Topic? 0
  • +

Replies To: Listview Sorting (Specific Advice)

#2 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 13483
  • View blog
  • Posts: 53,820
  • Joined: 12-June 08

Re: Listview Sorting (Specific Advice)

Posted 09 March 2016 - 02:07 PM

I am unclear - what's the issue with sorting?
Was This Post Helpful? 1
  • +
  • -

#3 JewishKing69  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 09-March 16

Re: Listview Sorting (Specific Advice)

Posted 09 March 2016 - 02:10 PM

View Postmodi123_1, on 09 March 2016 - 02:07 PM, said:

I am unclear - what's the issue with sorting?


Apologies, sir. The issue is apparently "InvalidArgument=Value of '1' is not valid for 'index'.
Parameter name: index", which means nothing to me. :( It will not sort any of the columns other than the first. If a simpler/alternative solution is possible, I wouldn't be opposed to restarting, despite how long this has taken me.

Thank you!

JewishKing69
Was This Post Helpful? 0
  • +
  • -

#4 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 13483
  • View blog
  • Posts: 53,820
  • Joined: 12-June 08

Re: Listview Sorting (Specific Advice)

Posted 09 March 2016 - 02:12 PM

Why not use an 'order by' in the SQL line? Order by any given list of columns and it's already done without fiddling with more code.
Was This Post Helpful? 1
  • +
  • -

#5 JewishKing69  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 09-March 16

Re: Listview Sorting (Specific Advice)

Posted 09 March 2016 - 02:19 PM

View Postmodi123_1, on 09 March 2016 - 02:12 PM, said:

Why not use an 'order by' in the SQL line? Order by any given list of columns and it's already done without fiddling with more code.


I'm probably too nooby for this forum. xD

Thank you, anyway.

JewishKing69
Was This Post Helpful? 0
  • +
  • -

#6 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 13483
  • View blog
  • Posts: 53,820
  • Joined: 12-June 08

Re: Listview Sorting (Specific Advice)

Posted 09 March 2016 - 02:22 PM

... or, you know, read up on the SQL docs on 'order by'.
https://msdn.microso...y/ms188385.aspx
Was This Post Helpful? 0
  • +
  • -

#7 JewishKing69  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 09-March 16

Re: Listview Sorting (Specific Advice)

Posted 09 March 2016 - 02:25 PM

View Postmodi123_1, on 09 March 2016 - 02:22 PM, said:

... or, you know, read up on the SQL docs on 'order by'.
https://msdn.microso...y/ms188385.aspx


Does this apply to access databases? I'm using one.

Sorry (again),

JewishKing69
Was This Post Helpful? 0
  • +
  • -

#8 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 13483
  • View blog
  • Posts: 53,820
  • Joined: 12-June 08

Re: Listview Sorting (Specific Advice)

Posted 09 March 2016 - 02:26 PM

You can find out yourself by searching terms like "access database order by".
Was This Post Helpful? 1
  • +
  • -

#9 JewishKing69  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 09-March 16

Re: Listview Sorting (Specific Advice)

Posted 09 March 2016 - 02:31 PM

View Postmodi123_1, on 09 March 2016 - 02:26 PM, said:

You can find out yourself by searching terms like "access database order by".


I'm utterly confused... I seem to have solved it, but I have no idea how...

Yay.

Many thanks!

JewishKing69
Was This Post Helpful? 0
  • +
  • -

#10 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 13483
  • View blog
  • Posts: 53,820
  • Joined: 12-June 08

Re: Listview Sorting (Specific Advice)

Posted 09 March 2016 - 02:35 PM

Seemed to have solved it but you don't know how? That is a problem.
Was This Post Helpful? 1
  • +
  • -

#11 JewishKing69  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 09-March 16

Re: Listview Sorting (Specific Advice)

Posted 09 March 2016 - 02:46 PM

View Postmodi123_1, on 09 March 2016 - 02:35 PM, said:

Seemed to have solved it but you don't know how? That is a problem.



Hmm, yes, you could say that. Haha. However, I have realised that the issue was implicitly caused by the default setting of "Ascending" in the listview's properties; perhaps this clashed with the attempted sorting.

You've been greatly helpful and inspired my sporadic solution. Thanks a lot!

JewishKing69
Was This Post Helpful? 0
  • +
  • -

#12 andrewsw  Icon User is online

  • the case is sol-ved
  • member icon

Reputation: 6374
  • View blog
  • Posts: 25,756
  • Joined: 12-December 12

Re: Listview Sorting (Specific Advice)

Posted 09 March 2016 - 02:48 PM

I'm not a whiz with ListViews but I would guess that the problem was here:
       For Each myRow In dt.Rows
           lstvwDatabase.Items.Add(myRow.Item(0))
           lstvwDatabase.Items(lstvwDatabase.Items.Count - 1).SubItems.Add(myRow.Item(1))
           lstvwDatabase.Items(lstvwDatabase.Items.Count - 1).SubItems.Add(myRow.Item(2))
       Next

I suspect that the sub-items aren't being correctly associated with the ListView's (main) items. See the example here, ListViewItem Constructor.

I could be completely off track though ;)

Quote

However, I have realised that the issue was implicitly caused by the default setting of "Ascending" in the listview's properties; perhaps this clashed with the attempted sorting.

Sounds like I am off track, never mind.
Was This Post Helpful? 1
  • +
  • -

#13 JewishKing69  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 09-March 16

Re: Listview Sorting (Specific Advice)

Posted 09 March 2016 - 02:50 PM

You were actually one of my original inspirations from this forum. :)

Thank you so much!

JewishKing69

This post has been edited by andrewsw: 09 March 2016 - 02:52 PM
Reason for edit:: Removed previous quote, just press REPLY

Was This Post Helpful? 0
  • +
  • -

#14 andrewsw  Icon User is online

  • the case is sol-ved
  • member icon

Reputation: 6374
  • View blog
  • Posts: 25,756
  • Joined: 12-December 12

Re: Listview Sorting (Specific Advice)

Posted 09 March 2016 - 02:52 PM

Oh good :) that's nice to hear.

(Note that there is a Reply button further down the page, without needing to quote the previous post in full.)
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1