Hello,
I was wondering if the XamDataGrid has some sort of auto-sorting turned on by default. I have an observable collection of custom row objects bound to a XamDataGrid.DataSource. When I insert a new row object at the top of the collection by calling the observable collection's Insert() method, the grid re-sorts the list so that my empty row object appears at the bottom of the list. I checked the order of the observable collection and it is correct; however, the grid display no longer matches the order of items in the collection. Note: the order of items is displayed correctly upon initially binding to the XamDataGrid.
Thanks,
David
Alex,
I did what you suggested (adding the RowIndex field to the output) and was able to at least see the strange behavior. As it turned out the row objects were being sorted correctly; however, the data displayed in the row no longer corresponded to the correct row.
For example - Let's say we have a grid with 4 columns (the first and second columns are unbound field objects and the third and fourth ones are bound fields)
The datasource is an observable collection of a custom RowObject class.
We implement 2 PropertyDescriptors (in the RowObject class) and bind these to the first and second unbound field controls respectively; the third column is bound to a "RowIndex" property and the fourth column is bound to a "VRowIndex" property. I initially create a collection of 3 of these RowObjects and bind it the XamDataGrid.DataSource.
When the binding occurs, the property descriptors in the rowobjects call back to our database to get the corresponding data resulting in 2 valid rows with data and the third rowobject being empty (since we only have 2 rows of real data in our database). Everything looks good so far.
Here is what the data looks like so far (note: VRowIndex = -1 indicates a false blank row to us)
RowIndex 0 VRowIndex 0 MurphyRowIndex1 VRowIndex 1 LizRowIndex2 vRowIndex -1
Now I create a new RowObject and call the insert on the observable collection to add it to the 0 row position. After calling insert - I get the following display
RowIndex0 vRowIndex -1 LizRowIndex1 vRowIndex 0 MurphyRowIndex2 vRowIndex 1 RowIndex3 vRowIndex -1
As you can see - the RowIndex and vRowIndex order is correct; however, the column information no longer displays correctly. If I modify data in each valid row - the data is still saved back to the database correctly which validates the fact that the observable collection is correct, but the display is not. Also, a strange thing happens if I enter data in RowIndex2 or RowIndex3 - any data entered is displayed in both rows. For example - if I enter data in RowIndex2, then RowIndex3 shows the same data (and vice-versa). It's almost like the XamDataGrid is holding onto the last RowObject instance so any new rows inserted are still using the reference to the last row object (this doesn't happen if I just add rows to grid). Has anyone reported a problem with PropertyDescriptors and the XamDataGrid? I found a work around where I set the datasource property to null and then bind the datasource again - but this has to be done via the dispatcher (otherwise it won't work) which results in an annoying flicker in the display since the whole grid is refreshed.
David,
To sort, you need a field to sort by. You say that this (index) property is in your class, but you do not display that field. In order to sort by this field you need it in your xamDataGrid. You can set its visibility to collapsed. Assuming that you can add this field to the SortedFields collection and sort by it like this:
xamDataGrid.FieldLayouts[0].SortedFields.Add(new FieldLayoutDescription( field name, direction, isGroupBy);
Hope this helps,
Alex.
Hi Alex,
Thanks for the response. Is there a way to sort the grid based on a property value in my row class? In my row class I maintain a private variable that stores the current Row index number. I don't display this in the grid, but is there some way I can set the XamDataGrid to use this property value and sort on that?
I was also not able to reproduce this behavior - my class implements only the INotifyPropertyChanged interface, Vlad as well. So I suppose the issue is coming from the ICustomTypeDescriptor. There are no internal auto-sorting properties that might have effect on this.
No, I'm using the ICustomTypeDescriptor interface to setup the binding between the PropertyDescriptors in the row class with our user controls in the grid. We are using our own custom unbound field user controls, but we aren't trying to hide any columns. Here is what is happening:
For example:
If our data set contains 2 rows of data - the XamDataGrid displays the rows correctly.
row1: Samrow:2 Danrow:3 (blank row as a placeholder) - we have a requirement to show a minimum of 3 rows so a empty row object is added to the collection.
I want to insert a new blank placeholder row at the top of the list (row 0 - zero based). When I go to my collection class and call the Insert on my observable collection - the collection is correct but the grid display the information in the following order:
row1: Danrow2: Samrow3: blank rowrow4: blank row