Hello. I currently have a custom sort setup.
ultraGridDaySheet.DisplayLayout.Bands[m_DaySheetDataSet.DaySheet.TableName].Columns[m_DaySheetDataSet.DaySheet.TimeColumn.ColumnName].SortIndicator = SortIndicator.Ascending; ultraGridDaySheet.DisplayLayout.Bands[m_DaySheetDataSet.DaySheet.TableName].Columns[m_DaySheetDataSet.DaySheet.ApptStateDispCharColumn.ColumnName].SortComparer = new MySortComparer(); ultraGridDaySheet.DisplayLayout.Bands[m_DaySheetDataSet.DaySheet.TableName].SortedColumns.Add(ultraGridDaySheet.DisplayLayout.Bands[m_DaySheetDataSet.DaySheet.TableName].Columns[m_DaySheetDataSet.DaySheet.ApptStateDispCharColumn.ColumnName], false); ultraGridDaySheet.DisplayLayout.Bands[m_DaySheetDataSet.DaySheet.TableName].Columns[m_DaySheetDataSet.DaySheet.ArrivalTimeColumn.ColumnName].SortIndicator = SortIndicator.Ascending;
What I'd like to accomplish is when the user clicks the first header of columns, I'd like it to be sorted with that code. How would I go about doing this?
I've tried the following:
private void ultraGridDaySheet_AfterSortChange(object sender, BandEventArgs e) { ultraGridDaySheet.DisplayLayout.Bands[m_DaySheetDataSet.DaySheet.TableName].Columns[m_DaySheetDataSet.DaySheet.TimeColumn.ColumnName].SortIndicator = SortIndicator.Ascending; ultraGridDaySheet.DisplayLayout.Bands[m_DaySheetDataSet.DaySheet.TableName].Columns[m_DaySheetDataSet.DaySheet.ApptStateDispCharColumn.ColumnName].SortComparer = new MySortComparer(); ultraGridDaySheet.DisplayLayout.Bands[m_DaySheetDataSet.DaySheet.TableName].SortedColumns.Add(ultraGridDaySheet.DisplayLayout.Bands[m_DaySheetDataSet.DaySheet.TableName].Columns[m_DaySheetDataSet.DaySheet.ApptStateDispCharColumn.ColumnName], false); ultraGridDaySheet.DisplayLayout.Bands[m_DaySheetDataSet.DaySheet.TableName].Columns[m_DaySheetDataSet.DaySheet.ArrivalTimeColumn.ColumnName].SortIndicator = SortIndicator.Ascending; }
But I get an error and crashes when this occurs.
What's the error?
I don't think you can change the sorting in the AfterSortChange. Doing so will either not work or it will cause an infinite loop, since changing the sorting causes the event to fire again, which changes the sorting, which fires the event again, etc.
You could use a flag to prevent recursion or use the EventManager to disable the event while you are in it. But I think it's probably easier to use BeforeSortChange, instead and modify the new sort information passed in to the event args.
Alright, I might've not made it clear what I wanted to accomplish, for that, sorry.
Here's what I'd like done:
UltraWInGrid loads -> sorts by a custom order with following code: (This works perfectly!)
private void ultraGridDaySheet_InitializeLayout(object sender, InitializeLayoutEventArgs e) { e.Layout.Override.HeaderClickAction = HeaderClickAction.SortMulti; this.ultraGridDaySheet.DisplayLayout.Bands[m_DaySheetDataSet.DaySheet.TableName].Columns[m_DaySheetDataSet.DaySheet.TimeColumn.ColumnName].SortIndicator = SortIndicator.Ascending; this.ultraGridDaySheet.DisplayLayout.Bands[m_DaySheetDataSet.DaySheet.TableName].Columns[m_DaySheetDataSet.DaySheet.ApptStateDispCharColumn.ColumnName].SortComparer = new MySortComparer(); this.ultraGridDaySheet.DisplayLayout.Bands[m_DaySheetDataSet.DaySheet.TableName].Columns[m_DaySheetDataSet.DaySheet.ApptStateDispCharColumn.ColumnName].SortIndicator = SortIndicator.Ascending; this.ultraGridDaySheet.DisplayLayout.Bands[m_DaySheetDataSet.DaySheet.TableName].SortedColumns.Add(ultraGridDaySheet.DisplayLayout.Bands[m_DaySheetDataSet.DaySheet.TableName].Columns[m_DaySheetDataSet.DaySheet.ApptStateDispCharColumn.ColumnName], false); this.ultraGridDaySheet.DisplayLayout.Bands[m_DaySheetDataSet.DaySheet.TableName].Columns[m_DaySheetDataSet.DaySheet.ArrivalTimeColumn.ColumnName].SortIndicator = SortIndicator.Ascending; }
Here's where I'd like to expand a bit. When the user clicks on the Column "Time", I'd like it to be sorted by this exact same way ( ultraGridDaySheet_InitializeLayout(object sender, InitializeLayoutEventArgs e) ).
I've used the following code:
private void ultraGridDaySheet_MouseClick_1(object sender, MouseEventArgs e) { UltraGrid grid = sender as UltraGrid; Infragistics.Win.UIElement controlElement = grid.DisplayLayout.UIElement; Infragistics.Win.UIElement elementAtPoint = controlElement != null ? controlElement.ElementFromPoint(e.Location) : null; UltraGridColumn column = null; while (elementAtPoint != null) { HeaderUIElement headerElement = elementAtPoint as HeaderUIElement; if (headerElement != null && headerElement.Header is Infragistics.Win.UltraWinGrid.ColumnHeader) { column = headerElement.GetContext(typeof(UltraGridColumn)) as UltraGridColumn; if (column.Key.Equals("Time")) { this.ultraGridDaySheet.DisplayLayout.Bands[m_DaySheetDataSet.DaySheet.TableName].Columns[m_DaySheetDataSet.DaySheet.TimeColumn.ColumnName].SortIndicator = SortIndicator.Ascending; this.ultraGridDaySheet.DisplayLayout.Bands[m_DaySheetDataSet.DaySheet.TableName].Columns[m_DaySheetDataSet.DaySheet.ApptStateDispCharColumn.ColumnName].SortComparer = new MySortComparer(); this.ultraGridDaySheet.DisplayLayout.Bands[m_DaySheetDataSet.DaySheet.TableName].Columns[m_DaySheetDataSet.DaySheet.ApptStateDispCharColumn.ColumnName].SortIndicator = SortIndicator.Ascending; this.ultraGridDaySheet.DisplayLayout.Bands[m_DaySheetDataSet.DaySheet.TableName].SortedColumns.Add(ultraGridDaySheet.DisplayLayout.Bands[m_DaySheetDataSet.DaySheet.TableName].Columns[m_DaySheetDataSet.DaySheet.ApptStateDispCharColumn.ColumnName], false); this.ultraGridDaySheet.DisplayLayout.Bands[m_DaySheetDataSet.DaySheet.TableName].Columns[m_DaySheetDataSet.DaySheet.ArrivalTimeColumn.ColumnName].SortIndicator = SortIndicator.Ascending; } break; } elementAtPoint = elementAtPoint.Parent; } }
But the issue is, it doesn't want to sort back to the way it was when ultraGridDaySheet_InitializeLayout(object sender, InitializeLayoutEventArgs e) was called.
Hopefully this clears up what I was trying to achieve.
I'm not sure I understand exactly what you are trying to do, but going by the code in InitializeLayout, it looks like you are trying to synchronize the sorting between the 'ApptStateDispCharColumn' and 'ArrivalTimeColumn' columns. If this is the case, I think all you have to do is assign the same SortComparer instance to both of those columns, and modify the implementation to account for both of them. You would sort the 'ApptStateDispCharColumn', and then when the values in that column are the same, sort by 'ArrivalTimeColumn', i.e., make that the secondary sort criteria.