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

Redundant count display for CountFunction

3 Answers 100 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Twistur
Top achievements
Rank 1
Twistur asked on 21 Dec 2011, 03:16 AM
Hi, I'm dealing with a format issue in a CountFunction. Basically, we want to display the count of the rows as a result of a grouping. I do this in the Grouping and Grouped event as shown below:

private void gridView_Grouping(object sender, GridViewGroupingEventArgs e)
       {
                   var descriptor = e.GroupDescriptor as ColumnGroupDescriptor;
                   if (descriptor != null)
                   {                       
                                     
                  
                       var functions = descriptor.Column.AggregateFunctions;
                       if (functions.Count == 0)
                       {
                           functions.Add(new CountFunction());
                       }                 
                   }           
       }      
 
       private void gridView_Grouped(object sender, GridViewGroupedEventArgs e)
       {
           if (e.Action == GroupingEventAction.Remove)
           {
               ((ColumnGroupDescriptor)(e.GroupDescriptor)).Column.AggregateFunctions.Clear();
           }
       }


I'm sorry if the format is somewhat whacked. The result of the code is something like this, (in the group header)

Column A  5   5   5
       Column B  2   2   2
             Column C  3   3   3
     
When the desired format should be

Column A  5
     Column B  2
           Column C 3

What exactly is my code doing wrong? Is this by design?


Thanks,
Gio

3 Answers, 1 is accepted

Sort by
0
Vlad
Telerik team
answered on 21 Dec 2011, 09:13 AM
Hello,

 Here is an example how to achieve your goal:

        private void RadGridView_Grouping(object sender, GridViewGroupingEventArgs e)
        {
            if (((RadGridView)(sender)).AggregateResults.Count == 0)
            {
                var descriptor = e.GroupDescriptor as ColumnGroupDescriptor;
                if (descriptor != null)
                {
                    var functions = descriptor.Column.AggregateFunctions;
                    if (functions.Count == 0)
                    {
                        functions.Add(new CountFunction());
                    }
                }
            }
        }

All the best,
Vlad
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

0
Twistur
Top achievements
Rank 1
answered on 21 Dec 2011, 08:07 PM
Thanks Vlad. This one works.

However if I have group descriptor defined in the markup, this one is not hit. We are required to give a default grouping. So I'm thinking of 2 options:

1) Adding the default group in the code. This will hit the Grouping event but exactly how, I'm not sure. This one does not work for me:
void gridView_DataLoaded(object sender, EventArgs e)
       {
           var selectedGroup = new GroupDescriptor { Member = "IsSelected", DisplayContent = "Selected" };
           listViewColumnChooser.GroupDescriptors.Add(selectedGroup );
           listViewColumnChooser.ExpandAllGroups();
      }
2) Continue with default grouping in the markup and just add the Aggregate function. For this I still have to rummage the docs and forum for the syntax.

Whichever you think is the better approach, I would highly appreciate if you could guide me to achieving my goal.

Thanks
0
Twistur
Top achievements
Rank 1
answered on 21 Dec 2011, 10:29 PM
I got this one already. Did the first option with the help of the docs.

Tags
GridView
Asked by
Twistur
Top achievements
Rank 1
Answers by
Vlad
Telerik team
Twistur
Top achievements
Rank 1
Share this question
or