class="author-p-824761" style="font-size: 15px">因数据源中需要显示**等内容 所以数据列是string类型,字符串类型排序时,结果如下:
gridColumn1.SortMode = DevExpress.XtraGrid.ColumnSortMode.Custom;
然后在事件CustomColumnSort
void gdv_CustomColumnSort(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnSortEventArgs e) { if (e.Column != null) { string value1 = e.Value1.ToString(); string value2 = e.Value2.ToString(); int result = Comparer.Default.Compare (ConvertToDecimal(value1, e.SortOrder), ConvertToDecimal(value2, e.SortOrder)); e.Result = result; e.Handled = true; } } private decimal ConvertToDecimal(string input, DevExpress.Data.ColumnSortOrder sortOrder) { decimal result = 0; if (string.IsNullOrWhiteSpace(input) || input.Equals("**") || input.Equals("--")) result = sortOrder == DevExpress.Data.ColumnSortOrder.Ascending ? 9999 : -9999; else decimal.TryParse(input, out result); return result; }之后又遇到一个问题:某一列根据int类型值对应显示图片,但是在排序时并不是按照int值排序。 经查找解决方法为设置该列的排序SortMode
gridColumn1.SortMode = DevExpress.XtraGrid.ColumnSortMode.Value;