Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to sort a DateTime-column in DataView?

I have a gridview with some columns and I need to sort the gridview by a date-column. But I fail in sorting it correctly. This is the code that I use:

dt.DefaultView.Sort = "Meldingsdatum asc";
gvOutlookMeldingen.DataSource = dt;
gvOutlookMeldingen.DataBind();

Can someone help me, please.

like image 299
Tassisto Avatar asked Nov 23 '25 12:11

Tassisto


2 Answers

http://forums.asp.net/p/1267353/2393006.aspx

like image 87
sajoshi Avatar answered Nov 26 '25 03:11

sajoshi


DataView stores date or anything as string type. Thus when you sort it sort by string. To sort it as DateTime , you need to convert it to DateTime before adding any data as follows,

dt.Columns["Date"].DataType = Type.GetType("System.DateTime");

like image 37
Mohsin Shaikh Avatar answered Nov 26 '25 02:11

Mohsin Shaikh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!