Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Need help in understanding how to sort BindingList

Posted on 2012-03-19
5
Medium Priority
?
1,075 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 2000 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 2000 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

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…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…

704 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