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

[Solved] How to hide empty Group Headers

2 Answers 422 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Jorge
Top achievements
Rank 1
Jorge asked on 12 Oct 2009, 01:40 PM
Hello,
I have a grid with Grouping enabled that is populated programatically, and during the ItemDataBound event, items that do not meet a certain criteria are set to Visible = False.
When the grid is rendered, there may be group headers that do not contain any items, if none of the items satisfied my criteria, so in those cases I need those groups to be hidden or removed. I only want the user to see and expand/collapse group headers that actually contain items within.

How can I do this?
Should I remove the items during ItemDataBound instead of setting them to Visible = False, so that no Group Header is generated? If so, what is the best way to do this?

Thanks in advance.

2 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 13 Oct 2009, 12:46 PM
Hello Jorge,

I suppose you want to hide the GroupHeaderItems of the grid when these do not have any child items. If thats the case, then you can try out the following code:
c#:
 protected void RadGrid1_PreRender(object sender, EventArgs e) 
    { 
        foreach (GridGroupHeaderItem groupheader in RadGrid1.MasterTableView.GetItems(GridItemType.GroupHeader)) 
        { 
            GridItem[] item = groupheader.GetChildItems(); 
            if (item.Length < 1) 
            { 
                groupheader.Visible = false
            } 
        } 
 
    } 

Thanks
Princy.
0
Jorge
Top achievements
Rank 1
answered on 13 Oct 2009, 02:00 PM
Thank you much Princy.
While your sugestion did not solve the issue directly, it pointed me in the right direction.
Since the unwanted GridItems in given groups were only set to Visible = False in the previous event Sub, they were still present, so the GetChildItems count alone was not sufficient. Here is my modification, which worked like a charm:

    Protected Sub HideEmptyGroupHeader(ByVal Grid As RadGrid)  
        For Each groupheader As GridGroupHeaderItem In Grid.MasterTableView.GetItems(GridItemType.GroupHeader)  
            Dim flag As New Boolean  
 
            For Each I As GridItem In groupheader.GetChildItems()  
                If I.Visible Then  
                    flag = True 
                    Exit For  
                End If  
            Next  
 
            groupheader.Visible = flag 
        Next  
    End Sub  

Thanks again.
Jorge
Tags
Grid
Asked by
Jorge
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Jorge
Top achievements
Rank 1
Share this question
or