views:

412

answers:

4

I have a custom class that inherits from BindingList(T) that I am binding to a DataGrid.

However, the DataGrid is being populated from the top down and I want it populated from the bottom up. So the bottom item is index 0 rather then the top item.

How can I change my BindingList(T) so that the DataGrid reads it in reverse?

+1  A: 

This article on CodeProject.com about implementing sortable BindingList might help you.

TheVillageIdiot
A: 

Its simple, but you could also populate the binding list in reverse order (unless you have another reason for keeping the current order).

Nader Shirazie
I'm not populating the list diagrammatically the user is populating the list through the DataGrid. Each row of the grid represents the floor of a building. So I want lower floors to show at bottom and upper floors to show at the top. I'd like to reverse the order so that I can reference floors in the list by floor number where floorIndex = floor number-1
Eric Anastas
now that's useful information - please add that to the question. it should help get a better answer. Is this a matter of the user doing something like "Add new Row"?
Nader Shirazie
A: 

Why not call the ApplySort method on the IBindingList interface implementation on the BindingList<T>? You can just pass the value of ListSortDirection.Descending for the second parameter and the DataGrid should display the items in reverse order.

casperOne
Will I need to re-apply the sort whenever I add or remove items from the list? Or Will it remember to keep the Descending sort order?
Eric Anastas
@Eric I don't see why it *wouldn't* keep the sort order, but it's easy enough to test.
casperOne
A: 

If your list is inheriting from a Collection class, you can use the Insert(0,item) method to insert to the beginning of the list. That way the newest element would appear on the top. Not sure if the BindingList supports this though.

Gergely Orosz