Solved

Need help in understanding how to sort BindingList

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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

705 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now