Hi and thanks in advance:
So I’m trying to pull a table from an SQL database and sort it on the “Date From” (DtFr) column, which represents the start of a lease. I thought it would be as easy as this:
Dim XmlReader As New XmlNodeReader(xmlDoc)
Dim ds As New DataSet
ds.ReadXml(XmlReader)
myDt = ds.Tables("Cam")
If myDt IsNot Nothing AndAlso myDt.Rows.Count > 0 Then
Dim dv As New DataView(myDt)
dv.Sort = "DtFr ASC"
myDt = dv.ToTable(False, "Id", "Name", "Amount", "Unpaid", "DtFr")
End If
But as you might have guessed this is not working (new user, can’t post images it’s just a picture of the dates out of order).

Could it be because at some point along the data retrieval it’s no longer of type Date and it’s a string or something? If I use simpler dates, like 1/1/2012, 1/2/2012, 1/3/2012 or 1/1/2012, 2/1/2012, 3/1/2012 etc. it sorts alright, but as you’ll notice above, if i have varied months, it sorts it out of order.
I’ve tried messing with the culture, thinking it could be that it’s sorting them on the months before the year to no avail… I’m at a loss, any help, ideas, or direction to something helpful would be great.
Much Appreciated,
Josh
EDIT:
You’re a genius, thanks so much. You were right about the string value. I added a data column called sortDtFr to the datatable and gave it System.DateTime as a datatype and iterated through the rows setting the new column’s value equal to the old one casted as a Date. Here’s the code for those interested:
ds.ReadXml(XmlReader)
myDt = ds.Tables("Cam")
myDt.Columns.Add(New DataColumn("sortDtFr", System.Type.GetType("System.DateTime")))
For Each row As DataRow In myDt.Rows
row("sortDtFr") = CDate(row("DtFr"))
Next
If myDt IsNot Nothing AndAlso myDt.Rows.Count > 0 Then
Dim dv As New DataView(myDt)
dv.Sort = "sortDtFr ASC"
myDt = dv.ToTable(False, "Id", "Name", "Amount", "Unpaid", "DtFr", "sortDtFr")
End If
The reason why your records are sorted in that way is because the datatype of that
DataColumnof yourDataTable(Cam) is System.String (by default). Try changing it to System.DateTime.