Solved

Need help in understanding how to sort BindingList

Posted on 2012-03-19
5
1,052 Views
Last Modified: 2012-03-19
I'm am trying to understand how I can sort a binding list.  I used a reverse function that doesn't work.  I'm surprised that c# doesn't have a method already built to sort a list.  Here is how my class looks:
public class FieldsToMonitor
{
    public int ID { get; set; }
    public DateTime ChangedDate { get; set; }
}

Public CreateList
{
  static BindingList<FieldsToMonitor> FieldsToMonitorList = new BindingList<FieldsToMonitor>();
  ... populate list 
  FieldsToMonitorList.Reverse<FieldsToMonitor>();
}

Open in new window



the FieldsToMonitorList.Reverse doesn't seem to work.  I'm probably using it wrong.  All I want to do is sort ChangedDate descending.  Thanks for the assist.

Nick
0
Comment
Question by:countyprob
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 10

Accepted Solution

by:
Mez4343 earned 500 total points
ID: 37739840
BindingList support an OrderBy() function to sort with. This  

FieldsToMonitorList.OrderBy(x => x.ChangedDate);  //acending order

FieldsToMonitorList.OrderByDescending(x => x.ChangedDate);  //descending order
0
 
LVL 10

Assisted Solution

by:Mez4343
Mez4343 earned 500 total points
ID: 37739856
0
 
LVL 1

Author Comment

by:countyprob
ID: 37739948
I tried

FieldsToMonitorList.OrderByDescending(x => x.ChangedDate);

Open in new window


but it's not ordering the dates.  I even tried

var OrderedFieldsToMonitorList = FieldsToMonitorList.OrderByDescending(x => x.ChangedDate);

Open in new window


but OrderedFieldsToMonitorList shows the dates ascending also.  

thanks
nick
0
 
LVL 1

Author Comment

by:countyprob
ID: 37740035
never mind, tried it this way and it work

foreach (var FieldsToMonitor in FieldsToMonitorList.OrderByDescending(x => x.ChangedDate))

Open in new window


thanks for the help
0
 
LVL 10

Expert Comment

by:Mez4343
ID: 37740072
sry for late reply.. Here is a code specific way to use OrderBy()'s in a basic windowsForm app.

public class FieldsToMonitor
        {
            public int ID { get; set; }
            public DateTime ChangedDate { get; set; }
        }
        public Form1()
        {
            InitializeComponent();
            _fieldsToMonitorList = CreateList;
            var sortedList = _fieldsToMonitorList.OrderByDescending(x => x.ChangedDate);
            listBox1.DataSource = new BindingSource(sortedList,null);
            listBox1.ValueMember = "ChangedDate";
        }
       
        public BindingList<FieldsToMonitor> CreateList    
        {
          get
          {
              BindingList<FieldsToMonitor> FieldsToMonitorList = new BindingList<FieldsToMonitor>();
              FieldsToMonitorList.Add(new FieldsToMonitor() { ID = 1, ChangedDate = DateTime.Now });
              FieldsToMonitorList.Add(new FieldsToMonitor() { ID = 2, ChangedDate = DateTime.Now.AddMonths(1) });
              FieldsToMonitorList.Add(new FieldsToMonitor() { ID = 3, ChangedDate = DateTime.Now.AddMonths(2) });
              return FieldsToMonitorList;
          }          
        }
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question