Click here to Skip to main content
Licence Ms-PL
First Posted 31 Jul 2008
Views 82,050
Downloads 3,635
Bookmarked 70 times

DataGridView with hierarchical data binding

The TreeGridView by Mark Rideout with data binding and sorting

Introduction

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!

Background

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>();
displayColumns.Add("id");
displayColumns.Add("Name");
displayColumns.Add("Fruit");
displayColumns.Add("BoughtFrom");
displayColumns.Add("Date");         

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");
connection.Open();

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

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

DataSet dsResults = new DataSet("Results");
dsResults.Tables.Add(dtResult1);
dsResults.Tables.Add(dtResult2);
DataRelation relation1 = new DataRelation(
    "relation1",
    dtResult1.Columns["id"],
    dtResult2.Columns["id"]);
dsResults.Relations.Add(relation1); 

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

DataGridSource newGridSource = new DataGridSource(
    dtResult1.DataSet,
    displayColumns,
    groupColumns);
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.

History

  • July 31, 2008 - Initial release

License

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

Engineer

Brazil Brazil

Member



Ricardo Macedo



Brazil Brazil

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionUnable to cast type Pinmemberduphrx8:41 7 Aug '11  
Answerupdate [modified] Pinmemberjoel_gil4:39 22 Sep '11  
QuestionBindingNavigator PinmemberNetKoder3:15 27 Apr '11  
GeneralJust what i needed PinmemberVincent Meijer4:34 27 Jan '11  
GeneralMy vote of 5 PinmemberVincent Meijer4:32 27 Jan '11  
GeneralNeed help for Drag & Drop Pinmemberbnsrinivas6:05 21 Jan '11  
QuestionAny possibility to format a column as checkbox? Pinmemberandibaden3:39 9 Jan '11  
QuestionHow to Adjust row's height? PinmemberMember 198466923:01 19 Sep '10  
GeneralMy vote of 5 Pinmemberblaknar2:46 9 Aug '10  
QuestionThat just works with Windows XP Style?? Pinmemberbruno_bert3:58 27 Apr '10  
AnswerRe: That just works with Windows XP Style?? PinmemberBassamKhalid19:55 6 Aug '11  
GeneralIt doesn't work PinmemberMetalPower12:13 18 Mar '10  
GeneralRe: It doesn't work PinmemberP0110X4:31 24 Jun '10  
GeneralRe: It doesn't work PinmemberMember 42219845:03 24 Sep '11  
QuestionVb Version? Pinmembercleomc3:28 8 Dec '09  
GeneralMy vote of 1 Pinmemberxpesdal1:21 1 Jul '09  
GeneralWhere did GroupColumn Come From PinmemberLemuel.Adane23:48 2 Jun '09  
GeneralData Source Property PinmemberRizwan Tahir4:09 6 Apr '09  
GeneralRe: Data Source Property PinmemberRajeevaSingh011:29 11 Jun '09  
GeneralRe: Data Source Property Pinmemberashvani pal1:31 11 Jun '09  
GeneralRegarding ur grid Pinmembermorshed_pintu23:56 2 Mar '09  
GeneralRe: Regarding ur grid PinmemberRajeevaSingh011:31 11 Jun '09  
GeneralIncorrect Redraw of Child Nodes after delete PinmemberJerry Dusing21:24 28 Nov '08  
QuestionRe: Incorrect Redraw of Child Nodes after delete Pinmemberabczan12311:38 3 Apr '09  
Generalgreat work! PinmemberMember 310882510:34 26 Nov '08  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    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 | Mobile
Web02 | 2.5.120517.1 | Last Updated 31 Jul 2008
Article Copyright 2008 by Joao Eduardo Grossi, Ricardo Macedo
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid