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

Hide a Group in the Group By

1 Answer 152 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Paul
Top achievements
Rank 1
Paul asked on 17 Aug 2014, 10:54 AM
Hello Again...

I want to be able to hide a Group in the Group By.

I am binding the grid to a custom business object.  For example:

​class PermissionBinding
{
     public string PermissionName { get; set; }
     public string PermissionGroup { get; set; }
}

And the data in a BindingList<PermissionBinding> (#id, Permission Name, Permission Group) has:
#0:    Create, Group 1
#1:    Edit, Group 1
#2:    View, Group 1
#3:    Delete, Group 1
​#4:    Create, Group 2
#5:    Edit, Group 2
#6:    View, Group 2
#7:    Delete, Group 2

... and so on.

So I bind the list to the grid via a BindingSource (all set up via the designer).  But I create my list and set it to the binding source using:

_permissions = CreatePermissionBinding();
this.permissionBindingBindingSource.DataSource = _permissions;

I use Custom Grouping to group the rows under each Permission group nicely using the following:

Field Members (used to order the Groups and format the group name)
private const string GroupSortFormat = "{0:D4}. {1}|{1}";
        private Dictionary<string, int> _groupOrder = new Dictionary<string, int>()
        {
            { "Group 1", 1 },
            { "Group 2", 2 },
            { "Group 3", 3 },
       };

Form Constructor
this.PermissionGridView.CustomGrouping += PermissionGridView_CustomGrouping;
this.PermissionGridView.GroupSummaryEvaluate += PermissionGridView_GroupSummaryEvaluate;
this.PermissionGridView.EnableCustomGrouping = true;            
this.PermissionGridView.GroupDescriptors.Add(new Telerik.WinControls.Data.GroupDescriptor("PermissionGroup"));

Event Handlers

        private void PermissionGridView_GroupSummaryEvaluate(object sender, Telerik.WinControls.UI.GroupSummaryEvaluationEventArgs e)
        {
            if (e.Value == null)
            {
                e.FormatString = e.Group.Key.ToString().Split('|')[1]; // strip out the order from the format string to show the group name
            }
        }
        
        private void PermissionGridView_CustomGrouping(object sender, Telerik.WinControls.UI.GridViewCustomGroupingEventArgs e)
        {
            var obj = (PermissionBinding)e.Row.DataBoundItem;
            e.GroupKey = string.Format(GroupSortFormat, _groupOrder[obj.PermissionGroup], obj.PermissionGroup); // make sure group is ordrered via string format
        }

Now suppose I want to hide Group 2.  Can I do it?

I have tried the following which only hides the rows in a group - it still shows the grouping Group 2:
foreach (var row in this.PermissionGridView.Rows)
{
        row.IsVisible = groups.Contains((row.DataBoundItem as PermissionBinding).PermissionGroup) ? true : false;


I have successfully done it by removing the groups from the binding source BindingList<PermissionBinding>.  But I see this as a view/filter on the data - suppose this is triggered by a user clicking a button.  I really don't want to create the data source each time as it is populated through the database.  Although it is not a major issue.  I could maintain 2 lists - one main list and one filtered.  But I was wondering if there was something in the Grid I had obviously missed to do this.

NOTE:  Telerik is seriously kicking a competitors product into touch the more I do with it ;)!

Thanks,

Andez

1 Answer, 1 is accepted

Sort by
0
Dimitar
Telerik team
answered on 20 Aug 2014, 10:34 AM
Hello Andez,

Thank you for writing.

In this case you can hide the group row (assuming the groups collection contains the groups that you want to hide):
List<string> groups = new List<string>();
 
private void button1_Click(object sender, EventArgs e)
{
    groups.Add("Group 1");
 
    foreach (var row in this.radGridView1.Rows)
    {
        if (groups.Contains((row.DataBoundItem as PermissionBinding).PermissionGroup))
        {
            row.Group.GroupRow.IsVisible = false;         
        }
    }
}

I hope this helps.

Regards,
Dimitar
Telerik
 
Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
 
Tags
GridView
Asked by
Paul
Top achievements
Rank 1
Answers by
Dimitar
Telerik team
Share this question
or