|
|||||||||||||||||||||
|
|||||||||||||||||||||
|
Announcements
Want a new Job?
Chapters
Services
Feature Zones
|
IntroductionThis is a C# implementation of data binding BackgroundWhen I was searching for such a data binding The ControlAlthough I am not the original author of the control, I would like to introduce it to all C# developers as I found it is a very useful and easy to use tree view control. The
Using the codeIn the demo, data was retrieved from four Microsoft Access tables:
After the data is retrieved into a this.myTree.DisplayMember = “title”;
this.myTree.ValueMember = “title_id”;
this.myTree.DataSource = this.myDataView;
So far, data in the
In order to display data in hierarchy, the data needs to be sorted and grouped. From the data tables above, we can see that the data can be grouped by Publisher, Author, Title, or the combinations of them. To display the hierarchical data as shown in the beginning, the following code needs to be added for sorting and grouping: this.myDataView.Sort = “pub_id, au_id, title_id”;
this.myTree.AddGroup ("Publisher", "pub_id", "pub_name", "pub_id", 0, 0);
this.myTree.AddGroup ("Author", "au_id", "au_name", "au_id", 1, 3);
It first groups the data by Publisher, and then by Author. Both the publisher and author nodes in the tree are group nodes, while the book title nodes are leaf nodes. Each of the nodes carry a In the demo project, I added a menu to show how to change the groups. Groups can be added by checking the menu items, and removed by unchecking them. The following example groups data by Author and Title with publishers as leaves.
Although you do not need to add a group for the leaves, you need to let the tree know which data group should be used as the leaves. For example, if the data is grouped by Publisher only, what will be the leaf nodes, the authors or the book titles? For this reason, I added a method in the This control contains useful functions to let you retrieve data from the nodes easily. Its Changes made to the original codeBesides what have been mentioned early, the major change was combining two derived public String GroupName; // the name of the group
public object Value; // the value member to identify the node
public object Item; // the data row in the data source
public int Position; // the row index in the data source
The result of this change is the elimination of the following classes, methods, and events in the control and in the code using the control: // In the control:
Classes: TreeGroupNode
TreeLeavNode
GroupTreeViewEventArgs
GroupTreeViewCancelEventArgs
LeafTreeViewEventArgs
LeafTreeViewCancelEventArgs
Methods: FindFirstLeafNode()
OnBeforeSelect()
OnAfterSelect()
currcyManager_PositionChanged()
Events: public delegate void BeforeGroupSelHandler()
public event BeforeGroupSelHandler BeforeGroupSelect;
public delegate void AfterGroupSelHandler()
public event AfterGroupSelHandler AfterGroupSelect;
public delegate void BeforeLeafSelHandler()
public event BeforeLeafSelHandler BeforeLeafSelect;
public delegate void AfterLeafSelHandler()
public event AfterLeafSelHandler AfterLeafSelect;
Event Handlers:
private void currcyManager_PositionChanged()
// In the code using the control:
Event Handlers:
private void myTree_BeforeGroupSelect()
private void myTree_AfterGroupSelect()
private void myTree_BeforeLeafSelect()
private void myTree_AfterLeafSelect()
The other change was adding the ability to disable the control’s auto-builder feature. In the original control, the tree was built or rebuilt whenever assigning a data source, adding a group, or removing a group. To display a three-level hierarchical tree as shown at the beginning, the tree was built 3 times. Only the last time, it generates the desired result. For large amounts of data, this would slow down the process, so I added an this.bookDataView.Sort = "pub_id, au_name, title_id";
this.myTree.DataSource = this.bookDataView;
this.myTree.AddGroup ("Publisher", "pub_id","pub_name", "pub_id", 0,0);
this.myTree.AddGroup ("Author", "au_id", "au_name", "au_id", 1, 3);
this.myTree.SetLeafData ("Title", "title", "title_id", 2, 2);
this.myTree.BuildTree();
ConclusionI hope this is a useful tree-view control to you. Credits should be given to the original author Duncan Mackenzie of MSDN. As he said in the original article: “The data-bound Tree control shown in this article will not be the answer for every project that needs a Tree control to display information from a database, but it does illustrate one way in which you could customize this control for your own purposes. Keep in mind that the majority of the code in this control would be essentially the same in any complex data-bound control you wish to build, and that you can leverage that existing work to make future control development easier.” Once again, the original code and article can be found at the link here. When you read the original article, please note that some sections may not apply to this C# implementation as many code have been eliminated. I wish that my modifications have simplified the original code without losing its functionalities, and that it will benefit C# users. | ||||||||||||||||||||