从数据库中绑定 DevExpress Treelist 中的数据

Binding data in DevExpress Treelist from database

在我的项目中,我有一个名为 SubControl 的 UserControl。在那个包含 TreeList 和两个 Button 的 UserControl 中。用于聚焦树列表的下一行和上一行的按钮。

在另一个项目中,我有另一个名为 MainControl 的 UserControl 各种控件,这里我使用的是 SubControl。

我的问题是,我无法从 MainControl 将数据从数据库绑定到树列表中。

SqlConnection SqlCon = new SqlConnection("Data Source=source; Initial Catalog=dbname; Integrated Security=True");
        SqlCon.Open();
        SqlCommand ad = new SqlCommand("Select* from mytablename", SqlCon);
        SqlDataAdapter da = new SqlDataAdapter(ad);
        DataSet ds = new DataSet();

        da.Fill(ds);

        SubControl.DataSource = ds; 
        SubControl.DataBindings();

我怎样才能做到这一点。帮助赞赏

您必须绑定到 DataTable,而不是 DataSet。

SubControl.DataSource = ds.Tables[0];

来自 DevExpress:

https://www.devexpress.com/Support/Center/Question/Details/Q520794

In order to create the tree list hierarchical structure, it's necessary to specify two additional fields in the source DataTable. The first field must store the nodes' unique IDs (in most cases, there is already a primary key in the DataTable). The other field must contain the parent node's ID for each node. To specify these fields for the TreeList control, use the TreeList.KeyFieldName and TreeList.ParentFieldName properties. Please refer to the following help article for more information in this regard: Tree Generation Algorithm in the XtraTreeList.