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..

  • 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
        }

  • 415
    posted

    Hi uMarM,

    You can do that like that:

    1. Prevent default sorting of grid: set property HeaderClickAction = HeaderClickAction.ExternalSortSingle

    2. Override the BeforeSortChange event of the grid

    3. Create your BeforeSortChange to Sort data as the sample code below

    void ultraGrid1_BeforeSortChange(object sender, BeforeSortChangeEventArgs e)

            {

                StringBuilder condString = new StringBuilder();

                string strIndicator = string.Empty;

     

                if (e.SortedColumns != null && e.SortedColumns.Count > 0)

                {

                    for (int i = 0; i < e.SortedColumns.Count; i++)

                    {

                        if (!e.SortedColumns[i].IsGroupByColumn)

                        {

                            strIndicator = getStringIndicator(e.SortedColumns[i].SortIndicator);

     

                            condString.Append(" " + e.SortedColumns[i].Key);

                            condString.Append(" " + strIndicator);

                        }

                    }

                }

     

                DataView dv;

     

                // Grouping

                bool groupFlag = false;

     

                string sortString = string.Empty;

                for (int i = 0; i < e.SortedColumns.Count; i++)

                {

                    if (e.SortedColumns[i].IsGroupByColumn)

                    {

                        // Sort grig on group column

                        sortString += e.SortedColumns[i].Key + ",";

                        groupFlag = true;

                    }

                    else {

                        strIndicator = getStringIndicator(e.SortedColumns[i].SortIndicator);

     

                        sortString += e.SortedColumns[i].Key + " " + strIndicator + ",";

                    }

                }

                dv = this.nwindDataSet.Tables[0].DefaultView;

                dv.Sort = sortString.TrimEnd(',');

                this.ultraGrid1.DataSource = dv;

     

                // Custom sorting

                if (!groupFlag)

                {

                    dv = this.nwindDataSet.Tables[0].DefaultView;

                    dv.Sort = condString.ToString();

                    this.ultraGrid1.DataSource = dv;

                }

            }

    Hope to help,

    Dan