Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Sort Datagrdview programmatically

Posted on 2016-10-11
19
45 Views
Last Modified: 2016-10-12
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

0
Comment
Question by:RIAS
  • 11
  • 8
19 Comments
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 41838448
is it possible that even if you see numbers in that column that the datatype of the column is really string (or varchar)?
0
 

Author Comment

by:RIAS
ID: 41838454
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
0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 41838461
they are 2 very different beast!
0
Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

 

Author Comment

by:RIAS
ID: 41838463
I know,but,need a way out
0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 41838475
can you pad your value with 0 on the left so it get sorted correctly?

0011a
0011b
0012a
0123a
1234a
0
 

Author Comment

by:RIAS
ID: 41838477
Can't do that Eric
0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 41838481
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.
0
 

Author Comment

by:RIAS
ID: 41838486
Eric, Is there no way we can have sort on Alphanumeric

Found this :

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

But,still not alphanumeric
0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 41838492
that method relies on Double.Parse which will not work in your scenario because 11a does not have a double representation
0
 

Author Comment

by:RIAS
ID: 41838494
Eric,I am trying to find a solution
0
 

Author Comment

by:RIAS
ID: 41838506
Eric,
Again Dates is an issue for sorting
0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 41838550
>>Again Dates is an issue for sorting

I thought it was for alphanumeric!
0
 

Author Comment

by:RIAS
ID: 41838668
There is another column for dates
0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 41838741
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).
0
 

Author Comment

by:RIAS
ID: 41838763
Its a txt column
0
 

Author Comment

by:RIAS
ID: 41838764
Ok
0
 

Author Comment

by:RIAS
ID: 41839999
Eric,
Now the column has all numbers but,it is declared as nvarchar in database.
Can you help me
0
 
LVL 70

Accepted Solution

by:
Éric Moreau earned 500 total points
ID: 41840031
The easiest way would be to cast your values in your select query. That way, you wouldn't have to write any code:
select field1, field2, cast(field3 as int) as field3 from yourtable ...

Open in new window

0
 

Author Closing Comment

by:RIAS
ID: 41840062
Worked like Charm ! Cheers!
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

809 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