Solved

Need help in understanding how to sort BindingList

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

756 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