C# – Treeview databinding with 3 layers model programming

As I reviewed database programming techniques with C#, I discovered that I have never used TreeView to display my data so I decide to make a small practice with loading data into TreeView and show sub-information on the other component when clicking on its node. The TreeView has no DataSource and DataBinding property as ComboBox and ListBox does. Therefore we must load data ourselves manually, node by node. I know this is very simple example. I believe that all of programmers can do it. So do not take this example as working with Treeview but take it as a review about “3 layers model prgramming”.

The picture below figures out the main idea of “3 layers model programming”.

Our application will consist of 3 layers: Data Access Layer, Business Logic Layer and Presentation Layer (User Interface).

  • A Data Access Layer (DAL) provides simplified access to data stored in persistent storage of some kind, such as an entity-relational database. For example, the DAL might return a reference to an object (in terms of object-oriented programming) complete with its attributes instead of a row of fields from a database table. This allows the client (or user) modules to be created with a higher level of abstraction.  The DAL hides the complexity of the underlying data store from the external world. In the code snippet below, DAL return the list of employees and completely hide the structure of database. The external world will only receive what this layer gives although in the database there are still a lot of information of each employee not only his name, his id and his function.
class DatabaseAccessLayer
 {
 public List<Employees> GetEmployees()
 {
 List<Employees> lstEmployees = new List<Employees>();
 dbConnection dbConn = new dbConnection();
 DbDataReader reader = dbConn.ExecuteSQLCommand("SELECT * FROM Personal");
 while (reader.Read())
 {
 lstEmployees.Add(new Employees(reader["Nachname"] + "", reader["Vorname"] + "", reader["PersonalNr"] + "", reader["Funktion"] + ""));
 }
 reader.Close();
 return lstEmployees;
 }
 }
  • A Business logic layer (BLL) is a software engineering practice of compartmentalizing the rules and calculations of a software application from its other design elements. For example, in an application with a properly separated Business logic layer and Data access layer, the Data access layer could be rewritten to retrieve data from a different database, without affecting any of the business logic. This practice allows software application development to be more effectively split into teams, with each team working on a different tier simultaneously. Let’s see the source code below of BLL. BLL has no idea what DAL does with database. He just takes the result of DAL and does his work. Completely blind with each other.
class BusinessLogicLayer
 {
 DatabaseAccessLayer dalLayer = new DatabaseAccessLayer();
 List<Employees> m_lstEmployees = new List<Employees>();

 public BusinessLogicLayer()
 {
 m_lstEmployees = dalLayer.GetEmployees();
 }
 public Employees GetUserByLastName(string LastName)
 {

 for (int nIndex = 0; nIndex < m_lstEmployees.Count; nIndex++)
 {
 if (m_lstEmployees[nIndex].LastName == LastName)
 {
 return m_lstEmployees[nIndex];
 }
 }
 return null;
 }

 public List<Employees> GetAllEmployees()
 {
 return m_lstEmployees;
 }
 }
  • Presentation Layer (User Interface) takes over the mission to show what user should see.
private void btnLoad_Click(object sender, EventArgs e)
 {

 for (int nIndex = 0; nIndex < m_bllLayer.GetAllEmployees().Count; nIndex++)
 {
 trvEmployee.Nodes.Add(m_bllLayer.GetAllEmployees()[nIndex].LastName);
 }
 }

 private void trvEmployee_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
 {
 Employees empTemp = m_bllLayer.GetUserByLastName(e.Node.Text);
 lblEmployeeID.Text = empTemp.EmployeeID;
 lblFirstname.Text = empTemp.FirstName;
 lblFunction.Text = empTemp.Function;
 }

At the end we have a small example with TreeView.

When clicking Load-Button, the TreeView loads data from table “Personal” from database Nordwind.mdb and show last name of each employee on each node. Clicking on node the sub-info of an employee will be shown on right labels. You can download the source code of “TreeView databinding with 3 layers model programming” as reference.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.