JuValencia
JuValencia

Reputation: 312

ASP.NET GridView fail to sort a Date passed as string

Hi I am building a site which has a gridview with AutoGenerateColumns = True, the issue that I am having is the Sort condition using a dataview does not work correctly for example. If I have the following records already displayed on the gridview:

If I do the sorting this way:

Dim dt As DataTable = Me.GetDataTable(Me.GetQuery(Sentence, params))
Dim dv As DataView = dt.DefaultView
dv.Sort = SortExpression & " " & SortDirection
gv.DataSource = dv
gv.DataBind()

The result in the grid is :

So the problem it seems to be the sort is only taking in consideration part of the date only the first five characters instead of the whole string.

The values for SortExpression is the ColumnName for Date which in the datasource is CallDate and the SorDirection Value is ASC

The Me.GetDataTable(Me.GetQuery(Sentence,paramas)) is just a way to populate the datatable with the records from the Database.

Another thing, sadly I cannot change the format of the Date because is a business requirement to be presented that way, also I am not allow to use third party controls because the budget of the project. :-(

Thanks for the any idea in advance.

Upvotes: 0

Views: 1647

Answers (1)

JKhuang
JKhuang

Reputation: 1553

First I want to confirm that whether your Date is DateTime type, if yes,

please try to sample as below, hope these will give you a hand.

                Dim dt As DataTable = Me.GetDataTable(Me.GetQuery(Sentence, params))
                dt.DefaultView.Sort = "Date ASC" ''Sort with Asc.
                gv.DataSource = dt
                gv.DataBind()

Upvotes: 0

Related Questions