The GridView Sorting Problem in DevGridControl,
Because ** and other content need to be displayed in the data source, the data column is of the string type. The result is as follows:
At this time, we need to do some processing to achieve the desired sorting effect, first set the attribute
gridColumn1.SortMode = DevExpress.XtraGrid.ColumnSortMode.Custom;
Then implement it in the CustomColumnSort event
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; }
The effect is as follows:
Then, another problem occurs: an image is displayed for a column based on the int type value, but the image is not sorted by the int type value. The solution is to set SortMode for this column.
gridColumn1.SortMode = DevExpress.XtraGrid.ColumnSortMode.Value;