Click here to Skip to main content
15,126,501 members
Articles / Programming Languages / Visual Basic
Tip/Trick
Posted 4 Mar 2018

Stats

7.7K views
9 bookmarked

Datagridview with Multisort

Rate me:
Please Sign up or sign in to vote.
5.00/5 (3 votes)
4 Mar 2018CPOL
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)

Share

About the Author

MarioRainer
Software Developer
Germany Germany
No Biography provided

Comments and Discussions

 
SuggestionGreat! Pin
Daniel Leykauf10-Mar-18 3:35
MemberDaniel Leykauf10-Mar-18 3: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.

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.