• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1006
  • Last Modified:

How to sort an unbound DataGridView control in vb.net

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
0
mkarkoukli
Asked:
mkarkoukli
  • 3
  • 2
1 Solution
 
AvelanCommented:
Hey,

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
anywhere.

-Avelan
0
 
mkarkoukliAuthor Commented:
Here is the code that populates the datagridview


Try
            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
            DataGridViewTickets.AutoResizeColumns()

        Catch ex As Exception
            MessageBox.Show(ex.Message, ex.GetType.ToString)
        End Try
0
 
AvelanCommented:
What is your datasource?

Your datasource needs to support sorting.

-Avelan
0
 
mkarkoukliAuthor Commented:
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)
 
        Try
            connection.Open()
            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")))
                Else
                    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
                TicketList.Add(Ticket)
            Loop
            reader.Close()
 
        Catch ex As Exception
            Throw ex
        Finally
            connection.Close()
        End Try
        Return TicketList
    End Function

Open in new window

0
 
AvelanCommented:
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 :)

-Avelan
0
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

Featured Post

Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now