Hello,
I have a datatable as follows:-
QuestionID,QuestionText,QuestionGroup,Sequence.
I want to group this by questionGroup but sort by Sequence ascending.
I did following:-
ultragrid.displaylayout.bands(0).columns("Sequence").sortIndicator = sortindicator.ascending
ultragrid.displaylayout.bands(0).sortedColumns.add("QuestionGroup",false,true)
This sorts by Sequence and groups by Question group. But it also sorts by QuestionGroup.
Here is what I have:-
I want General Information group first followed by Billing information followed by Processing Information.
Any idea?
Thank you very much
Hi,
The way grouping works is that the grid must sort the grouped column first. Then it loops through the rows in order and groups rows with the same values that are adjacent.
So ordinarily, if you sort by one column and then group by another, the groups would not come out right, because you might have the same value in the GroupBy that are no longer adjacent to each other and you would end up with the same group twice.
From the screen shot you posted here, it looks like in your case, the sequences are the Group are linked in such a way that sorting by sequence would cause all of the groups to end up together.
If that is the case, then what you could do is write a SortComparer for the Group column which makes it sort by the Sequence and that should do it.
If your screen shot does not accurately represent all of the data and there are cases where the sequence will be out of order, then what you would need to do is write a GroupBySortComparer to sort the GroupByRows after they are grouped.
Hi Mike,
Thank you for the response. I am not sure if I am understanding right.
In the data I do not have sequence for Groups. I have sequence for questions. Here is sample data
QID QText Sequence QGroup
1 What is Name? 1 General Info
2 What is age? 2 General Info
3 Location? 3 General info
4 Billing number? 4 Billing info
5 Country? 5 Billing info
Now I want to diplay Group General info which contrain Q1,q2,q3
followed by group Billing info containing q4,q5
I get Billing Info group first caz enforced ascending order sorting on group names.
Okay, let me try to explain a little more clearly.
The data you have shown here is sorted by Sequence. Now it just so happens that in this data, there are two categories displayed: General Info and Billing Info.
The question is - in your application, is it possible for you to have the data sorted by Sequence and for the rows in the General Info Category to be discontinuous. In other words, could something like this ever happen?
2 What is age? 2 Billing info
Notice that in my example, the items are still in Sequence, but there is a Billing Info item in between two General Info items.
If this is not possible in your data, then the easiest thing for you to do is to apply a SortComparer to the QGroup Column which causes the groups to be sorted by sequence. This only works if the above example is not possible.
If it is possible for this to occur, then sorting the QGroup column by sequence will not work, since it will result in the groups being discontinuous. If that's the case, then what you would have to do is write a SortComparer that compares the groups themselves based on the sequence of their child row. This is pretty tricky, and I can help you with it if that's what you need. But I'd rather not spend the time writing the complicated code if the easier solution will work for you. :)
I have the exact same question as this. Trying to sort on one field and group on another. And there is no risk that the sort sequence would cause the grouping column to be split up. Your answer said " the easiest thing for you to do is to apply a SortComparer to the QGroup Column which causes the groups to be sorted by sequence". That is what I want to do but I cannot seem to come up with the correct syntax. How do I set up my sortcomparer to sort on Sequence but group by QGroup? My code looks like this:
.Bands(0).SortedColumns.Add("QGroup", False, True) .Bands(0).Columns("QGroup").SortComparer = New MySortComparer()
Private Class MySortComparer Implements IComparer Public Sub New() End Sub Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer Implements IComparer.Compare Dim xCell As UltraGridCell = DirectCast(x, UltraGridCell) Dim yCell As UltraGridCell = DirectCast(y, UltraGridCell) Dim text1 As String = xCell.Value.ToString() Dim text2 As String = yCell.Value.ToString() Return String.Compare(text1, text2, True) End Function End Class
Assuming everything I wrote here three years ago was all correct, then you need to change your SortComparer to sort on the sequence column. What you are doing right now is getting two cells from the QGroup column and then you are comparing the Values of these two cells. That's not what you want.
Instead, take the cells you have and use cell.Row.Cells["Sequence"] to get the Sequence cell for the same row. Then compare the two Sequence cells, not the QGroup cells.
Private Class MySortComparer Implements IComparer Public Sub New() End Sub Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer Implements IComparer.Compare Dim xCell As UltraGridCell = DirectCast(x, UltraGridCell) Dim yCell As UltraGridCell = DirectCast(y, UltraGridCell) xCell = xCell.Row.Cells("Sequence") yCell = yCell.Row.Cells("Sequence") Dim text1 As String = xCell.Value.ToString() Dim text2 As String = yCell.Value.ToString() Return String.Compare(text1, text2, True) End Function End Class
Also... depending on the data type of the Sequence column, comparing by strings may not be a good idea. If the Sequence column is numeric, this will give you weird results. So you probably want to cast the cell's Value into the real DataType of the column.
You may also need to check for DBNull, if that's a possibility in your data.
Works like a charm! Thanks so much!