4

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?

Olivier de Rivoyre
  • 1,579
  • 1
  • 18
  • 24

6 Answers6

2

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

DevExpress Team
  • 11,338
  • 2
  • 24
  • 23
  • My data source is a System.Data.DataTable. Unlike List<>, there is no Sort() method on dataTable (to be checked). Maybe by changing the PrimaryKey of the table, but it make a lot of changes in the existing code for so few. Will try the EndSorting event solution first. – Olivier de Rivoyre May 06 '11 at 12:53
  • EndSorting solution does not work. Altering the PrimaryKey solution does not work. Your solution using dataTable.Rows.InsertAt(row, position) and Remove() to maintains the rows order on each update works. I think I can create a procedure that use dataTable.Select("", "LastUpdateDate, Id") and loop over results and the original table in same time to remove row not-in-their-place and re-create it and insert it at the good position. I afraid I will have some focus problems on my cells editors after each updates. Any idea for an easier solution? – Olivier de Rivoyre May 06 '11 at 14:01
  • What if my DataSOurce is not a DataView, or has not? – Guillermo Gutiérrez Oct 22 '13 at 17:13
1

Just put this after InitializeComponent(); on constructor

GridView1.Columns["FieldName"].SortOrder = ColumnSortOrder.Ascending;
Stedy
  • 7,359
  • 14
  • 57
  • 77
1

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?

user528573
  • 143
  • 1
  • 6
  • I want the user be able to sort the grid and have the possibility to come back to the default sort when there is no user filter. I want my filter to be applied if the user does not specify a filter. – Olivier de Rivoyre May 06 '11 at 12:49
0

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.

Ivan Ferić
  • 4,725
  • 11
  • 37
  • 47
0

As per this answer in Devexpress support center How to sort GridView multiple columns programmatically?

The following code can be used for this purpose:

<your GridView>.SortInfo.AddRange(new DevExpress.XtraGrid.Columns.GridColumnSortInfo[] {  
       new DevExpress.XtraGrid.Columns.GridColumnSortInfo(<your first GridColumn>, DevExpress.Data.ColumnSortOrder.Ascending),  
       new DevExpress.XtraGrid.Columns.GridColumnSortInfo(<your second GridColumn>, DevExpress.Data.ColumnSortOrder.Ascending)});  

More details can be found at Sorting in Code

knoami
  • 75
  • 1
  • 2
  • 7
-3

GridControl.SortBy(DateTimeStampColumn, ColumnSortOrder.Descending);

Bernt
  • 1