TreeView for WinForms | ComponentOne
Data Binding / DataSet Binding
In This Topic
    DataSet Binding
    In This Topic

    Just like binding with relational lists, TreeView can be bound to relational datasets, where relationships are defined between individual tables. The TreeView control can automatically generate the KeyField and ParentKeyField properties from the relations in the dataset. This also helps in displaying the tables hierarchically by setting the DataSource and DataMemberPath properties of BindingInfo class.

    The image below depicts dataset binding in the TreeView control.

    TreeView UI

    To bind TreeView to a DataSet, follow these steps:

    1. Set the datasource for TreeView using the DataSource property. Note that here we fetch the data from the DataSet class using the GetData method. You can find the code for DataSet class here.
      C#
      Copy Code
      c1TreeView1.BindingInfo.DataSource = GetData();
      
    2. Set the data members for tree levels.
      C#
      Copy Code
      // set the data member path
      c1TreeView1.BindingInfo.DataMemberPath.Add(0, "Customers");
      c1TreeView1.BindingInfo.DataMemberPath.Add(1, "Orders");
      c1TreeView1.BindingInfo.DataMemberPath.Add(2, "OrderDetail");
      
    3. Configure and add the columns to be displayed in the TreeView. Set the DisplayFieldName property to get the list of fields to be displayed in the columns and the DisplayMemberPath property to get a collection that identifies the fields to be displayed in a column depending on the node level.
      C#
      Copy Code
      // configure columns
      // ID
      var column = c1TreeView1.Columns[0];
      column.DisplayFieldName = "ID";
      column.HeaderText = "ID";
      // Customer info
      column = new C1.Win.TreeView.C1TreeColumn();
      column.DisplayMemberPath.Add(0, "Name");
      column.DisplayMemberPath.Add(1, "CustomerID");
      column.DisplayMemberPath.Add(2, "OrderID");
      column.HeaderText = "Customer info";
      c1TreeView1.Columns.Add(column);
      // Order Info
      column = new C1.Win.TreeView.C1TreeColumn();
      column.DisplayMemberPath.Add(0, "Address");
      column.DisplayMemberPath.Add(1, "Date");
      column.DisplayMemberPath.Add(2, "Qty");
      column.HeaderText = "Order Info";
      c1TreeView1.Columns.Add(column);