Solved

Sorting In List View VB2005

Posted on 2010-11-16
4
245 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Flash (http://en.wikipedia.org/wiki/Adobe_Flash) has evolved over the years to where it has become a masterful tool for displaying content screen.  It has excellent layout placement, UI precision as well as rendering capabilities. This, along with t…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

839 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