Solved

Sorting In List View VB2005

Posted on 2010-11-16
4
244 Views
Last Modified: 2012-05-10
I have code designed to sort a list view based on which column head is clicked. It works fine for the first two columns - sorting by User Last Name or Customer Name - but none of the other ones work.

Since all of the data in the list view is text, I would understand why I might have to trick the system for sorting on the data or number values, but CampaignName, Starts and Ends are just other string data in the table. So I must have some additional work to do.

Thanks!
'The Click Event
Private Sub lvwCampaigns_ColumnClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ColumnClickEventArgs) Handles lvwCampaigns.ColumnClick
        Select Case e.Column
            Case Is = 0
                intSelectedSortOrder = 0 'Rep
            Case Is = 1
                intSelectedSortOrder = 1 'Client
            Case Is = 3
                intSelectedSortOrder = 2 'Campaign
            Case Is = 4
                intSelectedSortOrder = 3 'Entered
            Case Is = 5
                intSelectedSortOrder = 4 'Starts
            Case Is = 6
                intSelectedSortOrder = 5 'End
            Case Is = 7
                intSelectedSortOrder = 6 'Total
        End Select
        Me.LoadCampaignsListView()

    End Sub

'The Load Function
Public Sub LoadCampaignsListView()
        Dim strSQL As String
        Dim boolWhereIn As Boolean = False
        Dim objListViewItem As ListViewItem

        'Set Up The SQL For The List Based on the Control State on the Form
        strSQL = "SELECT Campaigns.RevLineID, ([Users]![Last] & ', ' " & _
                    " & Left([Users]![First],1) & '.') AS Rep, Accounts.Customer, " & _
                    " Campaigns.CampaignName, Campaigns.Entered, Campaigns.Starts, " & _
                    " Campaigns.Ends, Campaigns.TotalAmount " & _
                    " FROM (Users INNER JOIN Campaigns ON Users.UserID = Campaigns.UserID) " & _
                    " INNER JOIN Accounts ON Campaigns.AcctID = Accounts.CustomerID "

        If Me.ckClosedCampaigns.CheckState = CheckState.Unchecked Then
            'Excluding Inactive Campaigns
            strSQL = strSQL & "WHERE Campaigns.Inactive <> True "
            boolWhereIn = True
        End If

        If lngSelectedSalesRepID <> 0 Then
            'Filter on the Selected Sales Rep
            If boolWhereIn = True Then
                strSQL = strSQL & "AND Users.UserID = " & lngSelectedSalesRepID

            Else
                strSQL = strSQL & "WHERE Users.UserID = " & lngSelectedSalesRepID
                boolWhereIn = True
            End If
        End If

        If lngSelectedBrandSpecialistID <> 0 Then
            'Filter on the Selected Brand Specialist Rep
            If boolWhereIn = True Then
                strSQL = strSQL & "AND Campaigns.BrandSpecID = " & lngSelectedBrandSpecialistID
            Else
                strSQL = strSQL & "WHERE Campaigns.BrandSpecID = " & lngSelectedBrandSpecialistID
                boolWhereIn = True
            End If
        End If

        Select Case intSelectedSortOrder
            Case Is = 0
                strSQL = strSQL & " ORDER BY Users.Last"
            Case Is = 1
                strSQL = strSQL & " ORDER By Accounts.Customer "
            Case Is = 2
                strSQL = strSQL & " ORDER By Campaigns.CampaignName "
            Case Is = 3
                strSQL = strSQL & " ORDER By Campaigns.Entered "
            Case Is = 4
                strSQL = strSQL & " ORDER By Campaigns.Starts "
            Case Is = 5
                strSQL = strSQL & " ORDER By Campaigns.Ends "
            Case Is = 6
                strSQL = strSQL & " ORDER By Campaigns.TotalAmount "
        End Select


        'Initialize a new instance of the data access base class
        Using objCampaignList As New WDABase
            Try
                'Get the Campaign List - add criteria to this later
                objCampaignList.SQL = strSQL
                objCampaignList.InitializeCommand()
                objCampaignList.OpenConnection()
                objCampaignList.DataReader = objCampaignList.Command.ExecuteReader

                'Check for Rows
                If objCampaignList.DataReader.HasRows Then
                    'Clear the List
                    Me.lvwCampaigns.Items.Clear()

                    'Process All Rows
                    While objCampaignList.DataReader.Read
                        'Create a new ListViewItem
                        objListViewItem = New ListViewItem

                        'Add the data to the List View Item
                        objListViewItem.Tag = objCampaignList.DataReader.Item("RevLineID")
                        objListViewItem.Text = objCampaignList.DataReader.Item("Rep")
                        objListViewItem.SubItems.Add(objCampaignList.DataReader.Item("Customer"))
                        objListViewItem.SubItems.Add(objCampaignList.DataReader.Item("CampaignName"))
                        objListViewItem.SubItems.Add(Format(objCampaignList.DataReader.Item("Entered"), "d"))
                        objListViewItem.SubItems.Add(objCampaignList.DataReader.Item("Starts"))
                        objListViewItem.SubItems.Add(objCampaignList.DataReader.Item("Ends"))
                        objListViewItem.SubItems.Add(FormatCurrency(objCampaignList.DataReader.Item("TotalAmount"), 0))
                        Me.lvwCampaigns.Items.Add(objListViewItem)
                    End While

                End If
            Catch ExceptionErr As Exception
                MessageBox.Show(ExceptionErr.Message, "No Matching Records")
            End Try
        End Using

        'Cleanup
        objListViewItem = Nothing

    End Sub

