Bind KetticTreeView to Database in C#.NET
KetticTreeView WinForms control provides powerful data binding capability and developers can just configure a simple property of the control to bind the TreeView to any data sources or objects. This control allows the developers to easily bind it to database data and show data in tree view. The KetticTreeView contains the RelationBindings collection to create tree levels when we process the data binding. The following C# tutorial demonstrates the database binding of KetticTreeView.
RelationBindings Collection of KetticTreeView in C#.NET
KetticTreeView provides the RelationBindings collection for the UI designers to bind itself to database data. The collection is capable of creating tree levels as data binding and its object includes the parent data and child data that will be bound to, as well as the data columns populating the Text and Value properties of TreeView nodes.
Parameters of RelationBinding Collection for KetticTreeView
The constructor of the RelationBinding collection contains a few overrides and we should at least pass the RelationName and DataSource to the Add() method.
- dataSource, this parameter determines the object of the data source that will be bound to.
- dataMember, this parameter is used to set the name of a specific record that will be shown in tree view. This is done through the DataSource property.
- displayMember, this parameter determines the name of the field populating the Text property of KetticTreeView nodes.
- valueMember, this parameter determines the name of the field populating the Value property of KetticTreeView nodes.
- parentMember, this parameter is used to set the name of the parent data field that will be bound to.
- childMember, this parameter is used to set the name of the child data field will be bound to.
Guide for Binding KetticTreeView to Database in Windows Forms
- Open an existing .NET project or create a new one in your Visual Studio
- Drag a KetticTreeView control from the toolbox and drop it on the form
- Open the Properties Window of the control, click the DataSource property drop down, choose the Add Project Data Source
- Click the Database button in the Choose a Data Source Type page, and go to Next step
- Select the New Connection in the page of Choose Your Data Connection wizard
- Click Change button in the Add Connection dialog, choose Microsoft Access Database File, and then hit OK button
- Click Browse button in the Database file name entry, and locate the provided StudentListSample.mdb file
- In the Choose Your Database Objects page, choose the Name, StudentID, and Class tables, and then click Finish button to add these data tables
- And then we can set the DataSource property to Class, StudentID, and Name datatable.
- Finally, we can add the following C# code to the load event in the Windows Forms
this.ketticTreeView1.DataSource = this.artistsBindingSource;
this.ketticTreeView1.DisplayMember = "StudentName";
this.ketticTreeView1.ValueMember = "StudentID";
this.ketticTreeView1.RelationBindings.Add(new RelationBinding(this.albumsBindingSource, "StudentName", "StudentID", "StudentClass"));
this.ketticTreeView1.RelationBindings.Add(new RelationBinding(this.songsBindingSource, "StudentName", "StudentID", "StudentClassâ));