DataGrid Sort Order in Model

Feb 24, 2010 at 2:53 AM

I have an application which heavily leverages DataGrid, and one of the customer requirements is that PDF reports be sorted in the same order that appears in the DataGrid controls.  For example, I have a DataGrid showing participants.  If the user sorts this by first name, and then exports the PDF, they want to see the participants sorted by first name.  If they sort by last name, they want the export to be sorted by last name.

Not wanting to re-invent the wheel, I would like to leverage the existing DataGrid sort functionality, but what I really need to do is be able to get the sort order in the view (the DataGrid) into the model.  What would be ideal is to be able to add an integer property to my model objects, CurrentSortOrder, for example, and data bind that to the current position in the DataGrid.  This is probably not possible, but maybe one of you gurus knows how.

Alternately, I'm sure there's an event when the sort order changes; can I respond to that event and update the model property manually?

As a last resort, I suppose I could have the DataGrid always just sort by the model object's CurrentSortOrder property, and then intercept clicks on column headers and update the collection objects' CurrentSortOrder property, but this would be re-inventing the wheel, which I would rather avoid.

At the end of the day, I basically want to be able to determine the current order of items in a DataGrid.  Being able to bind that to a model object property would be ideal, but I welcome all suggestions as to how I could do this.

~ Ethan