Click here to Skip to main content
15,170,651 members
Articles / Desktop Programming / Windows Forms
Posted 29 May 2015


34 bookmarked


Rate me:
Please Sign up or sign in to vote.
4.93/5 (26 votes)
29 May 2015CPOL3 min read
DataGridViewGrouper: add grouping functionality to the .NET DataGridView


The DataGridViewGrouper is a component that can add grouping functionality to any existing DataGridView. The component can be added in the designer or in code. Optionally, a control can be used (DataGridViewGrouperControl) to provide a user interface to alter the grouping.


The component was something I wrote in 2008 and published on (no longer available). That was a primary version and although it hasn't been updated in a long time now, the component and its controls are something that I use every day in custom applications and contains a lot of functionality the old version did not.

Using the Code

To simply start grouping, the component can be added in designer, or on the fly. Grouping can be started with one of the appropriate functions, or by attaching a custom grouper.

All the code needed to make a grid grouped on a property (in this example called AString):

var grouper = new Subro.Controls.DataGridViewGrouper(dataGridView1);

which creates something like:

Image 1

N.B.: The test project generates random data, so the displayed data will vary.

The miscellaneous options for formatting/sorting/collapsing/etc. can be set either in code, or with the included wrapper control:

Image 2

The dropdown contains the columns from whatever source the grid is bound to:

Image 3

And the various options can be set via the secondary options button:

Image 4

The options can of course be set in code, but also with the help of an included toolstripmenuitem which can be added to a contextmenu or other menu.

In the example, a string was used, but the same can be accomplished by using a PropertyDescriptor/DataGridViewColumn or lambda expression. For example, these do the same as using the string:

grouper.SetGroupOn<TestData>(t => t.AString);

