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

Total number of rows in a group

6 Answers 635 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Guy
Top achievements
Rank 1
Guy asked on 25 Jan 2011, 01:09 AM
Hi,

I'm sure this is a quick issue but i'm struggling to find a solution on my own. Basically I used to have it where I would create a group and have the total number of rows within that group inside the group head. I used to use the following code

private void radGridView1_GroupSummaryEvaluate(object sender, GroupSummaryEvaluationEventArgs e)
{
    int contactsCount = e.Group.ItemCount;
    e.SummaryItem.Name = contactsCount.ToString();
}

But now that i've returned to my project after a break and updated to the latest controls, this no longer seems to function. Have I made an error somewhere or has the procedure completely changed with the latest version?

Regards,


Guy

6 Answers, 1 is accepted

Sort by
0
Richard Slade
Top achievements
Rank 2
answered on 25 Jan 2011, 10:32 AM
Hello Guy,

What you need is a Summary Row. Please have a look at this documentation on Summary Rows which will guide you through adding a summary row. If you need more help, just let me know
regards,
Richard
0
Richard Slade
Top achievements
Rank 2
answered on 26 Jan 2011, 04:23 PM
Hello,

Did this help? If so please remember to mark as answer. If you need more information though, just let me know
thanks
Richard
0
Guy
Top achievements
Rank 1
answered on 31 Jan 2011, 10:57 PM
Hi Richard,

Sorry for getting back to you late. Before my original post, I had tried the Summary Row and also Group Aggregates as that appeared to be similar to what I was used to. Unfortunately both produce issues.

With the Summary Rows, I get no value for row count when group, just the grey top bar. When I remove the grouping I suddenly get a couple of bars on the main gridview showing the row count. If I go back into a group I get the same blank row along the top for that collapsed group and again once removed, more copies of the header appear in the main gridview.

The code I am using for that is as follows.

private void radGridView1_GroupByChanged(object sender, GridViewCollectionChangedEventArgs e)
{
    GridViewSummaryItem summaryItem = new GridViewSummaryItem();
    summaryItem.Name = "Owner";
    summaryItem.Aggregate = GridAggregateFunction.Count;
    GridViewSummaryRowItem summaryRowItem = new GridViewSummaryRowItem();
    summaryRowItem.Add(summaryItem);
    this.radGridView1.SummaryRowsTop.Add(summaryRowItem);
}

I suspect it has something to do with the GroupByChanged event. Is this correct? I was trying to use GroupSummaryEvaluate but that would create loads of multiple header rows for each group!

Then like I said, I tried to use Group Aggregates from here http://www.telerik.com/help/winforms/gridview-grouping-group-aggregates.html but when used with GroupSummaryEvaluate it would spawn multiple groups of the same type (Owner). If I used GroupByChanged it would just hang. For that code I used the following.

private void radGridView1_GroupSummaryEvaluate(object sender, GroupSummaryEvaluationEventArgs e)
{
      
    GroupDescriptor descriptor = new GroupDescriptor();
    descriptor.GroupNames.Add("Owner", ListSortDirection.Ascending);
    descriptor.Aggregates.Add("Count(Owner)");
    descriptor.Format = "{0}: {1} has {2} records in its group.";
    this.radGridView1.GroupDescriptors.Add(descriptor);
}

Not really sure where I've gone wrong. Basically all I need is the name for the group to show the group name and the number of rows inside it no matter what I use to group.

Any pointers would be greatly appreciated!

Regards,

Guy
0
Accepted
Richard Slade
Top achievements
Rank 2
answered on 01 Feb 2011, 12:56 AM
Hello,

I've included a basic example. Lets say you have 2 columns in your grid. Name and Id. If you have a summary item on Name that shows the number of rows, then if you group by Id, then it will still show you the number of rows at the bottom. In this samepl I have added the summary item onto both columns so it shows the row count which ever column you group by

I hope this example is what you need, but if not, then please let me know.

Designer File
namespace RadGridView_Basic_C
{
    partial class Form1
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components;
  
        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }
  
        #region Windows Form Designer generated code
  
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.radGridView1 = new Telerik.WinControls.UI.RadGridView();
            ((System.ComponentModel.ISupportInitialize)(this.radGridView1)).BeginInit();
            this.SuspendLayout();
            // 
            // radGridView1
            // 
            this.radGridView1.Location = new System.Drawing.Point(0, 0);
            this.radGridView1.Name = "radGridView1";
            this.radGridView1.Size = new System.Drawing.Size(457, 510);
            this.radGridView1.TabIndex = 0;
            this.radGridView1.Text = "radGridView1";
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(457, 556);
            this.Controls.Add(this.radGridView1);
            this.Name = "Form1";
            this.Text = "Form1";
            this.Load += new System.EventHandler(this.Form1_Load);
            ((System.ComponentModel.ISupportInitialize)(this.radGridView1)).EndInit();
            this.ResumeLayout(false);
  
        }
  
        #endregion
  
        private Telerik.WinControls.UI.RadGridView radGridView1;
    }
}

Form1.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Telerik.WinControls.UI;
using Telerik.WinControls;
using Telerik.WinControls.Data;
using System.Globalization;
  
  
namespace RadGridView_Basic_C
{
    public partial class Form1 : Form
    {
  
        private List<Person> m_myList = new List<Person>();
  
  
        public Form1()
        {
            InitializeComponent();
        }
  
        private void Form1_Load(object sender, EventArgs e)
        {
  
            this.radGridView1.AutoGenerateColumns = false;
  
            m_myList.Add(new Person(10, "Richard"));
            m_myList.Add(new Person(20, "Stew"));
            m_myList.Add(new Person(30, "Chris"));
            m_myList.Add(new Person(40, "Peter"));
            radGridView1.DataSource = m_myList;
  
            GridViewDecimalColumn idColumn = new GridViewDecimalColumn();
            idColumn.Name = "Id";
            idColumn.HeaderText = "Id";
            idColumn.FieldName = "Id";
            this.radGridView1.Columns.Add(idColumn);
  
            GridViewTextBoxColumn nameColumn = new GridViewTextBoxColumn();
            nameColumn.Name = "Name";
            nameColumn.HeaderText = "Name";
            nameColumn.FieldName = "Name";
            this.radGridView1.Columns.Add(nameColumn);
  
  
            this.radGridView1.EnableGrouping = true;
  
  
            GridViewSummaryItem summaryItem1 = new GridViewSummaryItem("Name", "Rows: {0}", GridAggregateFunction.Count);
            GridViewSummaryItem summaryItem2 = new GridViewSummaryItem("Id", "Rows: {0}", GridAggregateFunction.Count);
            GridViewSummaryRowItem summartRowItem = new GridViewSummaryRowItem();
            summartRowItem.Add(summaryItem1);
            summartRowItem.Add(summaryItem2);
            this.radGridView1.SummaryRowsBottom.Add(summartRowItem);
      
            this.radGridView1.GroupSummaryEvaluate += new GroupSummaryEvaluateEventHandler(radGridView1_GroupSummaryEvaluate);
  
            this.radGridView1.AutoExpandGroups = true;
            GroupDescriptor descriptor = new GroupDescriptor();
            descriptor.GroupNames.Add("Id", ListSortDirection.Ascending);
            this.radGridView1.GroupDescriptors.Add(descriptor);
        }
  
        void radGridView1_GroupSummaryEvaluate(object sender, GroupSummaryEvaluationEventArgs e)
        {
            if (e.Parent == this.radGridView1.MasterTemplate)
            {
                e.FormatString = String.Format("Rows: {0}", e.Value);
            }
        }
  
  
        class Person
        {
            public Person()
            { }
  
            public Person(decimal id, string name)
            {
                this.Id = id;
                this.Name = name;
            }
  
            public decimal Id { get; set; }
            public string Name { get; set; }
  
        }
  
    }
  
}

Regards
Richard
0
Guy
Top achievements
Rank 1
answered on 01 Feb 2011, 01:57 PM
Richard,

First of all thank you for taking the time out to help me with my issue. I have taken the above example and have made the following solution for my needs.

private void radGridView1_GroupSummaryEvaluate(object sender, GroupSummaryEvaluationEventArgs e)
{
      
    if (e.Parent != this.radGridView1.MasterTemplate)
    {
        e.FormatString = string.Format("{0} - "+e.Group.ItemCount+" records found.", e.Value);
    }
}


Thanks again,

Guy
0
Richard Slade
Top achievements
Rank 2
answered on 01 Feb 2011, 02:24 PM
You're welcome Guy.. I'm glad that the exmaple helped you solve this.
All the best
Richard
Tags
GridView
Asked by
Guy
Top achievements
Rank 1
Answers by
Richard Slade
Top achievements
Rank 2
Guy
Top achievements
Rank 1
Share this question
or