// Mechanism to build a custom BoundTreeNode, supplies the data, currency and position... private BoundTreeNode CreateNode(DataRowView drv, CurrencyManager cm, int position) { // TableBinding object allows us to customize the DisplayMember and ValueMember for each binding TableBinding tableBinding = GetBinding(drv.Row.Table.TableName); BoundTreeNode node = null; if (tableBinding != null) { node = new BoundTreeNode(drv[tableBinding.DisplayMember].ToString(), drv[tableBinding.ValueMember], cm, position, tableBinding.ImageIndex, tableBinding.SelectedImageIndex); } else { // when no binding is supplied, default to the first datarow column... node = new BoundTreeNode(drv[0].ToString(), drv[0], cm, position, -1, -1); } return(node); }
// When the BoundTreeView's nodes are selected, we must synchronize the CurrencyManagers... private void tv_AfterSelect(object sender, TreeViewEventArgs e) { // We have to move the currency manager positions for every node in the // selected heirarchy because the parent node selection determines the // currency manager "list" contents for the children ArrayList nodeList = new ArrayList(); // Start with the node that has been selected BoundTreeNode node = (BoundTreeNode)((TreeView)sender).SelectedNode; nodeList.Add(node); // Recursively add all the parent nodes node = (BoundTreeNode)node.Parent; while (node != null) { nodeList.Add(node); node = (BoundTreeNode)node.Parent; } // Don't fire the our own position change event other controls bound to the // currency managers will move accordingly because we are setting the position // explicitly DisablePositionChanged = true; // Start at the highest parent node for (int i = nodeList.Count; i > 0; i--) { node = (BoundTreeNode)nodeList[i - 1]; ((IBindingList)node.CurrencyManager.List).ListChanged -= handlerListChanged; node.CurrencyManager.Position = node.Position; ((IBindingList)node.CurrencyManager.List).ListChanged += handlerListChanged; } DisablePositionChanged = false; }