private void OnCellValueChanged
        (
            object sender,
            DataGridViewCellEventArgs e
        )
        {
            if (e.ColumnIndex != 1)
            {
                return;
            }

            if (e.RowIndex < 0 || e.ColumnIndex < 0)
            {
                return;
            }

            IECClass ecClass = getECClassInformationForCurrentSelection(e.RowIndex);
            //this[ClassColumn, e.RowIndex].Tag as IECClass;
            IECRelationshipClass ecRelClass = getECRelationshipClassInformationForCurrentSelection(e.RowIndex);
            //this[ClassColumn, e.RowIndex].Tag as IECRelationshipClass;
            string strVal = LocalizableStrings.None;

            if (null != this[ComboBoxColumn, e.RowIndex].Value)
            {
                strVal = this[ComboBoxColumn, e.RowIndex].Value.ToString();
            }

            if (strVal == _cstr_Varies)
            {
                return;
            }

            DgnUtilities dgnUtil = DgnUtilities.GetInstance();

            IECInstance newECIsnt = getInstanceForCurrentSelection(e.RowIndex, strVal);

            // get from Tag
            //dgnUtil.GetSourceInstance (ecClass, strVal);

            foreach (IECInstance ecInstance in _instList)
            {
                IECRelationshipInstanceCollection     ecRelCol      = ecInstance.GetRelationshipInstances();
                IEnumerator <IECRelationshipInstance> ecRelInstEnum = ecRelCol.GetEnumerator();

                bool newSourceSet = false;
                while (ecRelInstEnum.MoveNext())
                {
                    IECRelationshipInstance ecRelInst = ecRelInstEnum.Current;
                    if (!ecRelInst.ClassDefinition.Is(ecRelClass))
                    {
                        continue;
                    }

                    if (strVal == LocalizableStrings.None || string.IsNullOrEmpty(strVal))
                    {
                        ecRelCol.Remove(ecRelInst);
                    }
                    else
                    {
                        ecRelInst.Source = newECIsnt;
                    }
                    newSourceSet = true;
                    break;
                }

                if (!newSourceSet && newECIsnt != null)
                {
                    IECRelationshipInstance temporaryRelInst = ecRelClass.CreateInstance() as IECRelationshipInstance;
                    temporaryRelInst.Source = newECIsnt;
                    temporaryRelInst.Target = ecInstance;
                    ecInstance.GetRelationshipInstances().Add(temporaryRelInst);
                }

                IECInstance ecInst = newECIsnt;
                if (ecInst != null)
                {
                    if ((dgnUtil.IsECInstanceFromReferenceFile(ecInst) || dgnUtil.IsECInstanceReferencedOut(ecInst) || ecInst.IsReadOnly))
                    {
                        ecInst = null;
                    }
                }

                DataGridViewImageCell tempImageCell = this["optionEdit", e.RowIndex] as DataGridViewImageCell;
                tempImageCell.Tag = ecInst;

                tempImageCell     = this["optionDelete", e.RowIndex] as DataGridViewImageCell;
                tempImageCell.Tag = ecInst;

                //Mark the property (having business key CA/or NAME property if no Business key is defined) as changed and set the event which will reset the property pane.
                //This is required to update UNIT and SERVICE property listed in property pane on the fly whenever user changes the UNIT or SERVICE in ASSOCIATIONS pane.
                IECPropertyValue propVal = BusinessKeyUtility.GetPropertyValue(ecInstance);
                if (propVal != null)
                {
                    ecInstance.OnECPropertyValueChanged(propVal);
                }
            }

            SaveSettings();
        }
        private void OnCellMouseClick
        (
            object sender,
            DataGridViewCellMouseEventArgs e
        )
        {
            if (e.RowIndex == -1)
            {
                return;
            }

            if (e.ColumnIndex == 1)
            {
                this.BeginEdit(true);
                ComboBox comboBox = (ComboBox)this.EditingControl;
                if (null != comboBox)
                {
                    comboBox.DroppedDown = true;
                }
            }

            if (e.ColumnIndex < AddButtonColumn)
            {
                return;
            }

            DgnUtilities dgnUtil = DgnUtilities.GetInstance();
            IECClass     ecClass = getECClassInformationForCurrentSelection(e.RowIndex);
            //this[ClassColumn, e.RowIndex].Tag as IECClass;
            IECRelationshipClass ecRelClass = getECRelationshipClassInformationForCurrentSelection(e.RowIndex);
            //this[ComboBoxColumn, e.RowIndex].Tag as IECRelationshipClass;
            DataGridViewComboBoxCell itemInstanceCell = this[s_WBSInstanceKey, e.RowIndex] as DataGridViewComboBoxCell;

            string strVal = this[s_WBSInstanceKey, e.RowIndex].Value as string;

            if (strVal == LocalizableStrings.None && (e.ColumnIndex != AddButtonColumn && e.ColumnIndex != BrowseButtonColumn))
            {
                this[e.ColumnIndex, e.RowIndex].Selected = false;
                return;
            }

            //ADD///////////////////////////////////////////////////////////////////////////////////////////////////////////
            if (e.ColumnIndex == AddButtonColumn)
            {
                // If we are connected to iModelHub we should acquire a a new tag for WBS

                ECInstanceDialog ecDlg = new ECInstanceDialog(ecClass.Name);
                ecDlg.ShowDialog();
                //User didn't save the new instance
                if (!ecDlg.SavePressed)
                {
                    return;
                }
                //We were not able to save the new instance
                if (!ecDlg.SaveData())
                {
                    return;
                }

                RefillItems(e.RowIndex, ecClass.Name);

                if (itemInstanceCell.Items.Count > 0)
                {
                    itemInstanceCell.Value = SchemaUtilities.GetDisplayNameForECInstance(ecDlg.GetECInstance(), false);
                }
            }
            //EDIT//////////////////////////////////////////////////////////////////////////////////////////////////////////
            else if (e.ColumnIndex == EditButtonColumn)
            {
                IECInstance ecInst = this[e.ColumnIndex, e.RowIndex].Tag as IECInstance;
                if (ecInst == null)
                {
                    MessageBox.Show(LocalizableStrings.NoEditDesc, LocalizableStrings.NoEditTitle, MessageBoxButtons.OK, MessageBoxIcon.Stop);
                }
                else
                {
                    IECInstance ecInstance = EditECInstance(ecInst);
                    if (ecInstance == null)
                    {
                        return;
                    }

                    RefillItems(e.RowIndex, ecClass.Name);

                    if (itemInstanceCell.Items.Count > 0)
                    {
                        itemInstanceCell.Value = SchemaUtilities.GetDisplayNameForECInstance(ecInstance, false);
                    }

                    _bInstanceModified = true;
                }
            }
            //DELETE////////////////////////////////////////////////////////////////////////////////////////////////////////
            else if (e.ColumnIndex == DeleteButtonColumn)
            {
                IECInstance ecInst = this[e.ColumnIndex, e.RowIndex].Tag as IECInstance;
                if (ecInst == null)
                {
                    MessageBox.Show(LocalizableStrings.NoDeleteDescRefOut, LocalizableStrings.NoDeleteTitle, MessageBoxButtons.OK, MessageBoxIcon.Stop);
                }
                else
                {
                    //.GetRelationshipInstances (ecClass.Name, _instance.ClassDefinition.Name, ecRelClass.Name, dgnUtil.GetDGNConnection());
                    ECInstanceList ecList = dgnUtil.GetRelatedInstances(ecInst, ecRelClass, _ClassDef, dgnUtil.GetDGNConnection());
                    if (ecList.Count > 0)
                    {
                        MessageBox.Show(string.Format(LocalizableStrings.NoDeleteDescAssocCompFound, itemInstanceCell.Value), LocalizableStrings.NoDeleteTitle, MessageBoxButtons.OK, MessageBoxIcon.Stop);
                        return;
                    }

                    if (MessageBox.Show(String.Format(LocalizableStrings.AreYouSureDelete, SchemaUtilities.GetDisplayNameForECInstance(ecInst, true)),
                                        String.Format(LocalizableStrings.DeleteInstanceTitle, ecInst.ClassDefinition.DisplayLabel), MessageBoxButtons.YesNo, MessageBoxIcon.Stop) == DialogResult.No)
                    {
                        return;
                    }

                    dgnUtil.DeleteECInstanceFromDgn(ecInst, dgnUtil.GetDGNConnection());

                    RefillItems(e.RowIndex, ecClass.Name);

                    if (itemInstanceCell.Items.Count > 0)
                    {
                        itemInstanceCell.Value = itemInstanceCell.Items[0];
                    }
                    else
                    {
                        itemInstanceCell.Value = null;
                    }
                }
                _bInstanceModified = true;
            }
            //BROWSE////////////////////////////////////////////////////////////////////////////////////////////////////////
            else if (e.ColumnIndex == BrowseButtonColumn)
            {
                IECInstance newEcinstance = ecClass.CreateInstance() as IECInstance;
                if (newEcinstance == null)
                {
                    return;
                }

                object instance = dgnUtil.TagBrowser(newEcinstance, dgnUtil.AssoicatedItemsTagBrowserOverrides);
                if (instance == null || !(instance is IECInstance))
                {
                    return;
                }
                IECInstance selectedECInstance = instance as IECInstance;

                //CommonTools.BIMAdapter.BIMAdapter.ReleaseTag (newEcinstance);
                dgnUtil.CopyProperties(selectedECInstance, newEcinstance, false);
                //BusinessKeyUtility.Release (newEcinstance);
                BusinessKeyUtility.SetIsReserved(newEcinstance, true);
                dgnUtil.UpdateFunctionalGuidProperty(selectedECInstance, newEcinstance);
                dgnUtil.WriteECInstanceToDgn(newEcinstance, dgnUtil.GetDGNConnection());

                //CommonTools.BIMAdapter
                //IECInstance inst = EditECInstance (ecInstance);
                //if(inst == null)
                //    return;
                //    inst = ecInstance;

                RefillItems(e.RowIndex, ecClass.Name);

                if (itemInstanceCell.Items.Count > 0)
                {
                    itemInstanceCell.Value = SchemaUtilities.GetDisplayNameForECInstance(newEcinstance, false);
                }

                _bInstanceModified = true;
            }
            this[e.ColumnIndex, e.RowIndex].Selected = false;
        }