Avatar of mcrmg
mcrmg
 asked on

GridView sort

Hi,

I have a GridView and would like to have sorted by Date Descending. The following is my code. But the result is as the  screenshot. How can I fix this? thank you


        FileInfo[] filesInDir = loanDir.GetFiles("*" + partialName + "*.*");

        List<FileDetail> files = new List<FileDetail>();
        foreach (FileInfo foundFile in filesInDir)
        {
            files.Add(new FileDetail(foundFile));
        }
        GridView1.DataSource = files;
        GridView1.DataSource = files.OrderByDescending(file => file.FileDate);
        GridView1.DataBind();
Annotation-2019-11-22-155617.gif
ASP.NET

Avatar of undefined
Last Comment
mcrmg

8/22/2022 - Mon
Miguel Oz

What exactly do you need to fix?
Date sorting is working by the way
mcrmg

ASKER
Thank you for the quick reply. Is there a way to sort as follow:

11/22/2019
...
11/18/2019
4/5/2019
....


thanks
Miguel Oz

Please provide class definition for FileDetail and how did you map it?
The code below:
 files.OrderByDescending(file => file.FileDate);

Open in new window

works only for  file.FileDate if this date is a DateTime type, are you displaying FileDate column in your screehshot?
You may be using another field, then you need to use the correct date field, also try
 files.OrderByDescending(d => d.Year).ThenByDescending(d => d.Month).ThenByDescending(d => d.Day);

Open in new window

I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
mcrmg

ASKER
please let me know if this is what you need. thanks

        DirectoryInfo loanDir = new DirectoryInfo(@"c:\inetpub\wwwroot\aaa\DocStorage");

        FileInfo[] filesInDir = loanDir.GetFiles("*" + partialName + "*.*");

        List<FileDetail> files = new List<FileDetail>();
        foreach (FileInfo foundFile in filesInDir)
        {
            files.Add(new FileDetail(foundFile));
        }
        GridView1.DataSource = files;
        GridView1.DataSource = files.OrderByDescending(file => file.FileDate);
        GridView1.DataBind();

Open in new window


    class FileDetail
    {
        public String FileName { get; set; }
        public String FileFullName { get; set; }
        public String FileDate { get; set; }

        public FileDetail(FileInfo foundFile)
        {
            this.FileFullName = foundFile.FullName;
            this.FileName = foundFile.Name;
            this.FileDate = foundFile.CreationTime.ToString();
        }
    }

Open in new window

ASKER CERTIFIED SOLUTION
Miguel Oz

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
mcrmg

ASKER
Thank you very much. This is exactly what I need.  thanks again.