private void SearchButton_Click(object sender, EventArgs e)
        {
            if (this.Context != null && SearchPanelHelpers.HasSearchPanel(this.ModelName))
            {
                SearchPanel panel = SearchPanelHelpers.CreatePanelForModel(this.ModelName);
                panel.Context = this.Context;

                SearchDialog dialog = new SearchDialog(panel);
                dialog.ShowDialog();

                if (dialog.Result != null)
                {
                    this.SelectedId = dialog.Result.GetId();
                    this.NameText.InvokeEx(() => this.NameText.Text = dialog.Result.GetDisplayName());
                    this.SetOpenButtonState();
                }
            }
        }
        private void OnControlBegin(object sender, DataGridViewEditingControlShowingEventArgs eventArgs)
        {
            int currentCol = this.DataGrid.CurrentCell.ColumnIndex;
            int currentRow = this.DataGrid.CurrentCell.RowIndex;

            DataRowView remappedRow = this.GetRowView(currentRow);

            EntityModel model = this.GetRowModel(remappedRow);

            string columnModelName = this.DataGrid.Columns[currentCol].DataPropertyName;
            FieldModel field = model.GetField(this.ModelMappings[model.ModelName].TableToModel[columnModelName]);

            string immediateValue = remappedRow[columnModelName].ToString();
            string trueValue = remappedRow[this.TrueColumnName(columnModelName)].ToString();

            if (field is EntityFieldModel)
            {
                EntityFieldModel entityField = field as EntityFieldModel;

                var cache = this.Context.GetEntitySelectionCache(entityField.ModelName);

                cache.RequestRefresh();
                cache.DataRefreshed += this.ResetControlCache;
                var cacheTable = cache.GetLatestCache();

                if (cacheTable.Count > 0)
                    this.DataGrid.Columns[currentCol].Width = cacheTable.EnumerateLeftToRight().Select(p => p.Value.Length).Max() * 10;
                else
                    this.DataGrid.Columns[currentCol].Width= 80;

                this.ResetControlCache(cacheTable, trueValue);

                TextBoxBase selector = this.DataGrid.EditingControl as TextBoxBase;

                MenuItem searchMenuItem = new MenuItem()
                {
                    Text = "Search...",
                };

                searchMenuItem.Click += (o, e) =>
                {
                    SearchPanel panel = SearchPanelHelpers.CreatePanelForModel(entityField.ModelName);
                    panel.Context = this.Context;
                    SearchDialog dialog = new SearchDialog(panel);
                    dialog.ShowDialog();

                    if (dialog.Result != null)
                    {
                        remappedRow[columnModelName] = dialog.Result.GetDisplayName();
                        selector.Text = dialog.Result.GetDisplayName();
                        remappedRow[this.TrueColumnName(columnModelName)] = dialog.Result.GetId().ToString();
                    }
                };

                MenuItem refreshMenuItem = new MenuItem()
                {
                    Text = "Force Refresh",
                };

                refreshMenuItem.Click += (o, e) =>
                {
                    cache.RequestRefresh(EntitySelectionCacheRefreshType.Force);
                };

                selector.ContextMenu = new ContextMenu()
                {
                    MenuItems =
                    {
                        searchMenuItem,
                        refreshMenuItem,
                    }
                };
            }
        }