Link to home
Start Free TrialLog in
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
Avatar of Miguel Oz
Miguel Oz
Flag of Australia image

What exactly do you need to fix?
Date sorting is working by the way
Avatar of mcrmg
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
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

Avatar of 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
Avatar of Miguel Oz
Miguel Oz
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mcrmg

ASKER

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