Friday, September 21, 2012

Sort ObservationCollection according to field value.

Sort ObservationCollection according to field value.

Hi experts,

I tried to sort the observationCollections using the follwing bt it is not working.

 _obserColl.OrderBy(p => p.empNo);

when i tried to print the value it is not also in sorted manner.

Can you suggest me ,how to do this?

Answers & Comments...

Answer: 1

BuntyBubly

BuntyBubly

There's no readson to use ObservableCollection with DataContext because most of those Entity already implemented it.

So that use this

http://msdn.microsoft.com/en-us/library/system.windows.data.pagedcollectionview(v=vs.95).aspx



Answer: 2

Actually I am using to store service data using asynchronous query.

So I can't use PagedCollectionView(Recomend to use  ObservableCollection)

Is there any way to implement sorting in  ObservableCollection?



Answer: 3

BuntyBubly

BuntyBubly

You can sort at server side or just wrap ObservableCollection property within PagedCollectionView.

There's no overhead and performance effect.

Event if you can use Linq but it doesn't work for the ObservableCollection Type.

Add System.Linq, Then you can try.



Answer: 4

Implementing ICollectionView

The preceding code example shows how to implement a simple view model property that returns a collection of items that can be displayed via data bound controls in the view. Because the ObservableCollection<T> class implements the INotifyCollectionChanged interface, the controls in the view will be automatically updated to reflect the current list of items in the collection as items are added or removed.

However, you will often need to more finely control how the collection of items is displayed in the view, or track the user's interaction with the displayed collection of items, from within the view model itself. For example, you may need to allow the collection of items to be filtered or sorted according to presentation logic implemented in the view model, or you may need to keep track of the currently selected item in the view so that commands implemented in the view model can act on the currently selected item.

WPF and Silverlight support these scenarios by providing various classes that implement the ICollectionView interface. This interface provides properties and methods to allow a collection to be filtered, sorted, or grouped, and allow the currently selected item to be tracked or changed. Both Silverlight and WPF provide implementations of this interface–Silverlight provides the PagedCollectionView class, and WPF provides the ListCollectionView class.

Collection view classes work by wrapping an underlying collection of items so that they can provide automatic selection tracking and sorting, filtering, and paging for them. An instance of these classes can be created programmatically or declaratively in XAML using the CollectionViewSource class.

public class MyViewModel : INotifyPropertyChanged  {      public ICollectionView Customers { get; private set; }        public MyViewModel( ObservableCollection<Customer> customers )      {          // Initialize the CollectionView for the underlying model          // and track the current selection.          Customers = new PagedCollectionView( customers );          Customers.CurrentChanged +=                             new EventHandler( SelectedItemChanged );      }        private void SelectedItemChanged( object sender, EventArgs e )      {          Customer current = Customers.CurrentItem as Customer;          ...      }  }


Answer: 5

BuntyBubly

Is there any way to implement sorting in  ObservableCollection?

var employees = new ObservableCollection<Employee>();              for (int i = 0; i < 10; i++)              {                  employees.Add(new Employee                                    {                                        FirstName = "First Name"+i,                                        LastName = "Last Name"+i                                    });              }              DtGrd.ItemsSource = employees.OrderByDescending(ele => ele.FirstName);  





Answer: 6

syed amjad

BuntyBubly

Is there any way to implement sorting in  ObservableCollection?

var employees = new ObservableCollection<Employee>();              for (int i = 0; i < 10; i++)              {                  employees.Add(new Employee                                    {                                        FirstName = "First Name"+i,                                        LastName = "Last Name"+i                                    });              }              DtGrd.ItemsSource = employees.OrderByDescending(ele => ele.FirstName);  


Don't know why it doesn't work with ria.



Answer: 7
_obserColl =_obserColl.OrderBy(p => p.empNo);  







No comments:

Post a Comment

Send us your comment related to the topic mentioned on the blog