如何在 DevExpress GridView 上设置默认排序
在.net WinForm上,DevExpress的GridControl/GridView绑定在DataSet上,如何指定默认排序顺序?当没有带有 SortOrder 的可见 GridColumn 时使用的一种。
默认情况下,我已在隐藏的 DateTimeStamp GridColumn 上的视图上设置了排序。如果用户单击某列,它当然会被用户覆盖。 用户可以使用列上的菜单或按住 Control 键单击列来“清除排序”。这样做时,行不再排序(或者可能按 PK?),而我希望它们按 DateTimeStamp 排序。
有什么想法吗?也许通过插入代码在用户“清除排序”时收到通知?我可以使用 GridView.PopupMenuShowing 和 GridStringId.MenuColumnClearSorting 来处理用户单击菜单的情况。但它不处理 Control+单击的情况。
有人遇到同样的问题并找到(简单的)解决方案吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
如果我是你,我会根据所需的列对网格的数据源进行排序。在这种情况下,如果最终用户清除了 gridView 的排序条件,则数据将按照您的数据源指定的顺序显示。
更新
以下是应该适合您的代码:
另外,请查看以下 MSDN 文章:
DataView.Sort 属性
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:
Also, take a look at the following MSDN article :
DataView.Sort Property
禁用最终用户排序不是最简单的吗?或者我是否误解了您的问题 - 即您是否希望在默认排序后应用它们的排序?
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?
只需将其放在构造函数的
InitializeComponent();
之后Just put this after
InitializeComponent();
on constructor您可以在 GridView.EndSorting 事件上添加事件处理程序,并在该处理程序中检查是否有任何列具有
SortIndex >= 0
。如果没有,您可以设置自己的排序。You could add event handler on
GridView.EndSorting
event, and in that handler check if there are any columns which haveSortIndex >= 0
. If there are not, you could set your own sorting.根据 Devexpress 支持中心的这个答案 如何以编程方式对 GridView 多列进行排序?
以下代码可用于此目的:
更多详细信息可以在 在代码中排序
As per this answer in Devexpress support center How to sort GridView multiple columns programmatically?
The following code can be used for this purpose:
More details can be found at Sorting in Code
GridControl.SortBy(DateTimeStampColumn, ColumnSortOrder.降序);
GridControl.SortBy(DateTimeStampColumn, ColumnSortOrder.Descending);