How to set the default Sort on a DevExpress GridVi

2019-07-04 04:18发布

问题:

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?

回答1:

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



回答2:

Would it not be easiest just to disable end-user sorting? Or have I misunderstood your problem - i.e. do you want their sorting to be applied after your default sorting?



回答3:

Just put this after InitializeComponent(); on constructor

GridView1.Columns["FieldName"].SortOrder = ColumnSortOrder.Ascending;


回答4:

You could add event handler on GridView.EndSorting event, and in that handler check if there are any columns which have SortIndex >= 0. If there are not, you could set your own sorting.



回答5:

GridControl.SortBy(DateTimeStampColumn, ColumnSortOrder.Descending);