Results 1 to 7 of 7

Thread: Data Grid View won't sort rows by column DATE

  1. #1

    Thread Starter
    Member
    Join Date
    Feb 2013
    Location
    Spain
    Posts
    44

    Data Grid View won't sort rows by column DATE

    Hi everyone, as the title says, I got a Data Grid View that I want it to be sorted by the date of a certain column, but I can't do it.

    Explaining the problem:

    The DataGridView sorts, okay, but it sorts by the first value of a date, not of the entire value of the date (at least that's what i think). For example I got a date:

    05/10/13 with this format: dd/MM/yy. Correctly declared as DATE. And then If I add another row with another Date Value, like: 01/11/13, it sorts to the first position! (It couldn't because the month of the date is a later month!). If the date is in the same month it sorts right.

    Im guessing it's sorting by the first value of the date, taken as a string.

    What I've Done:

    - Edited CellStyle to Date format (DataGridViewCellStyle). No results.
    - I also tried changing Date format of dated to dd/MM/yyyy instead of dd/MM/yy. No results.

    I use the following code to Sort Rows (Columns(3)) is the column that I want the DataGridView be sorded by.

    Code:
    DataGridView1.Sort(DataGridView1.Columns(3), System.ComponentModel.ListSortDirection.Ascending)
    Any tips? Thanks so much!

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Data Grid View won't sort rows by column DATE

    Thread moved from the 'VB6 and Earlier' forum to the 'VB.Net' (VB2002 and later) forum

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

    Re: Data Grid View won't sort rows by column DATE

    I tried to re-create your problem, but in my test, the dgv sorted correctly:

    Code:
    Public Class Form1
    
        Dim r As New Random
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            For x As Integer = 1 To 50
                DataGridView1.Rows.Add(Now.AddDays(r.Next(0, 101))) 'these are dates i added
            Next
            DataGridView1.Columns(0).DefaultCellStyle.Format = "dd/MM/yyyy" 'this sets what part of the date string will be shown
        End Sub
    
    End Class
    your problem is that the dgv is treating the column values as strings, which sort alphabetically, + don't sort numbers or dates correctly

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

    Re: Data Grid View won't sort rows by column DATE

    when adding the values, you've added them as strings + not as dates, so the dgv sorts them as strings

  5. #5

    Thread Starter
    Member
    Join Date
    Feb 2013
    Location
    Spain
    Posts
    44

    Re: Data Grid View won't sort rows by column DATE

    Quote Originally Posted by .paul. View Post
    when adding the values, you've added them as strings + not as dates, so the dgv sorts them as strings
    Yeah I noticed now that:

    Code:
      DataGridView1.Rows.Add(New String() {TextBox2.Text, ComboBox2.Text, Format(Now, "dd/MM/yy"), Format(datevar, "dd/MM/yy"), " "})
    there is a "New String". But Removed it and still doesnt work :/

  6. #6

    Thread Starter
    Member
    Join Date
    Feb 2013
    Location
    Spain
    Posts
    44

    Re: Data Grid View won't sort rows by column DATE

    Okay, found the mistake:

    Code:
    DataGridView1.Rows.Add(New String() {TextBox2.Text, ComboBox2.Text, Format(Now, "dd/MM/yy"), Format(datevar, "dd/MM/yy"), " "})
    First removed the "New String()" and then added "CDate()" to the Formatted date, like this:

    Code:
    DataGridView1.Rows.Add(TextBox2.Text, ComboBox2.Text, Format(Now, "dd/MM/yy"), CDate(Format(datevar, "dd/MM/yy")), " ", " ")
    Thanks .paul.!

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

    Re: Data Grid View won't sort rows by column DATE

    that works, but what you're doing is taking datevar which is a date, formatting it as a string, then converting that string back into a date.
    it'd be better to just add datevar as it is to that column, + set the column's format property to "dd/MM/yy"

Tags for this Thread

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