Click here to Skip to main content
13,259,771 members (51,741 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


84 bookmarked
Posted 31 Jul 2008

DataGridView with hierarchical data binding

Rate this:
Please Sign up or sign in to vote.
The TreeGridView by Mark Rideout with data binding and sorting


The TreeGridView by Mark Rideout (here's the link) is a great control. It allows us to display related data in a hierarchical form (a tree!), but I needed it to be sortable and support data binding.

The HierarchicalGrid does that, the quick and dirty way. It displays the table relations in a hierarchical form and sorts it through the levels too.

Also, this is my first contribution and any feedback would be greatly appreciated!


I would really recommend you see the TreeGridView.

Using the code

There's a new class called DataGridSource, and the DataSource of the HierarchicalGrid must be of that type.

It takes three arguments: the dataset, a list of display columns and a list of group columns. Now, what are those? The display columns are names of the table columns to be displayed, and the group columns are columns that group total values, like sum, average, product, etc.

Let's suppose that we have two tables, one with a list of people and their fruit preference, and another with where and when each person bought their favorite fruit.

For instance, the display columns would be:

List<string> displayColumns = new List<string>();

Now we have a list of columns to be displayed. What about the group columns? Let's set them:

List<GroupColumn> groupColumns = new List<GroupColumn>();
groupColumns.Add(new GroupColumn("Quantity", GroupTypeEnum.Sum));   

Pay attention to the GroupColumn type. It groups the name of the column and the type of operation that it should do on its child results. We added a new sum of quantities.

Okay! We have our display columns list and our group columns list. Let's get our dataset.

SqlConnection connection = new SqlConnection("Your connection string");

SqlDataAdapter dataAdapter = new SqlDataAdapter(
    "SELECT id, Name, Fruit FROM FruitPrefs",
DataTable dtResult1 = new DataTable();

dataAdapter = new SqlDataAdapter(
    "SELECT id, BoughtFrom, Date, Quantity FROM SalesRecords",
DataTable dtResult2 = new DataTable();

DataSet dsResults = new DataSet("Results");
DataRelation relation1 = new DataRelation(

To set the dataset, we must use one of the datatables' dataset when creating a new DataGridSource, like this:

DataGridSource newGridSource = new DataGridSource(
hierarchicalGridView1.DataSource = newGridSource; 

Hooray! That's it! It displays an hierarchical form of the dataset, with the relations.

Points of Interest

Two things here:

- When you set the dataset, it tries to find the best sequence of relations to fill the dataset. In other words, it works like matching a sequence of domino pieces. It has one solution and it tries to find it. It finds the top and the bottom relation, then it successively searches for the relation the matches the previous one.

- We can't let the DataGridView sort function work here, otherwise it would break the structure of the data. So, each time it is asked to sort, it clears the grid and adds the nodes again, in the proper order. That's clearly NOT efficient. This is where work must be done.


  • July 31, 2008 - Initial release


This article, along with any associated source code and files, is licensed under The Microsoft Public License (Ms-PL)


About the Authors

Joao Eduardo Grossi
Brazil Brazil
No Biography provided

No Biography provided

You may also be interested in...


Comments and Discussions

QuestionAdd Images Pin
Member 1299863116-Aug-17 20:07
memberMember 1299863116-Aug-17 20:07 
QuestionNo working as expected Pin
Member 24828243-Dec-15 23:30
memberMember 24828243-Dec-15 23:30 
Questionupdates? Pin
kiquenet.com13-Oct-14 1:53
memberkiquenet.com13-Oct-14 1:53 
QuestionWhere is code for HierarchicalGridView Pin
sacp.net24-Jul-14 9:26
membersacp.net24-Jul-14 9:26 
QuestionVery good! Pin
JLDevC#20-Oct-13 4:26
memberJLDevC#20-Oct-13 4:26 
QuestionHow to Set Right To Left Property when the Hierarchical Gridview Control Converts from English to Arabic Pin
archanakumari30-Dec-12 23:18
memberarchanakumari30-Dec-12 23:18 
Questiondatagridview with Hierachical Pin
thanhtungtrucgiang27-Dec-12 19:15
memberthanhtungtrucgiang27-Dec-12 19:15 
AnswerRe: datagridview with Hierachical Pin
Claudia Bolis2-Apr-13 6:28
memberClaudia Bolis2-Apr-13 6:28 
QuestionVery good Pin
DadajiIn15-Oct-12 16:01
memberDadajiIn15-Oct-12 16:01 
I like the article, really nice Hierarchical DataGridView Blush | :O simple and easy to follow !!
GeneralMy vote of 5 Pin
Member 382405323-Jul-12 21:44
memberMember 382405323-Jul-12 21:44 
QuestionUnable to cast type Pin
duphrx7-Aug-11 9:41
memberduphrx7-Aug-11 9:41 
Answerupdate Pin
joel_gil22-Sep-11 5:39
memberjoel_gil22-Sep-11 5:39 
AnswerRe: Unable to cast type Pin
Zeke Susman9-Apr-13 13:11
memberZeke Susman9-Apr-13 13:11 
QuestionBindingNavigator Pin
NetKoder27-Apr-11 4:15
memberNetKoder27-Apr-11 4:15 
GeneralJust what i needed Pin
Vincent Meijer27-Jan-11 5:34
memberVincent Meijer27-Jan-11 5:34 
GeneralMy vote of 5 Pin
Vincent Meijer27-Jan-11 5:32
memberVincent Meijer27-Jan-11 5:32 
GeneralNeed help for Drag & Drop Pin
bnsrinivas21-Jan-11 7:05
memberbnsrinivas21-Jan-11 7:05 
QuestionAny possibility to format a column as checkbox? Pin
andibaden9-Jan-11 4:39
memberandibaden9-Jan-11 4:39 
QuestionHow to Adjust row's height? Pin
Member 198466920-Sep-10 0:01
memberMember 198466920-Sep-10 0:01 
GeneralMy vote of 5 Pin
blaknar9-Aug-10 3:46
memberblaknar9-Aug-10 3:46 
QuestionThat just works with Windows XP Style?? Pin
bruno_bert27-Apr-10 4:58
memberbruno_bert27-Apr-10 4:58 
AnswerRe: That just works with Windows XP Style?? Pin
BassamKhalid6-Aug-11 20:55
memberBassamKhalid6-Aug-11 20:55 
GeneralIt doesn't work Pin
MetalPower18-Mar-10 13:13
memberMetalPower18-Mar-10 13:13 
GeneralRe: It doesn't work Pin
P0110X24-Jun-10 5:31
memberP0110X24-Jun-10 5:31 
GeneralRe: It doesn't work Pin
Member 422198424-Sep-11 6:03
memberMember 422198424-Sep-11 6:03 
QuestionVb Version? Pin
cleomc8-Dec-09 4:28
membercleomc8-Dec-09 4:28 
AnswerRe: Vb Version? Pin
Famous Mortimer15-Sep-15 3:22
memberFamous Mortimer15-Sep-15 3:22 
GeneralMy vote of 1 Pin
xpesdal1-Jul-09 2:21
memberxpesdal1-Jul-09 2:21 
GeneralWhere did GroupColumn Come From Pin
Lemuel.Adane3-Jun-09 0:48
memberLemuel.Adane3-Jun-09 0:48 
GeneralData Source Property Pin
Rizwan Tahir6-Apr-09 5:09
memberRizwan Tahir6-Apr-09 5:09 
GeneralRe: Data Source Property Pin
RajeevaSingh0111-Jun-09 2:29
memberRajeevaSingh0111-Jun-09 2:29 
GeneralRe: Data Source Property Pin
ashvani pal11-Jun-09 2:31
memberashvani pal11-Jun-09 2:31 
GeneralRegarding ur grid Pin
morshed_pintu3-Mar-09 0:56
membermorshed_pintu3-Mar-09 0:56 
GeneralRe: Regarding ur grid Pin
RajeevaSingh0111-Jun-09 2:31
memberRajeevaSingh0111-Jun-09 2:31 
GeneralIncorrect Redraw of Child Nodes after delete Pin
Jerry Dusing28-Nov-08 22:24
memberJerry Dusing28-Nov-08 22:24 
QuestionRe: Incorrect Redraw of Child Nodes after delete Pin
abczan1233-Apr-09 12:38
memberabczan1233-Apr-09 12:38 
Generalgreat work! Pin
Member 310882526-Nov-08 11:34
memberMember 310882526-Nov-08 11:34 
GeneralRe: great work! Pin
RajeevaSingh0111-Jun-09 2:33
memberRajeevaSingh0111-Jun-09 2:33 
QuestionDoes it support multi level hierarchy?? Pin
ChrisOnNet19-Oct-08 9:47
memberChrisOnNet19-Oct-08 9:47 
AnswerRe: Does it support multi level hierarchy?? Pin
narkotic_ro5-Jan-11 12:49
membernarkotic_ro5-Jan-11 12:49 
Generalchild table data is missing Pin
benito.lopez.d30-Sep-08 21:07
memberbenito.lopez.d30-Sep-08 21:07 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.171114.1 | Last Updated 31 Jul 2008
Article Copyright 2008 by Joao Eduardo Grossi, Ricardo Macedo
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid