/// <summary> /// Initializes the query results grid to receive results for the specified ManagementClass. /// </summary> /// <param name="c">The System.Management.ManagementClass to be displayed.</param> private void InitQueryResultGrid(ManagementClass c, String query) { // Get the type of query to build columns for var queryType = query.GetWqlQueryType(); // Reset grid config this.gridQueryResults.Rows.Clear(); this.gridQueryResults.Columns.Clear(); // Configure grid context menu this.btnGetAssociatorsOf.Visible = this.btnGetReferencesOf.Visible = this.btnResultPropertiesSeparater.Visible = queryType == WqlQueryType.Select; // Create column for result count DataGridViewTextBoxColumn colIndex = new DataGridViewTextBoxColumn(); colIndex.Name = colIndex.HeaderText = "#"; colIndex.DefaultCellStyle.BackColor = SystemColors.ControlLight; colIndex.DefaultCellStyle.ForeColor = SystemColors.ControlDark; colIndex.AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells; // Create column for result inspector link DataGridViewImageColumn colInspector = new DataGridViewImageColumn(); colInspector.Image = this.ImageList1.Images["Property"]; colInspector.AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells; this.gridQueryResults.Columns.AddRange(colIndex, colInspector); // Add a column for each available result property if (queryType == WqlQueryType.Select) { // Extract properties from selected fields in the query itself. // This may be all properties or a subset (Eg. SELECT Caption from Win32_ComputerSystem). var properties = query.GetWqlQueryProperties(c); // Create a column for each property foreach (PropertyData p in properties) { DataGridViewColumn colProperty; // Create hyperlink columns for objects if (p.Type == CimType.Object || p.Type == CimType.Reference) { colProperty = new DataGridViewLinkColumn(); DataGridViewLinkColumn link = (DataGridViewLinkColumn)colProperty; link.LinkBehavior = LinkBehavior.HoverUnderline; link.VisitedLinkColor = link.LinkColor; link.ActiveLinkColor = link.LinkColor; } else { colProperty = new DataGridViewTextBoxColumn(); } colProperty.Name = colProperty.HeaderText = p.Name; if (p.IsKey()) { colProperty.DefaultCellStyle.BackColor = SystemColors.Info; colProperty.DefaultCellStyle.ForeColor = SystemColors.InfoText; } if (c.IsAssociation()) colProperty.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; else colProperty.AutoSizeMode = DataGridViewAutoSizeColumnMode.NotSet; this.gridQueryResults.Columns.Add(colProperty); } } else { // Create columns for a ASSOCIATORS OF or REFERENCES OF query DataGridViewLinkColumn col = new DataGridViewLinkColumn(); col.LinkBehavior = LinkBehavior.HoverUnderline; col.VisitedLinkColor = col.LinkColor; col.ActiveLinkColor = col.LinkColor; col.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; switch (queryType) { case WqlQueryType.AssociatorsOf: col.Name = "Association"; break; case WqlQueryType.ReferencesOf: col.Name = "Reference"; break; } this.gridQueryResults.Columns.Add(col); } }