public TreeGridView() { // Control when edit occurs because edit mode shouldn't start when expanding/collapsing this.EditMode = DataGridViewEditMode.EditProgrammatically; this.RowTemplate = new TreeGridNode() as DataGridViewRow; // This sample does not support adding or deleting rows by the user. this.AllowUserToAddRows = false; this.AllowUserToDeleteRows = false; this._root = new TreeGridNode(this); this._root.IsRoot = true; // Ensures that all rows are added unshared by listening to the CollectionChanged event. base.Rows.CollectionChanged += delegate(object sender, System.ComponentModel.CollectionChangeEventArgs e){}; }
private void UpdateChildNodes(TreeGridNode node) { if (node.HasChildren) { foreach (TreeGridNode childNode in node.Nodes) { childNode._grid = node._grid; this.UpdateChildNodes(childNode); } } }
protected internal virtual bool RemoveChildNode(TreeGridNode node) { if ((this.IsRoot || this._isSited) && this.IsExpanded ) { //We only unsite out child node if we are sited and expanded. this._grid.UnSiteNode(node); } node._grid = null; node._parent = null; return true; }
protected internal virtual bool InsertChildNode(int index, TreeGridNode node) { node._parent = this; node._grid = this._grid; // ensure that all children of this node has their grid set if (this._grid != null) UpdateChildNodes(node); //TODO: do we need to use index parameter? if ((this._isSited || this.IsRoot) && this.IsExpanded) this._grid.SiteNode(node); return true; }
protected internal virtual bool AddChildNode(TreeGridNode node) { node._parent = this; node._grid = this._grid; // ensure that all children of this node has their grid set if (this._grid != null) UpdateChildNodes(node); if ((this._isSited || this.IsRoot) && this.IsExpanded && !node._isSited) this._grid.SiteNode(node); return true; }
protected virtual void RefreshView() { this.dataGridView1.Rows.Clear(); this.dataGridView1.ReadOnly = this.ReadOnly; if (this.managementObject == null) return; // Create group headers styling DataGridViewCellStyle groupCellStyle = new DataGridViewCellStyle(); groupCellStyle.BackColor = groupCellStyle.SelectionBackColor = SystemColors.ControlLight; groupCellStyle.ForeColor = groupCellStyle.SelectionForeColor = SystemColors.ControlDark; Font f = this.dataGridView1.DefaultCellStyle.Font; groupCellStyle.Font = new Font(f.FontFamily, f.Size, FontStyle.Bold); // Create group header nodes if (this.ShowProperties) { this.PropertiesGroup = this.dataGridView1.Nodes.Add(this.PropertiesHeaderText); this.PropertiesGroup.DefaultCellStyle = groupCellStyle; this.PropertiesGroup.Tag = this.ManagementObject; foreach (PropertyData p in this.ManagementObject.Properties) { this.AddNode(this.ManagementObject, p, this.PropertiesGroup.Nodes); } if (this.AutoExpandProperties) this.dataGridView1.ExpandNode(this.PropertiesGroup); } if (this.ShowSystemProperties) { this.SystemPropertiesGroup = this.dataGridView1.Nodes.Add(this.SystemPropertiesHeaderText); this.SystemPropertiesGroup.DefaultCellStyle = groupCellStyle; this.PropertiesGroup.Tag = this.ManagementObject; foreach (PropertyData p in this.ManagementObject.SystemProperties) { this.AddNode(this.ManagementObject, p, this.SystemPropertiesGroup.Nodes); } if (this.AutoExpandSystemProperties) this.dataGridView1.ExpandNode(this.SystemPropertiesGroup); } if (this.ShowQualifiers) { this.QualifiersGroup = this.dataGridView1.Nodes.Add(this.QualifiersHeaderText); this.QualifiersGroup.DefaultCellStyle = groupCellStyle; this.PropertiesGroup.Tag = this.ManagementObject; foreach (QualifierData q in this.ManagementObject.Qualifiers) { this.QualifiersGroup.Nodes.Add(q.Name, q.Value.ToString()); } if (this.AutoExpandQualifiers) this.dataGridView1.ExpandNode(this.QualifiersGroup); } }
protected internal virtual void UnSiteNode(TreeGridNode node) { if (node.IsSited || node.IsRoot) { // remove child rows first foreach (TreeGridNode childNode in node.Nodes) { this.UnSiteNode(childNode); } // now remove this row except for the root if (!node.IsRoot) { try { base.Rows.Remove(node); // Row isn't sited in the grid anymore after remove. Note that we cannot // Use the RowRemoved event since we cannot map from the row index to // the index of the expandable row/node. node.UnSited(); } catch (ArgumentException) { // Odd ArgumentException occurs when closing a form with the datagridview contained in it. } } } }
protected internal virtual void SiteNode(TreeGridNode node, int index) { if (index < base.Rows.Count) { base.Rows.Insert(index, node); } else { // for the last item. base.Rows.Add(node); } }
protected internal virtual void SiteNode(TreeGridNode node) { //TODO: Raise e if parent node is not the root or is not sited. int rowIndex = -1; TreeGridNode currentRow; node._grid = this; if (node.Parent != null && node.Parent.IsRoot == false) { // row is a child Debug.Assert(node.Parent != null && node.Parent.IsExpanded == true); if (node.Index > 0) { currentRow = node.Parent.Nodes[node.Index - 1]; } else { currentRow = node.Parent; } } else { // row is being added to the root if (node.Index > 0) { currentRow = node.Parent.Nodes[node.Index - 1]; } else { currentRow = null; } } if (currentRow != null) { while (currentRow.Level >= node.Level) { if (currentRow.RowIndex < base.Rows.Count - 1) { currentRow = base.Rows[currentRow.RowIndex + 1] as TreeGridNode; Debug.Assert(currentRow != null); } else // no more rows, site this node at the end. break; } if (currentRow == node.Parent) rowIndex = currentRow.RowIndex + 1; else if (currentRow.Level < node.Level) rowIndex = currentRow.RowIndex; else rowIndex = currentRow.RowIndex + 1; } else rowIndex = 0; Debug.Assert(rowIndex != -1); this.SiteNode(node, rowIndex); Debug.Assert(node.IsSited); if (node.IsExpanded) { // add all child rows to display foreach (TreeGridNode childNode in node.Nodes) { //TODO: could use the more efficient SiteRow with index. this.SiteNode(childNode); } } }
protected internal virtual bool ExpandNode(TreeGridNode node) { if (!node.IsExpanded || this._virtualNodes) { ExpandingEventArgs exp = new ExpandingEventArgs(node); this.OnNodeExpanding(exp); if (!exp.Cancel) { this.LockVerticalScrollBarUpdate(true); this.SuspendLayout(); _inExpandCollapse = true; node.IsExpanded = true; //TODO Convert this to a InsertRange foreach (TreeGridNode childNode in node.Nodes) { Debug.Assert(childNode.RowIndex == -1, "Row is already in the grid."); this.SiteNode(childNode); //this.BaseRows.Insert(rowIndex + 1, childRow); //TODO : remove -- just a test. //childNode.Cells[0].Value = "child"; } ExpandedEventArgs exped = new ExpandedEventArgs(node); this.OnNodeExpanded(exped); //TODO: Convert this to a specific NodeCell property _inExpandCollapse = false; this.LockVerticalScrollBarUpdate(false); this.ResumeLayout(true); this.InvalidateCell(node.Cells[0]); } return !exp.Cancel; } else { // row is already expanded, so we didn't do anything. return false; } }
protected internal virtual bool CollapseNode(TreeGridNode node) { if (node.IsExpanded) { CollapsingEventArgs exp = new CollapsingEventArgs(node); this.OnNodeCollapsing(exp); if (!exp.Cancel) { this.LockVerticalScrollBarUpdate(true); this.SuspendLayout(); _inExpandCollapse = true; node.IsExpanded = false; foreach (TreeGridNode childNode in node.Nodes) { Debug.Assert(childNode.RowIndex != -1, "Row is NOT in the grid."); this.UnSiteNode(childNode); } CollapsedEventArgs exped = new CollapsedEventArgs(node); this.OnNodeCollapsed(exped); //TODO: Convert this to a specific NodeCell property _inExpandCollapse = false; this.LockVerticalScrollBarUpdate(false); this.ResumeLayout(true); this.InvalidateCell(node.Cells[0]); } return !exp.Cancel; } else { // row isn't expanded, so we didn't do anything. return false; } }