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..
yep that's true didn't need the second sort comparer.. the only challenge was to find when the column was sorted and when it was grouped..
Anyways thanks for the help mike..
You don't really need a SortComparer that compares both the date and time. If you simply set the column's SortComparer to null, this is the default behavior. So really, you could handle the BeforeSortChange event and watch to see if the DateTime column is grouped and if so, apply the SortComparer I gave you here. If it's not grouped, just set the SortComparer to Null.
Thank you guys for your responses.. much obliged..
and yes the sort comparer method worked.. i used two actually , one for grouping for which i only required the date and then one for sorting for which i required the entire value of the date time column and not just the date..
By doing this i could perform the sorting once i had grouped the grid with the DateTime column.
Thanks again..
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 }
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;
if (e.SortedColumns[i].IsGroupByColumn)
// Sort grig on group column
sortString += e.SortedColumns[i].Key + ",";
groupFlag = true;
else {
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.Sort = condString.ToString();
Hope to help,
Dan