private void OnEditTest(object sender, CommandEventArgs e)
 {
     if (DetailView is TestBrowser tb && tb.Grid.SelectedRows != null && tb.Grid.SelectedRows.Count == 1)
     {
         TestObjectAdapter targetTestObject = (TestObjectAdapter)tb.Grid.SelectedRows[0].DataBoundItem;
         using (NewTestDialog ntd = new NewTestDialog(ManagementGroup, null, null, targetTestObject.Source.Id))
         {
             ntd.ShowDialog();
         }
         // emulate Refresh command for detail view
         SendRefreshCommandToDetailView();
     }
 }
        private void OnDeleteTest(object sender, CommandEventArgs e)
        {
            if (DetailView is TestBrowser tb && tb.Grid.SelectedRows != null && tb.Grid.SelectedRows.Count > 0)
            {
                try
                {
                    TestObjectAdapter firstDataItem = (TestObjectAdapter)tb.Grid.SelectedRows[0].DataBoundItem;

                    bool doDelete = false;
                    if (tb.Grid.SelectedRows.Count == 1)
                    {
                        doDelete = MessageBox.Show($"You're about to delete the {firstDataItem.DisplayName} destination.\r\nAre you sure?", "Warning!", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes;
                    }
                    else
                    {
                        doDelete = MessageBox.Show($"You're about to delete multiple destinations.\r\nAre you sure?", "Warning!", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes;
                    }
                    if (doDelete)
                    {
                        EnterpriseManagementConnector connector            = ManagementGroup.ConnectorFramework.GetMonitoringConnector();
                        IncrementalDiscoveryData      incrementalDiscovery = new IncrementalDiscoveryData();
                        ManagementPackClass           fqdnClass            = ManagementGroup.EntityTypes.GetClass(IDs.FullyQualifiedDomainNameClassId);

                        foreach (DataGridViewRow gridRow in tb.Grid.SelectedRows)
                        {
                            TestObjectAdapter dataItem      = (TestObjectAdapter)gridRow.DataBoundItem;
                            MonitoringObject  currentObject = ManagementGroup.EntityObjects.GetObject <MonitoringObject>(dataItem.Source.Id, ObjectQueryOptions.Default);
                            CreatableEnterpriseManagementObject oldInstance = new CreatableEnterpriseManagementObject(ManagementGroup, currentObject.GetMostDerivedClasses().First());
                            // host keys
                            oldInstance[IDs.FullyQualifiedDomainNameClassProperties.FullyQualifiedDomainNamePropertyId].Value = currentObject[IDs.FullyQualifiedDomainNameClassProperties.FullyQualifiedDomainNamePropertyId].Value;
                            oldInstance[IDs.FullyQualifiedDomainNameClassProperties.TargetIndexPropertyId].Value = currentObject[IDs.FullyQualifiedDomainNameClassProperties.TargetIndexPropertyId].Value;
                            // key
                            oldInstance[IDs.TestBaseClassProperties.TestIdPropertyId].Value = currentObject[IDs.TestBaseClassProperties.TestIdPropertyId].Value;
                            incrementalDiscovery.Remove(oldInstance);
                        }

                        incrementalDiscovery.Commit(connector);
                    }

                    // emulate Refresh command for detail view
                    SendRefreshCommandToDetailView();
                }
                catch (Exception ex)
                {
                    MessageBox.Show($"In {System.Reflection.MethodBase.GetCurrentMethod().Name} {ex.GetType().Name} said {ex.Message} at {ex.StackTrace}");
                }
            }
        }