User (Old forums)MemberAugust 14, 2007 at 9:25 amPost count: 23064
Our grid is bound programmatically to an IEnumerable collection through a DataGridCollectionView (DGCV). We need to control the visible columns and their titles, so in code we construct the DGCV with the collection in the constructor, clear the DGCV’s ItemProperties to blow away the auto-generated columns, then fill in ItemProperties with the columns we want. So far, it works great, except now we need a few more things to work:
– We want to programmatically change an individual line item in the collection and have the change immediately reflected in the grid *without resorting.* Right now to see the change we apparently have to call Refresh on the DGCV, which re-sorts.
– We want to programmatically add items to the end of our collection and see them at the bottom of the grid, again without re-sorting.
Nothing except the call to Refresh seems to get the grid to display the programmatic changes to our collection. So we thought we’d try an “unbound” DGCV. According to the documentation you can construct a DGCV with a type in the constructor, then spin through and insert the line items directly into the DGCV with SourceItems.Add. The problem is that after construction, the DGCV’s ItemProperties collection is null, and there appears to be no way to instantiate it. Trying to use the DGCV in the grid control then crashes the program with a null object reference.
Bound or unbound, our primary goal is to be able to manipulate the grid’s line items without triggering a sort. Any ideas?
Imported from legacy forums. Posted by Mark (had 576 views)User (Old forums)MemberAugust 14, 2007 at 10:21 amPost count: 23064
Taking a guess…assuming you don’t want to sort… you may want to confirm that the DGCV SortDescriptions are cleared DataGridCollectionView.SortDescriptions.Clear().
It’s not clear to me what magic DataGridCollectionView does in detail when it comes to sorting, but I believe it handles all the details independently of the underlying collection you supply when creating it.
Therefore assigning a different kind of collection to ItemsSource may solve your problem assuming you don’t need the features of DGCV. Perhaps a collection that simply supports IBindingList or a BindingListCollectionView instead. I believe the DataGridControl will even work with collections that only support IEnumerable. Using simpler collections will give you less functionality, but that may not matter to you if you’re doing most of the modifications programmatically anyway.
Imported from legacy forums. Posted by Richard (had 375 views)User (Old forums)MemberAugust 14, 2007 at 10:38 amPost count: 23064
We do want the user to be able sort the grid using the grid UI, so we do want a DGCV that sorts the list. But we have a separate view in our application that shows the details of the selected grid item and lets them edit the data there. We then want to reflect any changes they made back to the grid, which we do in code-behind. But in this case we don’t want the grid to re-sort, which would result in too much jumping around in the grid. Also, if the user adds a new record in the details view, we want that new record to appear at the bottom of the grid (until the user manually resorts the grid).
To summarize, the user should be able to sort by clicking on grid column headers, but any changes we make to line items in code-behind shouldn’t trigger a re-sort.
Imported from legacy forums. Posted by Mark (had 356 views)User (Old forums)MemberAugust 14, 2007 at 1:47 pmPost count: 23064
I haven’t tried to do what you want to, but if you haven’t already, I’d look to see what happens if you temporarily suspend sorting by clearing the sort from the DGCV.
You could also consider modifying the sort to include a check for the Record.LastModificationTime < LastUserSortTime. The LastUserSortTime would be updated each time there is a sort and the Record.LastModificationTime would be updated each time the record is changed. This would keep all the records added since the last sort at the end of the Grid.
Imported from legacy forums. Posted by Richard (had 384 views)User (Old forums)MemberAugust 14, 2007 at 6:04 pmPost count: 23064
When you clear and reinstate the sort, it changes the sort in the grid which isn’t what we want.
For now we’ve worked around the problem by adding an invisible column SortIndex and making it be the primary sort. We start with all the items’ SortIndex values being -1 and let the grid sort using the remaining sort criteria. Then we spin through the items and set each SortIndex to each item’s row index. After that, as we programmatically change the item data, the SortIndex doesn’t change so the rows stay put.
Imported from legacy forums. Posted by Mark (had 4375 views)
- You must be logged in to reply to this topic.