Back to Devexpress

TreeList.DataSource Property

windowsforms-devexpress-dot-xtratreelist-dot-treelist-60b995e5.md

latest8.0 KB
Original Source

TreeList.DataSource Property

Gets or sets the data source for the current TreeList control.

Namespace : DevExpress.XtraTreeList

Assembly : DevExpress.XtraTreeList.v25.2.dll

NuGet Packages : DevExpress.Win.Navigation, DevExpress.Win.TreeList

Declaration

csharp
public object DataSource { get; set; }
vb
Public Property DataSource As Object

Property Value

TypeDescription
Object

The data source object.

|

Remarks

You can bind all traditional data sources to the Tree List control: BindingSource, BindingList<T>, DataTable, DataView, IBindingList, IList, etc. In bound mode, the Tree List control creates nodes for all records in the data source at one time, when it loads data. It does not load data dynamically in this mode.

Because of the data source’s tree-like structure, the Tree List control implements a specific sorting algorithm. Therefore you cannot use the data source’s functionality in order to sort data. Use the XtraTreeList’s methods instead (for example, the TreeListColumn.SortOrder property).

The code sample below illustrates how to populate a tree list with random values.

csharp
treeList1.DataSource = CreateTLData(100);

DataTable CreateTLData(int recordCount)
{
    Random rnd = new Random();
    DataTable tbl = new DataTable();
    tbl.Columns.Add("ID", typeof(int));
    tbl.Columns.Add("ParentID", typeof(int));
    tbl.Columns.Add("Name", typeof(string));
    tbl.Columns.Add("Date", typeof(DateTime));
    tbl.Columns.Add("Checked", typeof(bool));
    for (int i = 0; i < recordCount; i++)
        tbl.Rows.Add(new object[] { i, rnd.Next(20), String.Format("Name{0}", i), DateTime.Now.Date.AddDays(rnd.Next(-250, 250)), rnd.Next(2) == 0 });
    return tbl;
}
vb
treeList1.DataSource = CreateTLData(100)

DataTable CreateTLData(Integer recordCount)
    Dim rnd As New Random()
    Dim tbl As New DataTable()
    tbl.Columns.Add("ID", GetType(Integer))
    tbl.Columns.Add("ParentID", GetType(Integer))
    tbl.Columns.Add("Name", GetType(String))
    tbl.Columns.Add("Date", GetType(Date))
    tbl.Columns.Add("Checked", GetType(Boolean))
    For i As Integer = 0 To recordCount - 1
        tbl.Rows.Add(New Object() { i, rnd.Next(20), String.Format("Name{0}", i), Date.Now.Date.AddDays(rnd.Next(-250, 250)), rnd.Next(2) = 0 })
    Next i
    Return tbl

The following code snippets (auto-collected from DevExpress Examples) contain references to the DataSource property.

Note

The algorithm used to collect these code examples remains a work in progress. Accordingly, the links and snippets below may produce inaccurate results. If you encounter an issue with code examples below, please use the feedback form on this page to report the issue.

winforms-dashboard-custom-items-extension/CS/CustomItemExtension/CustomItems/TreeList/TreeListItemControlProvider.cs#L78

csharp
skipMasterFiltering = true;
tree.DataSource = null;
tree.ParentFieldName = tree.KeyFieldName = string.Empty;

xaf-win-gantt-control/CS/XPO/GanttSolutionXPO/GanttSolutionXPO.Win/Editors/CustomGanttEditor.cs#L40

csharp
control.BeginUpdate();
control.DataSource = controlDataSource;
control.RefreshDataSource();

winforms-treelist-virtual-mode/CS/ExampleMainForm.cs#L31

csharp
MyTtreeList.SelectImageList = imageListForTree;
    MyTtreeList.DataSource = new object();
}

winforms-tokenedit-dropdown-with-treelist/CS/TokenEditTest/CustomTokenEditDropDownControl.cs#L23

csharp
public override void SetDataSource(object dataSource) {
    TreeList.DataSource = new TreeListDataSet((IEnumerable<TokenEditToken>)dataSource);
    TreeList.ExpandAll();

create-custom-editor-with-auto-height-functionality/CS/TreeListAutoNodeHeight/Form1.cs#L14

csharp
InitializeComponent();
treeList1.DataSource = CreateDataset_Table().Tables["Customers"];
CustomAnyControlEditRepositoryItem CustomRepository = new CustomAnyControlEditRepositoryItem();

winforms-dashboard-custom-items-extension/VB/CustomItemExtension/CustomItems/TreeList/TreeListItemControlProvider.vb#L85

vb
skipMasterFiltering = True
tree.DataSource = Nothing
tree.KeyFieldName = String.Empty

winforms-treelist-virtual-mode/VB/ExampleMainForm.vb#L23

vb
MyTtreeList.SelectImageList = imageListForTree
    MyTtreeList.DataSource = New Object()
End Sub

winforms-tokenedit-dropdown-with-treelist/VB/TokenEditTest/CustomTokenEditDropDownControl.vb#L25

vb
Public Overrides Sub SetDataSource(ByVal dataSource As Object)
    TreeList.DataSource = New TreeListDataSet(DirectCast(dataSource, IEnumerable(Of TokenEditToken)))
    TreeList.ExpandAll()

winforms-treelist-unbound-columns/VB/TreeList_UnboundDataViaEvent/Form1.vb#L19

vb
Private Sub InitTreeList()
    treeList1.DataSource = SalesDataGenerator.CreateData()
    ' Create and customize an unbound column.

reporting-winforms-create-hierarchical-report-from-flat-table/VB/TreeViewReport/TreeReport.vb#L49

vb
subReport.ReportSource = childReport
parentForm.treeList1.DataSource = helperField.ReportDataSource
parentForm.treeList1.ExpandAll()

See Also

Sorting

Data Binding

TreeList Class

TreeList Members

DevExpress.XtraTreeList Namespace