Results 1 to 4 of 4

Thread: Datagrid Multiple Column Sort

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2004
    Location
    Manchester
    Posts
    266

    Cool Datagrid Multiple Column Sort

    Hello there,

    I have a table with two columns, date and time. I am using the following code to sort in descending date order.

    VB Code:
    1. If historydataGrid.Rows.Count > 0 Then
    2.             'if there are rows, sort by date (most recent first)
    3.             historydataGrid.Sort(historydataGrid.Columns(0), System.ComponentModel.ListSortDirection.Descending)
    4.         End If

    Where Columns(0) is the Date. However, how difficult is it for me to then sort secondarily by time. So that the grid will sort by most recent date and time.. Working down from the most recent time on the most recent date, and then moving backwards towards the oldest date with the oldest time.

    Cheers!

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,415

    Re: Datagrid Multiple Column Sort

    you'd need to use an IComparer:

    vb Code:
    1. Public Class comparer
    2.     Implements IComparer
    3.  
    4.     Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer Implements System.Collections.IComparer.Compare
    5.         Dim xRow As DataGridViewRow = DirectCast(x, DataGridViewRow)
    6.         Dim yRow As DataGridViewRow = DirectCast(y, DataGridViewRow)
    7.  
    8.         If Date.ParseExact(yRow.Cells("dateField").Value.ToString, "dd/MM/yyyy", CultureInfo.CurrentCulture) > Date.ParseExact(xRow.Cells("dateField").Value.ToString, "dd/MM/yyyy", CultureInfo.CurrentCulture) Then
    9.             Return 1
    10.         ElseIf Date.ParseExact(yRow.Cells("dateField").Value.ToString, "dd/MM/yyyy", CultureInfo.CurrentCulture) < Date.ParseExact(xRow.Cells("dateField").Value.ToString, "dd/MM/yyyy", CultureInfo.CurrentCulture) Then
    11.             Return -1
    12.         Else
    13.             Return TimeSpan.Parse(yRow.Cells("timeField").Value.ToString).CompareTo(TimeSpan.Parse(xRow.Cells("timeField").Value.ToString))
    14.         End If
    15.  
    16.     End Function
    17.  
    18. End Class

    add the comparer to your project + use it like this:

    vb Code:
    1. DataGridView1.Sort(New comparer)

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2004
    Location
    Manchester
    Posts
    266

    Re: Datagrid Multiple Column Sort

    Thanks Paul. I feared I would have to use this class. I really dislike it. Hey-ho. Thanks for the info. The forums won't let me give you any more rep at the moment. :/

  4. #4
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,713

    Re: Datagrid Multiple Column Sort

    Another idea is to sort on the data, assign your DataTable to a BindingSource which becomes the DataSource for the DataGridView. Set the sort property of the BindingSource to multiple columns i.e. BindingSource1.Sort = "Col1,Col2 desc"

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