winformsdevexpressxtratreelist

TreeListLookupEdit - Focus Node


I'm trying to select a node in TreeListLookupEdit.

var fn = treeListLookupEdit1.FindNodeByKeyID(NodeId);
treeListLookupEdit1.Properties.TreeList.FocusedNode = fn;

My TreeListLookupEdit is already filled with the data (from an EF datasource), I need to focus the desired row and see this value in both treeListLookUpEdit1.Text (when it is in a closed state) and when I open a popup window too.

But nothing happens, it does not selects the node.

I've also tried this (Where "treeNodes" is the actual TreeList inside the TreeListLookupEdit):

treeNodes.FocusedNode = fn;

But, when I run this piece of code, it works:

treeListLookupEdit1.ShowPopup();
treeListLookupEdit1.Properties.TreeList.FocusedNode = fn;
treeListLookupEdit1.ClosePopup();

So, how to avoid using the ShowPopup?

Update It seems, you should set EditValue

treeListLookupEdit1.EditValue = NodeId

Solution

  • You need to set up TreeListLookUpEdit.Properties.DisplayMember property and TreeListLookUpEdit.Properties.ValueMember property.
    Set the TreeListLookUpEdit.Properties.DisplayMember property to the column that you want to display in your TreeListLookupEdit and TreeListLookUpEdit.Properties.ValueMember to ID column and use TreeListLookUpEdit.EditValue to focus node.
    After that you can do something like this:

    treeListLookupEdit1.EditValue = fn.GetValue("YourIDColumn");
    

    Here is example with DataTable as data source:

    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();
    

    If you set EditValue property of this treeListLookUpEdit object for example to 5 then you will see "1.2" text in control and node with such text will be focused:

    treeListLookUpEdit.EditValue = 5;