Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
60
Sorting a Column with in a group
posted

I have grouped the grid by a certain column with type DateTime and GroupByMode = GroupByMode.Date.

Now i need to sort the values with in the group based on the new column clicked , not including the column that has already been used to make a group.

i have tried the property HeaderClickAction = SortSingle but it hasnt worked.

any ideas..

Parents
  • 469350
    Suggested Answer
    Offline posted

    Hi,

    Grouping is very tightly tied to sorting. So if the grid is grouped by a particular field, the first thing the grid has to do is sort by that field.

    So let's say for example that you are grouping the grid by an integer field. Once the grid is sorted and grouped, you can sort within a group by clicking on a column header. This works because since every row in the group has the same grouped value, sorting within the group doesn't affect the group field sorting.

    When you set the GroupByMode to date, the grid is sorted by the DateTime, then grouped by Date and the time is ignored. When you then sort the grid by another field, the sorting has no effect, since the column is still sorted by the data and the time. So even though the time is not accounted for in the grouping, it's still considered in the sorting. So this won't work unless all of the times are the same.

    This raises an interesting point. The grid column should probably have a SortMode to go along with the GroupByMode so that you could tell it to sort only on the Date and ignore the time.

    In the mean time, the easiest way to get around this would be to use a SortComparer on the column that only sorts based on Date and not time. This is very easy to do, you just strip out the times and compare the values:


        public class DateComparer : IComparer
        {
            #region IComparer Members

            int IComparer.Compare(object x, object y)
            {
                UltraGridCell cellX = x as UltraGridCell;
                UltraGridCell cellY = y as UltraGridCell;

                DateTime dtX = (DateTime)cellX.Value;
                DateTime dtY = (DateTime)cellY.Value;

                dtX = new DateTime(dtX.Year, dtX.Month, dtX.Day);
                dtY = new DateTime(dtY.Year, dtY.Month, dtY.Day);           

                return dtX.CompareTo(dtY);
            }

            #endregion
        }

Reply Children