I need to sort my records by date (month & year) as displayed on my asp.net page;
Any ideas / suggestions would be helpful.
This is the code I currently ha
The ASP.NET content that you have should be put in a Repeater
. The files
that you have should then be grouped by the month and date. So basically you will end up with a parent-child list. The parent which is the group of files will be bound to the Repeater
, and the children which are the files belonging to that group will be bound the the GridView
in the Repeater
.
The ASP.NET content would be something along this line. Take note the gvInvoiceList
DataSource
property is bound to InvoiceList
, a property of the group that I came up with (which you will see in the code below) that will have a list of files belonging to the group.
As for the code, I'm not fluent on using DataTable
to have the parent-child relationship needed for the ASP.NET structure that I used for my answer, but it should be easily doable using normal classes. And I'm also not fluent on using VB.NET, so excuse my example which will be using C#, which I guess you should able to convert it quite easily to VB.NET.
I'm using Linq
to do the grouping and an anonymous class to have the parent-child relationship for this, so the code is rather short.
repInvoiceGroups.DataSource = files
.GroupBy(f => f.LastWriteTime.ToString("yyyy-MM"))
.Select(g => new {
MonthYear = DateTime.ParseExact(g.Key, "yyyy-MM", CultureInfo.InvariantCulture),
InvoiceList = g.OrderByDescending(f => f.LastWriteTime) })
.OrderByDescending(o => o.MonthYear);
repInvoiceGroups.DataBind();
p/s: The code was written in a text editor and untested. Let me know if you face any errors. :)