How to set the default Sort on a DevExpress GridView

Olivier de Rivoyre picture Olivier de Rivoyre · May 5, 2011 · Viewed 17.6k times · Source

On .net WinForm, DevExpress's GridControl/GridView bound on a DataSet, how to specify the default sort order? The one that is used when there is no visible GridColumn with a SortOrder.

By default, I have set a sorting on the view on my hidden DateTimeStamp GridColumn. It is of course overrided by user if user click on a column. User can "Clear Sorting" using the menu on column or by clicking on a column while pressing the Control key. When doing that, Rows are not sorted anymore (or maybe by PK?) while I would like them sorted by DateTimeStamp.

Any idea? Maybe by plugging code to be notified when user "Clear Sorting"? I can play with GridView.PopupMenuShowing and GridStringId.MenuColumnClearSorting to handle the user-click-on-menu case. But it does not handle the case of Control+click.

Has someone meet the same problem and found a (simple) solution?

Answer

DevExpress Team picture DevExpress Team · May 6, 2011

If I were you, I would sort the grid's DataSource based on the required column. In this case, if the gridView's sorting condition is cleared by the end-user, data will be displayed in the order specified by your DataSource.

UPDATE here is the code which should work for you:

DataView dv = yourDataTable.DefaultView;
dv.Sort = "SomeField";
gridControl.DataSource = dv;

Also, take a look at the following MSDN article :

DataView.Sort Property