您需要设置TreeListLookUpEdit.Properties.DisplayMember
属性和TreeListLookUpEdit.Properties.ValueMember
属性。
将TreeListLookUpEdit.Properties.DisplayMember
属性设置为要在您的TreeListLookupEdit
和TreeListLookUpEdit.Properties.ValueMember
ID 列中显示的列并用于TreeListLookUpEdit.EditValue
聚焦节点。
之后,您可以执行以下操作:
treeListLookupEdit1.EditValue = fn.GetValue("YourIDColumn");
以下是DataTable
作为数据源的示例:
var dataTable = new DataTable();
dataTable.Columns.Add("ID", typeof(int));
dataTable.Columns.Add("Parent_ID", typeof(int));
dataTable.Columns.Add("Name", typeof(string));
dataTable.Rows.Add(1, null, "1");
dataTable.Rows.Add(2, null, "2");
dataTable.Rows.Add(3, null, "3");
dataTable.Rows.Add(4, 1, "1.1");
dataTable.Rows.Add(5, 1, "1.2");
dataTable.Rows.Add(6, 3, "3.1");
dataTable.Rows.Add(7, 3, "3.2");
dataTable.Rows.Add(8, 5, "1.2.1");
var treeListLookUpEdit = new TreeListLookUpEdit();
var properties = treeListLookUpEdit.Properties;
properties.DataSource = dataTable;
properties.DisplayMember = "Name";
properties.ValueMember = "ID";
var treeList = properties.TreeList;
treeList.KeyFieldName = "ID";
treeList.ParentFieldName = "Parent_ID";
treeList.RootValue = DBNull.Value;
Controls.Add(treeListLookUpEdit);
treeListLookUpEdit.Size = treeListLookUpEdit.CalcBestSize();
例如,如果EditValue
将此treeListLookUpEdit
对象的属性设置为 5,那么您将在控件中看到“1.2”文本,并且具有此类文本的节点将被聚焦:
treeListLookUpEdit.EditValue = 5;