Click here to Skip to main content
15,881,709 members
Articles / Programming Languages / Visual Basic
Tip/Trick

Datagridview with Multisort

Rate me:
Please Sign up or sign in to vote.
5.00/5 (3 votes)
4 Mar 2018CPOL 9.4K   9   2
Here is a simple way to sort the datagridview by Multi-Columns

Introduction

This tip shows you how to sort the datagridview by any column.

Use the ColumnHeaderMouseClick event to sort a BindingSource.

Image 1

Using the Code

VB.NET
If Control.ModifierKeys = Keys.Control Then
    If dataSource.Sort.Contains(DataGridView1.Columns(e.ColumnIndex).Name) Then
        If dataSource.Sort.Contains(DataGridView1.Columns(e.ColumnIndex).Name + " ASC") Then
            dataSource.Sort = dataSource.Sort.Replace(DataGridView1.Columns(e.ColumnIndex).Name + _
            " ASC", DataGridView1.Columns(e.ColumnIndex).Name + " DESC")
        Else
            dataSource.Sort = dataSource.Sort.Replace(DataGridView1.Columns(e.ColumnIndex).Name + _
            " DESC", DataGridView1.Columns(e.ColumnIndex).Name + " ASC")
        End If
    Else
        dataSource.Sort += ", " & DataGridView1.Columns(e.ColumnIndex).Name + " ASC"
    End If
    
    Dim allSort() As String = Split(dataSource.Sort, ", ")
    For Each s As String In allSort
        Dim iSortOrder() As String = Split(s, " ")
        Dim cName As String = s.Replace(" " + iSortOrder(iSortOrder.Length - 1), "")
        Select Case iSortOrder(iSortOrder.Length - 1)
            Case "ASC"
                DataGridView1.Columns(cName).HeaderCell.SortGlyphDirection = SortOrder.Ascending
            Case "DESC"
                DataGridView1.Columns(cName).HeaderCell.SortGlyphDirection = SortOrder.Descending
        End Select
    Next
Else
    If dataSource.Sort.Contains(DataGridView1.Columns(e.ColumnIndex).Name + " DESC") Then
        dataSource.Sort = DataGridView1.Columns(e.ColumnIndex).Name + " ASC"
    Else
        dataSource.Sort = DataGridView1.Columns(e.ColumnIndex).Name + " DESC"
    End If
End If

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Germany Germany
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
SuggestionGreat! Pin
Daniel Leykauf10-Mar-18 2:35
Daniel Leykauf10-Mar-18 2:35 
Great idea Mario, but I would suggest to use 'DataPropertyName' instead of 'Name' (to ensure column is part of datasource) and to check if clicked column is sortable.
So this part of code would I place in front of your code:
        Dim c As DataGridViewColumn = Columns(e.ColumnIndex)
        If DataSource Is Nothing OrElse _
                Not DataSource.GetType Is GetType(BindingSource) OrElse _
                c.SortMode = DataGridViewColumnSortMode.NotSortable OrElse _
                String.IsNullOrEmpty(c.DataPropertyName) Then
            MyBase.OnColumnHeaderMouseClick(e)
            Return
        End If

... Your code ...

With this modifications you can also use unbound columns (i.e. buttons) in your bound datagridview. Furthermore it ensures that underlying datasource is a bindingscoure.

modified 10-Mar-18 9:28am.

GeneralRe: Great! Pin
oscarmothafucka23-Mar-22 13:35
oscarmothafucka23-Mar-22 13:35 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.