This is a migrated thread and some comments may be shown as answers.

GridView Grouping By Data Instead Of Column

6 Answers 14 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Brandon
Top achievements
Rank 1
Brandon asked on 30 Jan 2019, 10:54 PM

Let's say we have 5 items and we want to group them by either 'Outdoor' or 'Indoor' respectively.

1) Lawn Mower - Outdoor

2) Cabinet - Indoor

3) Fence - Outdoor

4) Table - Indoor

5) Couch - Indoor

 

There isn't a column dedicated to whether these are Indoor or Outdoor items, but I still want to group them based on that "Indoor/Outdoor" data that I have for them.

I can do this with a ListView fairly easily by just dynamically creating a group and adding the items I want to this group.  What I need to know is how I can create a group in a GridView based off of data not present in the columns.

6 Answers, 1 is accepted

Sort by
0
Dimitar
Telerik team
answered on 31 Jan 2019, 11:23 AM
Hello Brandon,

You can use custom groping to create such custom groups. You will still need to add a valid sort descriptor and this is why you can use the ShowGroupedColumns property to show the column that is used for the descriptor. I have attached a small project that demonstrates how you can implement this. 

I hope this helps. Should you have any other questions do not hesitate to ask.
 
Regards,
Dimitar
Progress Telerik
Get quickly onboard and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
0
Brandon
Top achievements
Rank 1
answered on 31 Jan 2019, 03:52 PM

That works.  Thank you. 

Last question I think.  How do I dictate what order the groups are in?  I don't want them in alphabetical order necessarily.  I want to position specifically two groups at the top, and the order doesn't matter for the rest.

 

I also want to Color the group header text as well, but I imagine I can find an example of coloring them elsewhere.

0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 01 Feb 2019, 09:49 AM
Hello, Brandon,       

By default, when you perform grouping, RadGridView sorts the created group rows alphabetically. You can change this sort order by using a group comparer. It is necessary to create a class that implements the IComparer interface where you should return an integer number in the implemented Compare method. A sample approach is demonstrated in the following help article: https://docs.telerik.com/devtools/winforms/controls/gridview/grouping/sorting-group-rows

As to the style of the group rows, it is suitable to use the ViewCellFormatting event. You can find below a sample code snippet: 
private void radGridView1_ViewCellFormatting(object sender, CellFormattingEventArgs e)
{
    GridGroupContentCellElement groupCell = e.CellElement as GridGroupContentCellElement;
    if (groupCell != null)
    {
        e.CellElement.DrawFill = true;
        e.CellElement.BackColor = Color.Yellow;
        e.CellElement.GradientStyle = Telerik.WinControls.GradientStyles.Solid;
    }
    else
    {
        e.CellElement.ResetValue(LightVisualElement.DrawFillProperty, ValueResetFlags.Local);
        e.CellElement.ResetValue(LightVisualElement.BackColorProperty, ValueResetFlags.Local);
        e.CellElement.ResetValue(LightVisualElement.GradientStyleProperty, ValueResetFlags.Local);
    }
}


I hope this information helps. If you need any further assistance please don't hesitate to contact me. 

Regards,
Dess | Tech Support Engineer, Sr.
Progress Telerik
Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
0
Brandon
Top achievements
Rank 1
answered on 01 Feb 2019, 05:01 PM

Thank you for your response.  Are you able to give an example of how to order the grouping based off of what the string value is?  I can't seem to get a real understanding of how the comparison works.  I just want to say:

 

Group Name = Position 1 (At the top)

Group Description = Position 2 (Directly underneath the top)

Any other group = Alphabetically, or in any order, really.  Doesn't matter. 3, 4, 5, 6, 7, 8

0
Brandon
Top achievements
Rank 1
answered on 01 Feb 2019, 05:46 PM

Here is what I have tried.  You'll see that "Pump" and "Pump Down" are manually added to the list in position 0 and 1, respectively.  The rest are just thrown in.  I can't see what I'm doing wrong here.

 

Public Class GroupComparer
    Implements IComparer(Of Group(Of GridViewRowInfo))
    Public Function [Compare](x As Group(Of GridViewRowInfo), y As Group(Of GridViewRowInfo)) As Integer _
    Implements IComparer(Of Group(Of GridViewRowInfo)).[Compare]
 
        Dim XVal As Integer = 0
        Dim YVal As Integer = 0
 
        Dim Items As New List(Of String)
        Items.Add("Pump")
        Items.Add("Pump Down")
 
        Dim data As DataSet = Grid.lvEquip.Tag
        If RecHasData(data) Then
            For i As Integer = 0 To data.Tables(0).Rows.Count - 1
                If Data.Tables(0).Rows(i).Item(1) <> "Pump" And Data.Tables(0).Rows(i).Item(1) <> "Pump Down" Then
                    Items.Add(Data.Tables(0).Rows(i).Item(1))
                End If
            Next
        End If
 
        For I As Integer = 0 To Items.Count - 1
            If Items(I) = x.Key Then XVal = I
            If Items(I) = y.Key Then YVal = I
        Next
 
        Dim Result As Integer = XVal.CompareTo(YVal)
 
        Dim xGroup As DataGroup = TryCast(x, DataGroup)
        If xGroup IsNot Nothing Then
            If xGroup.GroupDescriptor.GroupNames.First().Direction = ListSortDirection.Descending Then
                Result *= -1
            End If
        End If
        Return Result
 
    End Function
End Class

0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 05 Feb 2019, 12:55 PM
Hello, Brandon,        
 
Your question has already been answered in the support thread you have opened on the same topic. Please, see our answer there for more information. Feel free to continue the communication regarding the groups order in the relevant thread. Thus, it would be easier for you to track the different case.

However, I am posting the answer here as well in order the community to benefit from it. Note that if you use custom grouping, you can define the GroupKey in the CustomGrouping event and add a number indicating the desired order. Then, in the GroupSummaryEvaluate event you can trim the number and display the text only. You can find below a sample code demonstrating how to force USA to be displayed as first group when grouping by ShipCountry:

Private Sub RadForm1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Me.OrdersTableAdapter.Fill(Me.NwindDataSet.Orders)
    Me.RadGridView1.BestFitColumns(Telerik.WinControls.UI.BestFitColumnMode.AllCells)
  
    Me.RadGridView1.EnableCustomGrouping = True
    Dim descriptor As New GroupDescriptor("ShipCountry")
    Me.RadGridView1.GroupDescriptors.Add(descriptor)
End Sub
  
Private Sub RadGridView1_CustomGrouping(ByVal sender As Object, ByVal e As Telerik.WinControls.UI.GridViewCustomGroupingEventArgs) Handles RadGridView1.CustomGrouping
    Dim country As String = e.Row.Cells("ShipCountry").Value.ToString()
    Select Case country
        Case "USA"
            e.GroupKey = "1. USA"
            Exit Select
        Case "Japan"
            e.GroupKey = "2. Belgium"
            Exit Select
        Case "Germany"
            e.GroupKey = "3. Germany"
            Exit Select
        Case Else
            e.GroupKey = "Other country"
            Exit Select
    End Select
End Sub


I hope this information helps. If you need any further assistance please don't hesitate to contact me. 

Regards,
Dess | Tech Support Engineer, Sr.
Progress Telerik
Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
Tags
GridView
Asked by
Brandon
Top achievements
Rank 1
Answers by
Dimitar
Telerik team
Brandon
Top achievements
Rank 1
Dess | Tech Support Engineer, Principal
Telerik team
Share this question
or