Also, a custom grouper can be assigned to the GroupOn property (any class inheriting GroupingInfo. This could be changed to an interface later on if needed.

Or by using the SetCustomGroup method:

grouper.SetCustomGroup<TestData>(t => t.AnInt % 10, "Mod 10");

Image 5

Some further examples:

//to start with all rows collapsed on a (re)load or 
//when the group is changed you can set the option startcollapsed:
grouper.Options.StartCollapsed = true;

//to collapse all loaded rows: (the difference with setting the option above, 
//is that after choosing a new grouping (or on a reload), the new groups would expand.

//if you don't want the (rowcount) to be shown in the headers:
grouper.Options.ShowCount = false;

//if you don't want the grouped column name to be repeated in the headers:
grouper.Options.ShowGroupName = false;

//default sort order for the groups is ascending, 
//you can change that in the options as well (ascending, descending or none)
grouper.Options.GroupSortOrder = SortOrder.Descending;

An example of collapse all:

Image 6

The component exposes a GroupingChanged event to catch when the grouping was changed or removed. And a DisplayGroup event which is fired whenever one of the grouping rows is being painted. The DisplayGroup event can be used to further customize whatever has to be shown:

//grouper.DisplayGroup += grouper_DisplayGroup;
void grouper_DisplayGroup(object sender, GroupDisplayEventArgs e)
    e.BackColor = (e.Group.GroupIndex % 2) == 0 ? Color.Orange : Color.LightBlue;
    e.Header = "[" + e.Header + "], grp: " + e.Group.GroupIndex;
    e.DisplayValue = "Value is " + e.DisplayValue;
    e.Summary = "contains " + e.Group.Count + " rows";

The snippet above would produce something like:

Image 7

Points of Interest

The class library is an extract from a larger control library. One of the things still missing here is a quick grouping context menu item, which sets the grouping on the column where the right click took place and is the way grouping is used most in the applications I've used so far. However, the context menu is a bit too integrated in the library and its custom ORM to quickly add here. If interest exists, I will add that later on, including its quick filter options.

Another note: The library also contains some searchboxes (for DataGridView/Treeview and bindingsource in general. Not documented here, but fully usable. Just drop onto a form and attach the appropriate control.


  • 2008: First draft on vbcity blog
  • 2015-05-28 First publish of current production version


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Software Developer
Netherlands Netherlands
No Biography provided

Comments and Discussions

AnswerRe: Unable to cast object of type 'Subro.Controls.GroupRow' to type 'System.Data.DataRowView'. Pin
wasniewc2-May-17 13:25
Memberwasniewc2-May-17 13:25 
QuestionIs there a method to group by multiple columns of a datatable Pin
Member 1056115613-Sep-16 8:02
MemberMember 1056115613-Sep-16 8:02 
QuestionRefresh causes Object reference not set to an instance of an object Pin
paperboy30-Jul-16 6:10
Memberpaperboy30-Jul-16 6:10 
AnswerRe: Refresh causes Object reference not set to an instance of an object Pin
GBriotti8-Mar-21 0:32
MemberGBriotti8-Mar-21 0:32 
Questionproblem: Pin
Member 118312927-Feb-16 4:04
MemberMember 118312927-Feb-16 4:04 
QuestionNot working 2016 Pin
Nerexis19-Jan-16 14:17
MemberNerexis19-Jan-16 14:17 
QuestionSummaries Pin
Member 1208329623-Nov-15 4:08
MemberMember 1208329623-Nov-15 4:08 
AnswerRe: Summaries Pin
wasniewc4-May-17 12:02
Memberwasniewc4-May-17 12:02 
did you make a row Sum function? i could use it as well.
GeneralMy vote of 5 Pin
Juan Aguilera Ramos22-Oct-15 21:31
MemberJuan Aguilera Ramos22-Oct-15 21:31 
QuestionGroup /Sub Group on multiple properties Pin
m4dh016-Sep-15 5:52
Memberm4dh016-Sep-15 5:52 
BugInvalidCastException Pin
ricpulvi2-Sep-15 5:48
Memberricpulvi2-Sep-15 5:48 
GeneralRe: InvalidCastException Pin
Robert.Verpalen2-Sep-15 6:03
MemberRobert.Verpalen2-Sep-15 6:03 
GeneralRe: InvalidCastException Pin
ricpulvi2-Sep-15 6:15
Memberricpulvi2-Sep-15 6:15 
GeneralRe: InvalidCastException Pin
ricpulvi3-Sep-15 0:27
Memberricpulvi3-Sep-15 0:27 
GeneralRe: InvalidCastException Pin
Robert.Verpalen3-Sep-15 9:33
MemberRobert.Verpalen3-Sep-15 9:33 
GeneralRe: InvalidCastException Pin
ricpulvi3-Sep-15 22:15
Memberricpulvi3-Sep-15 22:15 
QuestionGrid Row/Text Color Pin
ricpulvi2-Sep-15 3:43
Memberricpulvi2-Sep-15 3:43 
AnswerRe: Grid Row/Text Color Pin
Robert.Verpalen2-Sep-15 6:04
MemberRobert.Verpalen2-Sep-15 6:04 
GeneralRe: Grid Row/Text Color Pin
ricpulvi2-Sep-15 6:08
Memberricpulvi2-Sep-15 6:08 
QuestionSystem.NullReferenceException Pin
Member 43148592-Sep-15 0:30
MemberMember 43148592-Sep-15 0:30 
AnswerRe: System.NullReferenceException Pin
Robert.Verpalen2-Sep-15 6:05
MemberRobert.Verpalen2-Sep-15 6:05 
QuestionCorrect Row number Pin
javon2713-Jul-15 6:24
professionaljavon2713-Jul-15 6:24 
AnswerRe: Correct Row number Pin
javon2713-Jul-15 6:30
professionaljavon2713-Jul-15 6:30 
GeneralRe: Correct Row number Pin
Robert.Verpalen16-Jul-15 22:32
MemberRobert.Verpalen16-Jul-15 22:32 
QuestionWorks in C#.NET only, not working on VB.NET Pin
Member 1152198226-Jun-15 4:48
MemberMember 1152198226-Jun-15 4:48 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.