private void FillGrid() { AutoNotes.RefreshCache(); List <AutoNote> listAutoNotes = AutoNotes.GetDeepCopy(); gridMain.BeginUpdate(); gridMain.ListGridColumns.Clear(); gridMain.ListGridColumns.Add(new GridColumn("", 100)); gridMain.ListGridRows.Clear(); GridRow row; foreach (AutoNote autoNote in listAutoNotes) { row = new GridRow(); row.Cells.Add(autoNote.AutoNoteName); row.Tag = autoNote; gridMain.ListGridRows.Add(row); } gridMain.EndUpdate(); }
public static void FillListTree(TreeView treeNotes, UserOdPref _userOdCurPref) { List <long> listExpandedDefNums = new List <long>(); if (treeNotes.Nodes.Count == 0 && _userOdCurPref != null) //if this is the fill on load, the node count will be 0, expanded node list from pref { listExpandedDefNums = _userOdCurPref.ValueString.Split(',').Where(x => x != "" && x != "0").Select(x => PIn.Long(x)).ToList(); } else //either not fill on load or no user pref, store the expanded node state to restore after filling tree //only defs (category folders) can be expanded or have children nodes { listExpandedDefNums = treeNotes.Nodes.OfType <TreeNode>().SelectMany(x => GetNodeAndChildren(x)) .Where(x => x.IsExpanded && x.Tag is Def).Select(x => ((Def)x.Tag).DefNum).ToList(); } TreeNode selectedNode = treeNotes.SelectedNode; TreeNode topNode = null; string topNodePath = treeNotes.TopNode?.FullPath; treeNotes.BeginUpdate(); treeNotes.Nodes.Clear(); //clear current tree contents _dictChildNodesForDefNum = Defs.GetDefsForCategory(DefCat.AutoNoteCats, true).GroupBy(x => x.ItemValue ?? "0") .ToDictionary(x => PIn.Long(x.Key), x => new NodeChildren() { ListChildDefNodes = x.Select(y => new TreeNode(y.ItemName, 0, 0) { Tag = y }).ToList() }); Dictionary <long, List <TreeNode> > dictDefNumAutoNotes = AutoNotes.GetDeepCopy().GroupBy(x => x.Category) .ToDictionary(x => x.Key, x => x.Select(y => new TreeNode(y.AutoNoteName, 1, 1) { Tag = y }).ToList()); foreach (KeyValuePair <long, List <TreeNode> > kvp in dictDefNumAutoNotes) { if (_dictChildNodesForDefNum.ContainsKey(kvp.Key)) { _dictChildNodesForDefNum[kvp.Key].ListAutoNoteNodes = kvp.Value; } else { _dictChildNodesForDefNum[kvp.Key] = new NodeChildren() { ListAutoNoteNodes = kvp.Value }; } } List <TreeNode> listNodes = new List <TreeNode>(); //all nodes to add to tree, categories and autonotes NodeChildren nodeChildren; if (_dictChildNodesForDefNum.TryGetValue(0, out nodeChildren)) { nodeChildren.ListChildDefNodes.ForEach(SetAllDescendantsForNode); listNodes.AddRange(nodeChildren.ListChildDefNodes); listNodes.AddRange(nodeChildren.ListAutoNoteNodes); } treeNotes.Nodes.AddRange(listNodes.OrderBy(x => x, new NodeSorter()).ToArray()); //add node list to tree, after sorting List <TreeNode> listNodesCur = listNodes.SelectMany(x => GetNodeAndChildren(x)).ToList(); //get flat list of all nodes, copy entire tree foreach (TreeNode nodeCur in listNodesCur) { if (!string.IsNullOrEmpty(topNodePath) && nodeCur.FullPath == topNodePath) { topNode = nodeCur; } if (nodeCur.Tag is Def && listExpandedDefNums.Contains(((Def)nodeCur.Tag).DefNum)) { nodeCur.Expand(); } if (selectedNode == null) { continue; } if (Equals(nodeCur, selectedNode)) { treeNotes.SelectedNode = nodeCur; } } treeNotes.TopNode = topNode; if (topNode == null && treeNotes.Nodes.Count > 0) { treeNotes.TopNode = treeNotes.SelectedNode ?? treeNotes.Nodes[0]; } treeNotes.EndUpdate(); treeNotes.Focus(); }