|
-
Oct 5th, 2013, 02:39 PM
#1
Thread Starter
Member
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!
-
Oct 5th, 2013, 03:27 PM
#2
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
-
Oct 5th, 2013, 03:48 PM
#3
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
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Oct 5th, 2013, 03:52 PM
#4
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
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Oct 5th, 2013, 04:06 PM
#5
Thread Starter
Member
Re: Data Grid View won't sort rows by column DATE
 Originally Posted by .paul.
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 :/
-
Oct 5th, 2013, 04:14 PM
#6
Thread Starter
Member
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.!
-
Oct 5th, 2013, 04:28 PM
#7
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"
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|