As part on the functionality I am trying to include in my web application, is the ability to “search” the TreeList. I have a field selector drop down list and a text box or check box to enter the search criteria. The search button performs a post back where I then location a match in the data source. Now that I have the matching record, how can I force it to be displayed selected? The result of the search may be a child node somewhere down the hierarchy not currently being displayed. If this is the case I would want the branch with the matching criteria to be expanded to this item. Then I would want to select the item. Does anyone have any ideas on how to force a node to be displayed?
I have not looked into using ExpandItemToLevel since it is liable to expose too many items. I don’t think it would work for one level at a time since I might be calling it for items not displayed, recursive calls.
4 Answers, 1 is accepted
RadGrid does not currently support expanding to a particular item specified by a key value or any other data. Only expanding to level is supported, which expands all items up to a particular level. RadTreeList does not keep reference to TreeListDataItems that are not expanded. Thus, the items you see in the treelist (expanded items and their children) are the only items RadTreeList maintains. You cannot find an item that is not expanded, because it doesn't exist.
However, you can implement a solution for this scenario. If you expand all items first, you can find an item by its data key value. This is made possible through the ExpandAllItems and CollapseAllItems methods in RadTreeList. Using these methods, we can expand all items, find the relevant item and collapse back the treelist, keeping the previously expanded indexes, as well as any parent item of the found item. Here is a sample method that searches for and selectes a data item by specified ID key value:
private
void
FindAndSelectItem(
int
id)
{
//first check if the item is not already in the Items collection
foreach
(var item
in
RadTreeList1.Items)
{
if
((
int
)item.GetDataKeyValue(
"ID"
) == id)
{
item.Selected =
true
;
return
;
}
}
//save the previously expanded indexes
var expandedIndexes =
new
TreeListHierarchyIndex[RadTreeList1.ExpandedIndexes.Count];
RadTreeList1.ExpandedIndexes.CopyTo(expandedIndexes);
//add newly expanded indexes here
var newIndexes =
new
List<TreeListHierarchyIndex>();
//cause all items to expand to reveal all data
RadTreeList1.ExpandAllItems();
//loop through all the items and search for the target by its key value
foreach
(var item
in
RadTreeList1.Items)
{
if
((
int
)item.GetDataKeyValue(
"ID"
) == id)
{
//select the item
item.Selected =
true
;
//expand all parents of the item
var parent = item.ParentItem;
while
(parent !=
null
)
{
newIndexes.Add(parent.HierarchyIndex);
parent = parent.ParentItem;
}
}
}
//shrink back the treelist
RadTreeList1.CollapseAllItems();
//restore the previously expanded indexes
RadTreeList1.ExpandedIndexes.AddRange(expandedIndexes);
//add the newly expanded indexes that will reveal the selected item
foreach
(var index
in
newIndexes)
{
if
(!RadTreeList1.ExpandedIndexes.Contains(index))
{
RadTreeList1.ExpandedIndexes.Add(index);
}
}
//rebind to reflect the changes
RadTreeList1.Rebind();
}
Attaching a test page to demonstrate this scenario.
Veli
the Telerik team


I am evaluating telerik controls.
I have some questions to this topic:
- How can I search for the display text?
I dit it on this way, is it the right one or is there a better solution?
//first check if the item is not already in the Items collection
foreach (var item in RadTreeList1.Items)
{
foreach (TableCell cel in item.Cells)
{
if (cel.Text == itemtosearch)
{
item.Selected = true;
return;
}
}
} - If the RadTreeList has multipages, it does not select the page where the itemtosearch where found. How can I select the right page?
- If I get multiple results, I want to move (select) the next result. How can I search for next result?
Thank You in Advance
Jo A. Kim
TsvetoslavHello Jo A. Kim,
Straight to your questions.
1. Your approach is correct and viable.
2. When the tree list has multiple pages, you should:
2.1. set AllowPaging = false
2.2. call the tree list's Rebind() method
2.3. search for the item.
2.4. determine what page it lies on given the total items count and the page size.
2.5. set the tree list's CurrentPageIndex to the page number determined in step 4.
2.6. once again rebind the tree list.
3. Find all items containing your search criteria, determine all the information you need to select the item (indexes to expand, page index, data key value), persist them in a custom object of yours and serialize it in ViewState or SessionState. Then on the next search, just pull it from there, expand the items by the expand indexes persisted and select the item containing the text.
Hope it helps.
Regards,
the Telerik team