Results 1 to 5 of 5

Thread: Sorting data in ascending order in a datagridview

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jan 2008
    Location
    Essex, UK
    Posts
    149

    Unhappy Sorting data in ascending order in a datagridview

    I am having a few problems getting a column in a datagridview to sort its items correctly in ascending order. The column contains number (File size) data so i simply want it to list the files in smallest to biggest order (Acsending order) but this doesnt work. Below is a example of the code im using for testing:

    Code:
    DataGridView1.Columns.Add("", "Number:")
    DataGridView1.Rows.Add("1")
    DataGridView1.Rows.Add("2")
    DataGridView1.Rows.Add("3")
    DataGridView1.Rows.Add("4")
    DataGridView1.Rows.Add("51")
    DataGridView1.Rows.Add("22")
    DataGridView1.Rows.Add("6334")
    DataGridView1.Rows.Add("610")
    
    DataGridView1.Sort(DataGridView1.Columns(0), System.ComponentModel.ListSortDirection.Ascending)
    You would expect the above code to order the items in '1/2/3/4/22/51/610/6334' order when set to ascending order and reversed when in descending order. What actually happens is the items are displayed as follows:

    1/2/22/3/4/51/610/6334

    What am i doing wrong?. It appears it only sorts the numbers based on their 1st digit so any double digit or above numbers dont get sorted correctly.
    Development Enviroment: Visual Studio 2008, VB.NET

    Recommend Winspector, far better then SPY++
    http://www.windows-spy.com/

  2. #2
    Junior Member
    Join Date
    Sep 2008
    Location
    Finland
    Posts
    20

    Re: Sorting data in ascending order in a datagridview

    You are actually sorting strings, not numbers.

    One solution is to add numbers to grid:
    Code:
    DataGridView1.Rows.Add(1)
    DataGridView1.Rows.Add(2)
    DataGridView1.Rows.Add(3)
    instead of strings.

    The second solution is to provide your own sorting routine that implements ICompare interface. Here's a Q&D version for integers:
    Code:
    Option Explicit On
    Option Strict On
    Public Class CDataGridViewComparer
      Implements IComparer
    
      Private m_Col As Integer
      Private m_SortOrder As Integer
    
      Public Sub New()
        ' Initialize
    
      End Sub
    
      Public Sub New(ByVal column As Integer)
        '
        ' Initialize
        m_Col = column
    
      End Sub
    
      Public Sub New(ByVal column As Integer, ByVal SortOrder As Integer)
        '
        ' Initialize
        m_Col = column
        m_SortOrder = SortOrder
    
      End Sub
    
      Protected Overrides Sub Finalize()
        '
        ' Terminate
        MyBase.Finalize()
    
      End Sub
    
      Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer _
        Implements IComparer.Compare
        '
        ' Compare
        '
        Dim TempInt1 As Integer
        Dim TempInt2 As Integer
    
        Try
          If m_SortOrder = System.ComponentModel.ListSortDirection.Ascending Then
            If Integer.TryParse(CType(x, DataGridViewRow).Cells(m_Col).Value.ToString, TempInt1) AndAlso _
              Integer.TryParse(CType(y, DataGridViewRow).Cells(m_Col).Value.ToString, TempInt2) Then
              If CInt(TempInt1) < CInt(TempInt2) Then
                Return -1
              ElseIf CInt(TempInt1) > CInt(TempInt2) Then
                Return 1
              End If
            End If
          Else
            If Integer.TryParse(CType(x, DataGridViewRow).Cells(m_Col).Value.ToString, TempInt1) AndAlso _
              Integer.TryParse(CType(y, DataGridViewRow).Cells(m_Col).Value.ToString, TempInt2) Then
              If CInt(TempInt1) > CInt(TempInt2) Then
                Return -1
              ElseIf CInt(TempInt1) < CInt(TempInt2) Then
                Return 1
              End If
            End If
          End If
          Return 0
        Catch ex As Exception
          ' Handle exception
        End Try
    
      End Function
    
    End Class
    And you call it:
    Code:
    DataGridView1.Sort(New CDataGridViewComparer(0, System.ComponentModel.ListSortDirection.Ascending))
    If you need to, you can extend Compare function to handle dates etc.

    Teme64 @ Windows Developer Blog

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Jan 2008
    Location
    Essex, UK
    Posts
    149

    Re: Sorting data in ascending order in a datagridview

    Thanks for your help. As the job is just a little enhancement to my program i wanted to stick with a very simple solution to the problem. I have tried the below code and it appears the .add row command only works with strings. If you use the below code nothing appears in the datagridview. Its possible i am executing the command incorrectly but i cant find any other method on MSDN.

    Code:
    DataGridView1.Rows.Add(3)
    Ill give method 2 a go if i cant find a soloution to the above problem. Thank you for your assistance
    Development Enviroment: Visual Studio 2008, VB.NET

    Recommend Winspector, far better then SPY++
    http://www.windows-spy.com/

  4. #4
    Junior Member
    Join Date
    Sep 2008
    Location
    Finland
    Posts
    20

    Re: Sorting data in ascending order in a datagridview

    Code:
    DataGridView1.Rows.Add(3)
    actually adds three new rows, not an integer with value three. Sorry, my mistake The second solution should be fine.

    Teme64 @ Windows Developer Blog

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Sorting data in ascending order in a datagridview

    vb.net Code:
    1. DataGridView1.Rows(DataGridView1.Rows.Add()).Cells(0).Value = someInteger
    vb.net Code:
    1. DataGridView1.Rows.Add(New Object() {someInteger})
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width