Solved

Need help in understanding how to sort BindingList

Posted on 2012-03-19
5
1,062 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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

622 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