/// <summary>
        /// Pega o registro através do formulário
        /// </summary>
        /// <param name="pFieldName">Nome do Campo que está sendo pesquisado</param>
        /// <param name="args">Argumentos do Evento gerado</param>
        /// <returns>True se pesquisou com sucesso</returns>
        private bool GetBySearchForm(string pFieldName, ref ChooseFromListEventArgs args)
        {
            try
            {
                IChooseFromListFormBase form;

                if (FormChoose == null)
                {
                    form = _formatConditions == null
                        ? new ChooseFromListForm()
                        : new ChooseFromListForm(_formatConditions, _columnsParamns);
                }
                else
                {
                    form = FormChoose.GetType().Assembly.CreateInstance(FormChoose.GetType().FullName) as IChooseFromListFormBase;
                }

                Debug.Assert(form != null, "form != null");

                form.AllowSelectionsMultiples = AllowSelectionsMultiples;
                form.Query         = Query;
                form.Text          = WindowText;
                form.SearchBy      = args.SearchBy == null ? String.Empty : args.SearchBy.ToString();
                form.VisbleColumns = VisbleColumns;
                form.SearchColumns.Add(Query.Fields.FirstOrDefault(p => p.Name.Equals(pFieldName)));
                form.SearchColumns.AddRange(WhereColumns);

                if (args.Record == null && _txtId != null)
                {
                    var argForCurrentRecord = new ChooseFromListEventArgs
                    {
                        Cancel   = false,
                        SearchBy = _txtId.EditValue
                    };

                    HasOneLine(FieldNameId, ref argForCurrentRecord);
                    form.Record = argForCurrentRecord.Record;
                }

                //form.ViewResult.FocusedColumn = ViewResult.Columns[pFieldName];
                if (form.ShowDialog(_ownerForm) == DialogResult.OK)
                {
                    args.Record  = form.Record;
                    args.Records = form.Records;
                    return(true);
                }
                else
                {
                    return(false);
                }
            }
            finally
            {
                _ownerForm.Select();
            }
        }
        void ChooseButtonClick()
        {
            try
            {
                InExecution = true;

                var args = new ChooseFromListEventArgs
                {
                    Cancel   = false,
                    SearchBy = string.Empty
                };

                if (BeforeTryGetRecord != null)
                {
                    BeforeTryGetRecord(this, args);
                }

                if (args.Cancel)
                {
                    return;
                }

                if (!GetBySearchForm(FieldNameDescription, ref args))
                {
                    return;
                }

                _editValueChangedByKeyDown = true;
                Validated = true;

                if (AfterTryGetRecord != null)
                {
                    AfterTryGetRecord(this, args);
                }

                if (!args.Cancel)
                {
                    SetRecord(args.Record);
                }

                _editValueChangedByKeyDown = false;
            }
            catch (Exception ex)
            {
                _ownerForm.ShowMessageError(ex);
            }
            finally
            {
                InExecution = false;
            }
        }
        /// <summary>
        /// Tenta obter um registro através da pesquisa passada
        /// </summary>
        /// <param name="pIsDescriptionField">O Campo Id é descrição do texto</param>
        /// <param name="pTextSearch">Texto de Pesquisa</param>
        /// <param name="pShowForm">Exibir formulário</param>
        /// <param name="pRow">Linha Corrente de pesquisa</param>
        /// <returns></returns>
        protected bool TryGetRecord(bool pIsDescriptionField, object pTextSearch, bool pShowForm, ref DataRow pRow)
        {
            var args = new ChooseFromListEventArgs {
                Cancel = false, SearchBy = pTextSearch
            };

            if (BeforeTryGetRecord != null)
            {
                BeforeTryGetRecord(this, args);
            }

            if (args.Cancel)
            {
                return(false);
            }

            var fieldName = pIsDescriptionField ? FieldNameDescription : FieldNameId;

            if (!HasOneLine(fieldName, ref args))
            {
                if (!pShowForm)
                {
                    return(false);
                }

                if (!GetBySearchForm(fieldName, ref args))
                {
                    return(false);
                }
            }

            if (AfterTryGetRecord != null)
            {
                AfterTryGetRecord(this, args);
            }

            pRow = args.Record;

            return(!args.Cancel);
        }
        private bool HasOneLine(string pFieldName, ref ChooseFromListEventArgs e)
        {
            var query = new TableQuery(Query)
            {
                Top = 2
            };

            var serarchField = new List <TableAdapterField>
            {
                query.Fields[pFieldName]
            };

            if (pFieldName == FieldNameDescription)
            {
                serarchField.AddRange(WhereColumns);
            }

            var where = new WhereCollection();

            foreach (var column in serarchField)
            {
                where.Add(column.DbTypeIsNumeric()
                    ? new QueryParam(column, e.SearchBy == null ? 0 : e.SearchBy.To <Int64>())
                               : new QueryParam(column, e.SearchBy ?? string.Empty));

                where.Last().Relationship = eRelationship.erOr;

                if (ConditionQuery != eCondition.ecNone)
                {
                    where.Last().Condition = ConditionQuery;
                }
            }

            query.Wheres.Add(where);

            var script = new SqlServerScriptWizard(query);

            var command = script.GetSelectStatment();
            var data    = Connection.Instance.SqlServerQuery(command.Item1, command.Item2);

            if (data.Rows.Count == 1)
            {
                e.Record = data.Rows[0];
            }
            else
            {
                serarchField = new List <TableAdapterField> {
                    query.Fields[pFieldName]
                };

                if (pFieldName == FieldNameDescription)
                {
                    serarchField.AddRange(WhereColumns);
                }

                where = new WhereCollection();

                foreach (var column in serarchField)
                {
                    @where.Add(column.DbTypeIsNumeric()
                                   ? new QueryParam(column, e.SearchBy == null ? 0 : e.SearchBy.To <Int64>())
                                   : new QueryParam(column, eCondition.ecLike, e.SearchBy ?? String.Empty));

                    where.Last().Relationship = eRelationship.erOr;
                }

                query.Wheres.Add(where);

                var comand = script.GetSelectStatment();
                data = Connection.Instance.SqlServerQuery(comand.Item1, comand.Item2);

                if (data.Rows.Count == 1)
                {
                    e.Record = data.Rows[0];
                }
            }

            return(data.Rows.Count == 1);
        }