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