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

Custom grouping, group by on more than one level

2 Answers 138 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Lars
Top achievements
Rank 1
Lars asked on 09 Oct 2012, 06:38 AM
I am trying to implement custom grouping and can't get it working when i group on more than one level. Can you provide an example on how this ca be done.

2 Answers, 1 is accepted

Sort by
0
Julian Benkov
Telerik team
answered on 10 Oct 2012, 02:59 PM
Hello Lars,

You can use the Level argument of the CustomGrouping event handler to implement a custom grouping operation for more levels. Here is an example:

using System;
using System.Data;
using System.Windows.Forms;
using Telerik.WinControls.UI;
 
namespace Lab.Grid
{
    public partial class GridCustomGroupingForm : Form
    {
        private RadGridView gridView = new RadGridView();
 
        public GridCustomGroupingForm()
        {
            InitializeComponent();
 
            gridView.Dock = DockStyle.Fill;
            gridView.Parent = this;
            gridView.EnableCustomGrouping = true;
            gridView.CustomGrouping += gridView_CustomGrouping;
 
            DataTable table = new DataTable();
            table.Columns.Add("Id");
            table.Columns.Add("Name");
            table.Columns.Add("Col3");
 
            table.Rows.Add(1, "Name1");
            table.Rows.Add(2, "Name12");
            table.Rows.Add(3, "Name12");
            table.Rows.Add(4, "Name1234");
            table.Rows.Add(5, "Name123");
            table.Rows.Add(6, "Name123456");
            table.Rows.Add(7, "Name1234567");
            table.Rows.Add(8, "Name123456");
 
            gridView.DataSource = table;
        }
 
        void gridView_CustomGrouping(object sender, GridViewCustomGroupingEventArgs e)
        {
            string colName = gridView.GroupDescriptors[e.Level].GroupNames[0].PropertyName;
 
            if (colName == "Id")
            {
                int val = Convert.ToInt32(e.Row.Cells[colName].Value);
                e.GroupKey = val % 2;
            }
            else if (colName == "Name")
            {
                e.GroupKey = e.Row.Cells[colName].Value.ToString().Length;
            }
        }
    }
}

I hope this helps.

Kind regards,
Julian Benkov
the Telerik team
RadControls for WinForms Q2'12 release is now live! Check out what's new or download a free trial >>
0
Lars
Top achievements
Rank 1
answered on 11 Oct 2012, 01:04 PM
This was what I was looking for, thank you!
Tags
GridView
Asked by
Lars
Top achievements
Rank 1
Answers by
Julian Benkov
Telerik team
Lars
Top achievements
Rank 1
Share this question
or