Show/hide grouped columns on group/ungroup action

Thread is closed for posting
2 posts, 0 answers
  1. Telerik Admin
    Telerik Admin avatar
    1679 posts
    Member since:
    Oct 2004

    Posted 16 Aug 2007 Link to this post

    Requirements

    RadGrid for ASP .NET version

    RadControls for ASP .NET AJAX version

    4.6.2 and later


    2008.1.415 and later

    .NET version

    2.0 and later

    Visual Studio version

    2005 and later

    Programming language

    C#, VB.NET

    Browser support

    all supported by RadGrid for ASP .NET


    all browsers supported by RadControls for ASP .NET AJAX


     
    PROJECT DESCRIPTION
    This demo illustrates how to hide the grouped column in RadGrid and then show it again when ungroup action is progressed. The solution is pretty straight-forward:
    • intercept the GroupsChanging event
    • check the action which is about to be executed (group or ungroup)
    • designate the column in progress using the group expression's GroupByFields collection
    • switch the visibility of that column
        Protected Sub RadGrid1_GroupsChanging(ByVal source As ObjectByVal e As Telerik.WebControls.GridGroupsChangingEventArgs) Handles RadGrid1.GroupsChanging  
            If (e.Action = GridGroupsChangingAction.Group) Then 
                RadGrid1.MasterTableView.GetColumnSafe(e.Expression.GroupByFields(0).FieldName).Visible = False 
            ElseIf (e.Action = GridGroupsChangingAction.Ungroup) Then 
                RadGrid1.MasterTableView.GetColumnSafe(e.Expression.GroupByFields(0).FieldName).Visible = True 
            End If 
        End Sub 
  2. Leon
    Leon avatar
    373 posts
    Member since:
    Apr 2007

    Posted 31 Dec 2007 Link to this post

    The example is great, thank you! This is exactly what I was looking for.

    One note, though. The code above requires unique column name matching the GroupBy FieldName. For some reason one may need to change the unique column name, which will cause NullReferenceException. Imagine a data extraction from two data tables in a single Grid for example.

    A possible solution is switch-case like the code below:

    RadGrid1_GroupsChanging...
    {
        String columnName = null;
        switch(e.Expression.GroupByFields[0].FieldName)
        {
           case ("myFieldName1"):
           {
              columnName = "myColumnName1";
              break;
            }
           case ("myFieldName2"):
           {
              columnName = "myColumnName2";
              break;
            }
            ...
        } //end switch

        RadGrid1.MasterTableView.GetColumnSafe(columnName)...
    }


    Hope it was helpful and Happy New Year!

    -Leon


Back to Top