Link to home
Start Free TrialLog in
Avatar of RIAS
RIASFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Sort Datagrdview programmatically

Hello,
How to sort the dgv columns programmatically.
Need to duplicate functionality as access for sorting.
Columns are of type integers/Text.

The code below does not quite work correctly for numbers.
Any suggestions

 Private Sub SortDgColumn(ByVal sender As Object, ByVal intColindex As Integer, ByVal DoSorting As Boolean)
        'Dosorting is flag to allow descending sorting on the grid columns this is used for Resetting or Selecting all fields as the datagridview should not sort if the selection column is not sorted.
        Dim int As Integer
        int = intColindex
        Me.Cursor = Cursors.WaitCursor
        Dim newColumn As DataGridViewColumn =
               sender.Columns(int)
        Dim oldColumn As DataGridViewColumn = sender.SortedColumn
        Dim direction As ListSortDirection
        'todo
        ' If oldColumn is null, then the DataGridView is not currently sorted. 
        If Not oldColumn Is Nothing Then
            ' Sort the same column again, reversing the SortOrder. 
            If oldColumn Is newColumn AndAlso sender.SortOrder =
                SortOrder.Ascending Then
                If DoSorting = True Then direction = ListSortDirection.Descending
            Else
                ' Sort a new column and remove the old SortGlyph. 
                direction = ListSortDirection.Ascending
                oldColumn.HeaderCell.SortGlyphDirection = SortOrder.None
            End If
        Else
            direction = ListSortDirection.Ascending
        End If
        ' Sort the selected column.
        Me.Cursor = Cursors.Default
        If IsNothing(sender.CurrentRow) Then Exit Sub
        sender.Sort(newColumn, direction)
        If TypeOf CType(sender, DataGridView).DataSource Is DataTable Then
            If direction = ListSortDirection.Ascending Then
                sender.datasource.DefaultView.Sort = newColumn.Name & "  ASC"
            Else
                If DoSorting = True Then sender.datasource.DefaultView.Sort = newColumn.Name & "  DESC"
            End If
        ElseIf TypeOf CType(sender, DataGridView).DataSource Is DataView Then
            Dim dv As New DataView
            Dim SortDataTable As New DataTable
            dv = sender.datasource
            sender.datasource = dv
            dv.Sort = newColumn.Name & "  ASC"
            If direction = ListSortDirection.Ascending Then
                dv.Sort = newColumn.Name & "  ASC"
            Else
                If DoSorting = True Then dv.Sort = newColumn.Name & "  DESC"
            End If
            SortDataTable = dv.ToTable
            sender.datasource = SortDataTable
        End If
    End Sub

Open in new window

Avatar of Éric Moreau
Éric Moreau
Flag of Canada image

is it possible that even if you see numbers in that column that the datatype of the column is really string (or varchar)?
Avatar of RIAS

ASKER

Yes, Eric thats the problem it is a varchar field with numbers and suffix as letter
Example:
11a
11b
12a

But,access can sort it perfectly so ,is there any way vb.net dgv does it
they are 2 very different beast!
Avatar of RIAS

ASKER

I know,but,need a way out
can you pad your value with 0 on the left so it get sorted correctly?

0011a
0011b
0012a
0123a
1234a
Avatar of RIAS

ASKER

Can't do that Eric
can you at least have that value in a hidden column at least to sort on it? You will need to add code so that when the user click on your initial column, to sort on the hidden column.
Avatar of RIAS

ASKER

Eric, Is there no way we can have sort on Alphanumeric

Found this :

https://www.experts-exchange.com/questions/21932003/DataGridView-Sorting.html?anchorAnswerId=17194545#a17194545

But,still not alphanumeric
that method relies on Double.Parse which will not work in your scenario because 11a does not have a double representation
Avatar of RIAS

ASKER

Eric,I am trying to find a solution
Avatar of RIAS

ASKER

Eric,
Again Dates is an issue for sorting
>>Again Dates is an issue for sorting

I thought it was for alphanumeric!
Avatar of RIAS

ASKER

There is another column for dates
is the date column also a text column or really a date column? if it is text, the link you have provided could help solving the issue (by replacing everything related to Double by DateTime).
Avatar of RIAS

ASKER

Its a txt column
Avatar of RIAS

ASKER

Ok
Avatar of RIAS

ASKER

Eric,
Now the column has all numbers but,it is declared as nvarchar in database.
Can you help me
ASKER CERTIFIED SOLUTION
Avatar of Éric Moreau
Éric Moreau
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of RIAS

ASKER

Worked like Charm ! Cheers!