How to sort an unbound DataGridView control in vb.net

Posted on 2009-04-07
Medium Priority
Last Modified: 2013-12-26
I have a vb.net form with datagridview control that bound programmatically. The data source for this control is coming from a SQL query, I want to take advantage of the sorting capabilities that are part of the datagridview. How can i accomplish the column sorting. I want to give the user the ability to sort by clicking on any of the columns.

Here is how I'm supplying the datasource:

 TicketList = TicketsDB.GetTickets("Open")

            DataGridViewTickets.DataSource = TicketList
Question by:mkarkoukli
  • 3
  • 2

Expert Comment

ID: 24092595

Without seeing your project, there's one suggestion that i'll make.
Check the SortMode property of each column in your datagridview and
make it Automatic. Although in a testproject i made, all of the columns are
set to Automatic by default and sorting works without any changes to


Author Comment

ID: 24098666
Here is the code that populates the datagridview

            TicketList = TicketsDB.GetTickets("Open")

            DataGridViewTickets.DataSource = TicketList

            DataGridViewTickets.Columns("TicketId").Visible = False
            DataGridViewTickets.Columns("User").Visible = False
            DataGridViewTickets.Columns("Category").Visible = False
            DataGridViewTickets.Columns("Product").Visible = False
            DataGridViewTickets.Columns("Status").Visible = False
            DataGridViewTickets.Columns("TicketDescription").Visible = False
            DataGridViewTickets.Columns("fkDepartmentID").Visible = False
            DataGridViewTickets.Columns("AssignedTo").Visible = False
            DataGridViewTickets.Columns("MarkForDeletion").Visible = False

            DataGridViewTickets.Columns("TicketNumber").HeaderText = "Ticket Number"
            DataGridViewTickets.Columns("DateEntered").HeaderText = "Date Created"
            DataGridViewTickets.Columns("UserFullName").HeaderText = "Client Name"
            DataGridViewTickets.Columns("TicketAging").HeaderText = "Aging"
            DataGridViewTickets.Columns("Priority").HeaderText = "Priority"
            DataGridViewTickets.Columns("StatusDesc").HeaderText = "Status"
            DataGridViewTickets.Columns("AssignedToName").HeaderText = "Assigned To"
            DataGridViewTickets.Columns("DateClosed").HeaderText = "Date Closed"
            DataGridViewTickets.Columns("TicketType").HeaderText = "Ticket Type"
            DataGridViewTickets.Columns("Department").HeaderText = "Department"
            DataGridViewTickets.Columns("CategoryName").HeaderText = "Category"
            DataGridViewTickets.Columns("ProductServiceName").HeaderText = "Product/Service"

            DataGridViewTickets.Columns("TicketNumber").DisplayIndex = 1
            DataGridViewTickets.Columns("StatusDesc").DisplayIndex = 2
            DataGridViewTickets.Columns("Priority").DisplayIndex = 3
            DataGridViewTickets.Columns("DateEntered").DisplayIndex = 4
            DataGridViewTickets.Columns("DateClosed").DisplayIndex = 5
            DataGridViewTickets.Columns("TicketAging").DisplayIndex = 6
            DataGridViewTickets.Columns("UserFullName").DisplayIndex = 7
            DataGridViewTickets.Columns("Department").DisplayIndex = 8
            DataGridViewTickets.Columns("AssignedToName").DisplayIndex = 9
            DataGridViewTickets.Columns("TicketType").DisplayIndex = 10
            DataGridViewTickets.Columns("CategoryName").DisplayIndex = 11
            DataGridViewTickets.Columns("ProductServiceName").DisplayIndex = 12

            'DataGridViewTickets.ReadOnly = True
            DataGridViewTickets.AllowUserToResizeColumns = False
            DataGridViewTickets.SelectionMode = DataGridViewSelectionMode.FullRowSelect

        Catch ex As Exception
            MessageBox.Show(ex.Message, ex.GetType.ToString)
        End Try

Expert Comment

ID: 24099250
What is your datasource?

Your datasource needs to support sorting.


Author Comment

ID: 24099347
My datasource is a list  "List(Of Tickets)"

