Set GroupByExpression on Drag and Drop


You can dynamically alter the GroupByExpression that is added when the user drags a column header to the group panel.

To change the GroupByExpression dynamically when the user drags a column header to the group panel, use the GroupsChanging server-side event. This is illustrated in the following example:

<telerik:RadGrid RenderMode="Lightweight" ID="RadGrid1" runat="server" DataSourceID="SqlDataSource1" ShowGroupPanel="true"
  <ClientSettings AllowDragToGroup="True" />
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
  SelectCommand="SELECT * FROM Orders">
protected void RadGrid1_GroupsChanging(object source, GridGroupsChangingEventArgs e)
{  //Expression is added (by drag/drop on group panel)
    if (e.Action == GridGroupsChangingAction.Group)
        if (e.Expression.GroupByFields[0].FieldName != "CustomerID")
            if (!IsGroupedByCustomer(RadGrid1.MasterTableView.GroupByExpressions))
                GridGroupByField customerGroupField = new GridGroupByField();
                customerGroupField.FieldName = "CustomerID";
                e.Expression.SelectFields.Insert(0, customerGroupField);
                e.Expression.GroupByFields.Insert(0, customerGroupField);
private Boolean IsGroupedByCustomer(GridGroupByExpressionCollection groups)
    foreach (GridGroupByExpression e in groups)
        foreach (GridGroupByField f in e.GroupByFields) if (f.FieldName == "CustomerID")
                return true;
    return false;
Protected Sub RadGrid1_GroupsChanging(ByVal source As Object, ByVal e As GridGroupsChangingEventArgs)
    'Expression is added (by drag/drop on group panel)
    If e.Action = GridGroupsChangingAction.Group Then
        If e.Expression.GroupByFields(0).FieldName <> "CustomerID" Then
            If Not IsGroupedByCustomer(RadGrid1.MasterTableView.GroupByExpressions) Then
                customerGroupField.FieldName = "CustomerID"
                e.Expression.SelectFields.Insert(0, customerGroupField)
                e.Expression.GroupByFields.Insert(0, customerGroupField)
            End If
        End If
    End If
End Sub

Private Function IsGroupedByCustomer(ByVal groups As GridGroupByExpressionCollection) As Boolean
    For Each e As GridGroupByExpression In groups
        For Each f As GridGroupByField In e.GroupByFields
            If f.FieldName = "CustomerID" Then
                Return True
            End If
    Return False
End Function
In this article
Not finding the help you need? Improve this article