473,383 Members | 1,742 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

VB.NET : Sorting Dates in DataGridView

Hi,

I'm using VB 2005 and I have a DataGridView that I am populating manually.

e.g.
Expand|Select|Wrap|Line Numbers
  1.   Private Sub List_Dates()
  2.  
  3.     dgvDates.Rows.Clear()
  4.  
  5.     Dim c As Integer
  6.     Dim Row() As String = New String(3) {}
  7.  
  8.      For c = 0 To WorkstationCount
  9.       Row(0) = Workstations(c)
  10.       Row(1) = FormatDateTime(DatesAssign(c), DateFormat.ShortDate)
  11.       Row(2) = FormatDateTime(DatesAgent(c), DateFormat.ShortDate)
  12.       dgvDates.Rows.Add(Row)
  13.     Next
  14.  
  15.   End Sub
The context is that I'm reading in a set of files (one for each workstation) with various data in that I am parsing, including two dates, then saving this data in various arrays before putting in DataGridViews to display.

Anyway, everything works fine except that when I click on the column header to sort the two dates columns, they sort as a string, not as a date.

I've seen various very similar questions, but none of them answer the question for me (I suppose primarily because I am not binding a data source).

What can I do to get these columns to sort as a date?

Thanks,

Steve
Nov 17 '07 #1
2 8644
Okay, never mind. Pretty simple solution in the end.
Expand|Select|Wrap|Line Numbers
  1. Private Sub dgvDates_SortCompare(ByVal sender As Object, ByVal e As DataGridViewSortCompareEventArgs) Handles dgvDates.SortCompare
  2.  
  3.     If e.Column.Index = 0 Then
  4.       e.SortResult = System.String.Compare(e.CellValue1, e.CellValue2)
  5.     Else
  6.       e.SortResult = System.DateTime.Compare(e.CellValue1, e.CellValue2)
  7.     End If
  8.  
  9.     e.Handled = True
  10.  
  11. End Sub
Nov 18 '07 #2
Stevelaw,

You are simply great. I Have been hunting this ans since 4 days. I was going to implement icomparable and all those stuff.
You have made it so simple.

i registered with the scripts just to leave a thankyou note to you.

Happy thanksgiving. You made my day!

Okay, never mind. Pretty simple solution in the end.

Private Sub dgvDates_SortCompare(ByVal sender As Object, ByVal e As DataGridViewSortCompareEventArgs) Handles dgvDates.SortCompare

If e.Column.Index = 0 Then
e.SortResult = System.String.Compare(e.CellValue1, e.CellValue2)
Else
e.SortResult = System.DateTime.Compare(e.CellValue1, e.CellValue2)
End If

e.Handled = True

End Sub
Nov 21 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: John Taylor | last post by:
I have a ListCtrl with 5 columns. The first 4 columns are either strings or integers, but the last column is a string in the format of MM-DD-YYYY. I searched google and also read over the...
22
by: mike | last post by:
If I had a date in the format "01-Jan-05" it does not sort properly with my sort routine: function compareDate(a,b) { var date_a = new Date(a); var date_b = new Date(b); if (date_a < date_b)...
3
by: amit.vasu | last post by:
Hi I have a datagridview control which I am binding to a datatable. Before binding to a datatable I am adding one row in the datatable for Total and populating data in that row at runtime. ...
3
by: joey.powell | last post by:
Does anyone know how to disable sorting in a datagridview? I mean, when a user clicks a column header, the rows should not reorder.
0
by: Gerrit | last post by:
Hello, I have searched with Google for hours, but I cannot find a good solution for sorting a Datagridview on a comboboxcolumn and than sorted on the displaymember instead of the value member. ...
0
by: mahesh.nimbalkar | last post by:
I want to use Multi column sorting in DataGridView. The scenario is like this: 1) DataGridView has two columns; Id and name 2) User clicks on Id column and DataGridView is sorted...
4
by: econobond007 | last post by:
I have to sort dates that are from a txt file. So readline is used. I need someone kind to help teach me or write it up. plz do message me or aim(<Screen Name Deleted by MODERATOR>) an example of...
1
by: Sreenath GV | last post by:
Hi, I want to get the old state (lets say datagridview default state) of grid after sorting is done. How can I achive this. Please suggest
0
by: peter gregory | last post by:
Getting incomplete sorting in datagridview whether clicking on the grid column header or using a sort method on an unbound grid sorts imcompletely. On some cols it appears to sort all rows except the...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.