Sorting a grid
Hi,
I have a grid where I want to insert rows at the top of the grid. I have binded my grid to a ThreadSafeBindedList<> and and I am using the Insert method like so:
private ThreadSafeBindingList<LastPrint> timeAndSales = new ThreadSafeBindingList<LastPrint>();
timeAndSales.Insert(0, new LastPrint() { Time = frmtTime, etc});
However, the rows do not seem to be inserted at the top. I was using a Queue<> but re-binding each time a new order came in was too slow.
Please notice from the attachment the timestamp increases going down the grid, indicating that the latest row is at the bottom and not the top.
TimeAndSales.PNG
-
Hi,
Indexes in the ThreadSafeBindedList or in simple BindingList don't correspond to ones in the grid. There are many reasons for that: rows in the grid can be filtered/grouped/sorted etc. Moreover multiple grids with different sorting can be bound to the same list.
The better solution is to sort by the 'time' column:
grid.Header[0]["time"].SortDirection = SortDirection.Ascending;
if you want to prevent the user from sort changing, set Column.Sortable property for each column to false.
For performance reasons it will be better to add data to the end of the collection.
Best regards,
Dapfor0
Please sign in to leave a comment.
Comments
1 comment