下面的示例展示了如何通过 ColumnView.CustomColumnSort 事件实现自定义排序。 在本示例中,当按照 ItemFolderDescription 字段排序时,将替换为按照 IsEmptyRow 字段对数据排序。 注意,在数据源级别访问 IsEmptyRow 字段的取值。 首先,使用 ListSourceRowIndex1 和 ListSourceRowIndex1 参数取回行对象。 然后,使用由行对象提供的方法获取字段值。

注意,假设已经为 ItemFolderDescription 列启用自定义排序 (它的 GridColumn.SortMode 属性值被设置为 ColumnSortMode.Custom)。

C#CopyCode image复制代码
void gridView_CustomColumnSort(object sender, 
DevExpress.XtraGrid.Views.Base.CustomColumnSortEventArgs e) {
    try {
        if (e.Column.FieldName == "ItemFolderDescription") {
            DataRowView dr1 = (gridView.DataSource as DataView)[e.ListSourceRowIndex1];
            DataRowView dr2 = (gridView.DataSource as DataView)[e.ListSourceRowIndex2];
            e.Handled = true;
            e.Result = System.Collections.Comparer.Default.Compare(dr1["IsEmptyRow"], 
                dr2["IsEmptyRow"]);
        }
    }
}
Visual BasicCopyCode image复制代码
Imports DevExpress.XtraGrid.Views.Base

Private Sub GridView1_CustomColumnSort(ByVal sender As System.Object, _
ByVal e As CustomColumnSortEventArgs) Handles GridView1.CustomColumnSort
    Try
        If e.Column.FieldName = "ItemFolderDescription" Then
           Dim dr1 As DataRowView = (TryCast(gridView.DataSource, DataView))(e.ListSourceRowIndex1)
           Dim dr2 As DataRowView = (TryCast(gridView.DataSource, DataView))(e.ListSourceRowIndex2)
           e.Handled = True
           e.Result = System.Collections.Comparer.Default.Compare(dr1("IsEmptyRow"), _ 
               dr2("IsEmptyRow"))
        End If
    End Try
End Sub