如何在 DevExpress GridView 上设置默认排序

发布于 2024-11-05 10:40:50 字数 448 浏览 1 评论 0原文

在.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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

夏の忆 2024-11-12 10:40:50

如果我是你,我会根据所需的列对网格的数据源进行排序。在这种情况下,如果最终用户清除了 gridView 的排序条件,则数据将按照您的数据源指定的顺序显示。

更新
以下是应该适合您的代码:

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

另外,请查看以下 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:

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

Also, take a look at the following MSDN article :

DataView.Sort Property

遇见了你 2024-11-12 10:40:50

禁用最终用户排序不是最简单的吗?或者我是否误解了您的问题 - 即您是否希望在默认排序后应用它们的排序?

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?

少钕鈤記 2024-11-12 10:40:50

只需将其放在构造函数的 InitializeComponent(); 之后

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

Just put this after InitializeComponent(); on constructor

GridView1.Columns["FieldName"].SortOrder = ColumnSortOrder.Ascending;
新雨望断虹 2024-11-12 10:40:50

您可以在 GridView.EndSorting 事件上添加事件处理程序,并在该处理程序中检查是否有任何列具有 SortIndex >= 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.

请你别敷衍 2024-11-12 10:40:50

根据 Devexpress 支持中心的这个答案 如何以编程方式对 GridView 多列进行排序?

以下代码可用于此目的:

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

更多详细信息可以在 在代码中排序

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

年华零落成诗 2024-11-12 10:40:50

GridControl.SortBy(DateTimeStampColumn, ColumnSortOrder.降序);

GridControl.SortBy(DateTimeStampColumn, ColumnSortOrder.Descending);

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文