Situation:
I have a grid with columns "NAME" and "DATE". When sorting (ascending) on date, all rows without a date should be placed at the end of the rowcollection (like when sorting descending).
This is what i'm currently doing:
Private Sub gridcandidates_AfterSortChange(ByVal sender As System.Object, ByVal e As Infragistics.Win.UltraWinGrid.BandEventArgs) Handles gridcandidates.AfterSortChange
Dim col As UltraGridColumn = e.Band.Columns("DATE") If col.SortIndicator = SortIndicator.Ascending Then For i As Integer = gridcandidates.Rows.Count - 1 To 0 Step -1 If Not IsDBNull(gridcandidates.Rows(i).Cells("DATE").Value) Then gridcandidates.Rows.Move(gridcandidates.Rows(i), 0) 'Move row to beginning Else gridcandidates.Rows.Move(gridcandidates.Rows(i), gridcandidates.Rows.Count - 1) 'Move row to end End If Next End If
End Sub
Question/problems:
- It messes up the sort... the columns aren't sorted by date.
- Is there anyway to improve the code? I'm sure i'm overlooking something!
This works:
public int Compare(object x, object y) { UltraGridCell xCell = x as UltraGridCell; UltraGridCell yCell = y as UltraGridCell; object xObject = xCell.Value; object yObject = yCell.Value; // If the two values are the same, compare the row index. // This isn't really neccessary, but it makes the sorting more reliable. // We won't get random switching of like values when sorting multiple // times. if (xObject.Equals(yObject)) return xCell.Row.Index.CompareTo(yCell.Row.Index); // If we get to this point, we know the values are not equal. So if // the x value is null, we know the y value is not. if (xObject is DBNull || xObject == null) { // If we are sorting ascending, then null is greater than any other // value. If we are sorting descending, null is less that any other // value. return ascending ? 1 : -1; } // If we get to this point, we know the values are not equal. So if // the y value is null, we know the x value is not. if (yObject is DBNull || yObject == null) { // If we are sorting ascending, then null is less than any other // value. If we are sorting descending, null is greater that any other // value. return ascending ? -1 : 1; } // If we get here, there are no nulls and the values are not equal // so just compare them as ints. int xInt = (int)xObject; int yInt = (int)yObject; return xInt.CompareTo(yInt); }
Hi
I am trying to do the same thing. I created a NullValueComparer class and assigned to the relevant column. The column contains integer values but some values are null, and I want the null value records to always appear after the records with values. However, the records with null values still appear intermittently throughout the grid, and the numerical values are not sorted properly either. The _ascending value is set in the BeforeSortChange event. Here is the code I am using:
{
// Passed in objects are cells. So you have to typecast them to UltraGridCell objects first.
UltraGridCell yCell = (UltraGridCell)y;
// Compare values of xCell and yCell and return a negative
// number if xCell is less than yCell, positive number if xCell is greater than yCell,
// and 0 if xCell and yCell are equal.
// Regardless of whether the sort is ascending or descending, we want the null values to
// always appear at the bottom
// If the sort is ascending, null values should be set to larger than non-null values
// If the sort is descending, null values should be set to smaller than non-null values
// Extract values from cells
if (string.IsNullOrEmpty(xCell.Value.ToString()))
else
}
if (string.IsNullOrEmpty(yCell.Value.ToString()))
// Compare values
// want xValue to appear after actual values if ascending, therefore xValue greater than yValue
return 1;
// want yValue to appear after actual values if ascending, therefore yValue greater than xValue
if (xValue < yValue) return -1;
Can you please help me see what I am missing here?
I'll give it a try!
Thanks Mike
I think the actual sorting process may be asynchronous, so the rows might not actually be sorted by the time AfterSortChange fires.
You might be better off applying a SortComparer to the column. Your custom SortComparer class would have to know whether the column is being sorted Ascending or Descending (you could set a property on it from the BeforeSortChange event of the grid) and then sort the null values as higher or lower depending on the sort direction.