How to set the default Sort on a DevExpress GridView

戏子无情 提交于 2019-12-12 16:00:43

问题


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);



来源:https://stackoverflow.com/questions/5901208/how-to-set-the-default-sort-on-a-devexpress-gridview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!