Public Shared Function GetTickets(ByVal strMode As String) As List(Of Tickets)
        Dim TicketList As New List(Of Tickets)
        Dim connection As SqlConnection = DBConnection.GetConnection(True)
        Dim SQL As String = ""
        Const closed As Integer = 5
        If strMode = "Open" Then
            SQL = "SELECT tblTicketsQueue.pkTicketID, tblTicketsQueue.DateEntered, tblUsers.UserFirstName, " _
                      & "tblUsers.UserLastName, tblUsers_1.UserFirstName AS AFirstName, tblUsers_1.UserLastName AS ALastName, tblDepartment.DepartmentName, " _
                      & "tblTicketsQueue.TicketNumber, " _
                      & "tblTicketsQueue.TicketPriority, tblTicketsQueue.TicketType, tblCategory.CategoryDesc, " _
                      & "tblProductsAndServices.ProductsAndServicesDesc, tblTicketsQueue.TicketDescription, " _
                      & "tblTicketsQueue.CloseDate, tblStatus.StatusDesc, tblTicketsQueue.fkUser, tblTicketsQueue.fkCategory, " _
                      & "tblTicketsQueue.fkProduct, tblTicketsQueue.fkStatus, tblTicketsQueue.AssignedTo, tblUsers.fkDepartmentID " _
                & "FROM tblTicketsQueue INNER JOIN " _
                      & "tblStatus ON tblTicketsQueue.fkStatus = tblStatus.pkStatusID INNER JOIN " _
                      & "tblUsers ON tblTicketsQueue.fkUser = tblUsers.pkUserID INNER JOIN " _
                      & "tblCategory ON tblTicketsQueue.fkCategory = tblCategory.pkCategoryID INNER JOIN " _
                      & "tblDepartment ON tblUsers.fkDepartmentID = tblDepartment.pkDepartmentID INNER JOIN " _
                      & "tblProductsAndServices ON tblTicketsQueue.fkProduct = tblProductsAndServices.pkProductAndServices LEFT OUTER JOIN " _
                      & "tblUsers AS tblUsers_1 ON tblTicketsQueue.AssignedTo = tblUsers_1.pkUserID " _
                & "WHERE tblTicketsQueue.fkStatus NOT LIKE " & closed & " AND tblTicketsQueue.MarkForDeletion NOT LIKE 1 " _
                & "ORDER BY pkTicketID DESC"
        End If
        Dim selectCommand As New SqlCommand(SQL, connection)
            Dim reader As SqlDataReader
            reader = selectCommand.ExecuteReader()
            Dim i As Integer
            i = reader.RecordsAffected
            Dim Ticket As Tickets
            Do While reader.Read
                Ticket = New Tickets
                Ticket.TicketId = CLng(reader("pkTicketID"))
                Ticket.DateEntered = CDate(reader("DateEntered"))
                If Not IsDBNull(reader("CloseDate")) Then
                    Ticket.TicketAging = DateDiff(DateInterval.DayOfYear, CDate(reader("DateEntered")), CDate(reader("CloseDate")))
                    Ticket.TicketAging = DateDiff(DateInterval.DayOfYear, CDate(reader("DateEntered")), Now)
                End If
                Ticket.TicketNumber = reader("TicketNumber").ToString
                Ticket.UserFullName = reader("UserFirstName").ToString & " " & reader("UserLastName").ToString
                Ticket.AssignedToName = reader("AFirstName").ToString & " " & reader("ALastName").ToString
                Ticket.Department = reader("DepartmentName").ToString
                Ticket.Priority = reader("TicketPriority").ToString
                Ticket.TicketType = reader("TicketType").ToString
                Ticket.CategoryName = reader("CategoryDesc").ToString
                Ticket.ProductServiceName = reader("ProductsAndServicesDesc").ToString
                Ticket.StatusDesc = reader("StatusDesc").ToString
                Ticket.User = CInt(reader("fkUser"))
                Ticket.Category = CInt(reader("fkCategory"))
                Ticket.Product = CLng(reader("fkProduct"))
                Ticket.Status = CInt(reader("fkStatus"))
                Ticket.fkDepartmentID = CInt(reader("fkDepartmentID"))
                Ticket.TicketDescription = reader("TicketDescription").ToString
                If Not IsDBNull(reader("CloseDate")) Then
                    Ticket.DateClosed = CDate(reader("CloseDate"))
                End If
                If Not IsDBNull(reader("AssignedTo")) Then
                    Ticket.AssignedTo = CLng(reader("AssignedTo"))
                End If
        Catch ex As Exception
            Throw ex
        End Try
        Return TicketList
    End Function

Open in new window


Accepted Solution

Avelan earned 1500 total points
ID: 24099568
Ok, you will need to implement sorting in a custom list.

I won't paste any links since there were too many of them,
just put 'sortable bindinglist' in google and you'll probably find
your answer quicker that way :)


Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Suggested Courses

624 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