Open in new window

0
Comment
Question by:Buck_Beasom
  • 2
  • 2
4 Comments
 
LVL 41

Accepted Solution

by:
ralmada earned 500 total points
ID: 34173778
>>but none of the other ones work. <<
What do you mean by this?

Your code looks ok at first glance, although you can simplify it like below

'The Click Event
Private Sub lvwCampaigns_ColumnClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ColumnClickEventArgs) Handles lvwCampaigns.ColumnClick
        Select Case e.Column
            Case 0
                intSelectedSortOrder = 2 'Rep
            Case 1
                intSelectedSortOrder = 3 'Client
            Case 3
                intSelectedSortOrder = 4 'Campaign
            Case 4
                intSelectedSortOrder = 5 'Entered
            Case 5
                intSelectedSortOrder = 6 'Starts
            Case 6
                intSelectedSortOrder = 7 'End
            Case 7
                intSelectedSortOrder = 8 'Total
        End Select
        Me.LoadCampaignsListView()

    End Sub

'The Load Function
Public Sub LoadCampaignsListView()
        Dim strSQL As String
        Dim boolWhereIn As Boolean = False
        Dim objListViewItem As ListViewItem

        'Set Up The SQL For The List Based on the Control State on the Form
        strSQL = "SELECT Campaigns.RevLineID, ([Users]![Last] & ', ' " & _
                    " & Left([Users]![First],1) & '.') AS Rep, Accounts.Customer, " & _
                    " Campaigns.CampaignName, Campaigns.Entered, Campaigns.Starts, " & _
                    " Campaigns.Ends, Campaigns.TotalAmount " & _
                    " FROM (Users INNER JOIN Campaigns ON Users.UserID = Campaigns.UserID) " & _
                    " INNER JOIN Accounts ON Campaigns.AcctID = Accounts.CustomerID "

        If Me.ckClosedCampaigns.CheckState = CheckState.Unchecked Then
            'Excluding Inactive Campaigns
            strSQL = strSQL & "WHERE Campaigns.Inactive <> True "
            boolWhereIn = True
        End If

        If lngSelectedSalesRepID <> 0 Then
            'Filter on the Selected Sales Rep
            If boolWhereIn = True Then
                strSQL = strSQL & "AND Users.UserID = " & lngSelectedSalesRepID

            Else
                strSQL = strSQL & "WHERE Users.UserID = " & lngSelectedSalesRepID
                boolWhereIn = True
            End If
        End If

        If lngSelectedBrandSpecialistID <> 0 Then
            'Filter on the Selected Brand Specialist Rep
            If boolWhereIn = True Then
                strSQL = strSQL & "AND Campaigns.BrandSpecID = " & lngSelectedBrandSpecialistID
            Else
                strSQL = strSQL & "WHERE Campaigns.BrandSpecID = " & lngSelectedBrandSpecialistID
                boolWhereIn = True
            End If
        End If

	'Sorting
	strSQL = strSQL & " ORDER BY " & cSTr(intSelectedSortOrder)


        'Initialize a new instance of the data access base class
        Using objCampaignList As New WDABase
            Try
                'Get the Campaign List - add criteria to this later
                objCampaignList.SQL = strSQL
                objCampaignList.InitializeCommand()
                objCampaignList.OpenConnection()
                objCampaignList.DataReader = objCampaignList.Command.ExecuteReader

                'Check for Rows
                If objCampaignList.DataReader.HasRows Then
                    'Clear the List
                    Me.lvwCampaigns.Items.Clear()

                    'Process All Rows
                    While objCampaignList.DataReader.Read
                        'Create a new ListViewItem
                        objListViewItem = New ListViewItem

                        'Add the data to the List View Item
                        objListViewItem.Tag = objCampaignList.DataReader.Item("RevLineID")
                        objListViewItem.Text = objCampaignList.DataReader.Item("Rep")
                        objListViewItem.SubItems.Add(objCampaignList.DataReader.Item("Customer"))
                        objListViewItem.SubItems.Add(objCampaignList.DataReader.Item("CampaignName"))
                        objListViewItem.SubItems.Add(Format(objCampaignList.DataReader.Item("Entered"), "d"))
                        objListViewItem.SubItems.Add(objCampaignList.DataReader.Item("Starts"))
                        objListViewItem.SubItems.Add(objCampaignList.DataReader.Item("Ends"))
                        objListViewItem.SubItems.Add(FormatCurrency(objCampaignList.DataReader.Item("TotalAmount"), 0))
                        Me.lvwCampaigns.Items.Add(objListViewItem)
                    End While

                End If
            Catch ExceptionErr As Exception
                MessageBox.Show(ExceptionErr.Message, "No Matching Records")
            End Try
        End Using

        'Cleanup
        objListViewItem = Nothing

    End Sub

Open in new window

0
 

Author Comment

by:Buck_Beasom
ID: 34175657
I will try your code. What I mean is, when I click on the headers for the first two columns (Rep and Client) the list view sorts the way I want. But when I click on the other headers, nothing changes.

Thanks. Will let you know how I make out.
0
 
LVL 41

Expert Comment

by:ralmada
ID: 34337192
Hi,

Any luck with my suggestion?
0
 

Author Closing Comment

by:Buck_Beasom
ID: 34368547
Thanks!
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It seems a simple enough task, yet I see repeated questions asking how to do it: how to pass data between two forms. In this article, I will show you the different mechanisms available for you to do just that. This article is directed towards the .N…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question