GridView sort

mcrmg
mcrmg used Ask the Experts™
on
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
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Miguel OzSenior Software Engineer
Top Expert 2009

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

Author

Commented:
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 OzSenior Software Engineer
Top Expert 2009

Commented:
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

OWASP: Forgery and Phishing

Learn the techniques to avoid forgery and phishing attacks and the types of attacks an application or network may face.

Author

Commented:
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

Senior Software Engineer
Top Expert 2009
Commented:
Notice that  FileDate is a string, thus you can not use for sorting purposes.
I suggest to to create an internal property  to keep the date time value:
class FileDetail
    {
        public String FileName { get; set; }
        public String FileFullName { get; set; }
        public String FileDate { get; set; }
        internal DateTime FileDateAsDateTime { get; set; }

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

Open in new window


and the data source code should change to
GridView1.DataSource = files.OrderByDescending(file => file.FileDateAsDateTime);

Open in new window

Author

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

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial