private void Insere()
 {
     mPeca model;
     rPeca regra = new rPeca();
     rPecaEstoque regraPecaEstoque = new rPecaEstoque();
     rPecaFornecedor regraPecaFornecedor = new rPecaFornecedor();
     try
     {
         this.ValidaDadosNulos();
         model = this.PegaDadosTela();
         regra.ValidarInsere(model);
         //Verifica se existe itens na lista de peça estoque
         //-------------------------------------------------
         if (this._listaPecaEstoque != null)
         {
             if (this._listaPecaEstoque.Count > 0)
             {
                 this.PopulaListaPecaEstoqueIdPeca(Convert.ToInt32(model.IdPeca));
                 foreach (mPecaEstoque modelPecaEstoque in this._listaPecaEstoque)
                 {
                     regraPecaEstoque.ValidarInsere(modelPecaEstoque);
                 }
             }
         }
         //Verifica se existe itens na lista de peça fornecedor
         //-------------------------------------------------
         if (this._listaPecaFornecedor != null)
         {
             if (this._listaPecaFornecedor.Count > 0)
             {
                 this.PopulaListaPecaFornecedorIdPeca(Convert.ToInt32(model.IdPeca));
                 foreach (mPecaFornecedor modePecaFornecedor in this._listaPecaFornecedor)
                 {
                     regraPecaFornecedor.ValidarInsere(modePecaFornecedor);
                 }
             }
         }
         base.LimpaDadosTela(this);
         this.btnConfirma.Enabled = false;
         MessageBox.Show("Registro Salvo com Sucesso!", "Atenção", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1);
     }
     catch (BUSINESS.Exceptions.Peca.CodigoPecaVazioException)
     {
         MessageBox.Show("O código da Peça não pode ser vazio", "Atenção", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1);
         this.txtCodigoReal.Focus();
     }
     catch (BUSINESS.Exceptions.CodigoTipoPecaVazioException)
     {
         MessageBox.Show("O Tipo da Peça não pode ser vazio", "Atenção", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1);
         this.btnCdTipoPeca.Focus();
     }
     catch (BUSINESS.Exceptions.Peca.NomePecaVazioException)
     {
         MessageBox.Show("É Necessário Preencher o nome da Peça", "Atenção", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1);
         this.txtNmPeca.Focus();
     }
     catch (BUSINESS.Exceptions.Peca.QtdMinimaNuloOuZeroException)
     {
         MessageBox.Show("A quantidade mínima da Peça deve ser maior que zero", "Atenção", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1);
         this.txtQtdPeca.Focus();
     }
     catch (BUSINESS.Exceptions.Peca.PecaJaExistenteException)
     {
         MessageBox.Show("Peça já existente! Favor Cadastrar outro Código Real", "Atenção", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1);
         this.txtCodigoReal.Focus();
     }
     catch (Exception ex)
     {
         MessageBox.Show(ex.Message, "Atenção", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1);
     }
     finally
     {
         model = null;
         regra = null;
         regraPecaEstoque = null;
     }
 }
        private void MarcaPecaEstoque()
        {
            DataTable dtPecaEstoqueTela = null;
            DataTable dtPecaEstoqueBanco = null;
            rPecaEstoque regraPecaEstoque = null;

            DataTable dtTemp = new DataTable();

            try
            {
                dtTemp.Columns.Add("id_estoq");
                dtTemp.Columns.Add("Estoque");
                dtTemp.Columns.Add("qtd_peca");
                DataRow drLinha;

                regraPecaEstoque = new rPecaEstoque();
                int idEstoqueTela, idEstoqueBanco;

                dtPecaEstoqueBanco = regraPecaEstoque.BuscaPecaEstoquePorPeca(_modelPeca);
                dtPecaEstoqueTela = (DataTable)dgEstoques.DataSource;
                if (dtPecaEstoqueTela == null)
                {
                    dtPecaEstoqueTela = new DataTable();
                }
                for (int linhaTela = 0; linhaTela < dtPecaEstoqueTela.Rows.Count; linhaTela++)
                {
                    drLinha = dtTemp.NewRow();

                    drLinha["id_estoq"] = dtPecaEstoqueTela.Rows[linhaTela]["id_estoq"];
                    drLinha["Estoque"] = dtPecaEstoqueTela.Rows[linhaTela]["Estoque"];
                    drLinha["qtd_peca"] = dtPecaEstoqueTela.Rows[linhaTela]["qtd_peca"];

                    idEstoqueTela = Convert.ToInt32(dtPecaEstoqueTela.Rows[linhaTela]["id_estoq"]);
                    for (int linhaBanco = 0; linhaBanco < dtPecaEstoqueBanco.Rows.Count; linhaBanco++)
                    {
                        idEstoqueBanco = Convert.ToInt32(dtPecaEstoqueBanco.Rows[linhaBanco]["id_estoq"]);
                        if (idEstoqueBanco == idEstoqueTela)
                        {
                            drLinha["qtd_peca"] = dtPecaEstoqueBanco.Rows[linhaBanco]["qtd_peca"];
                            continue;
                        }
                    }

                    dtTemp.Rows.Add(drLinha);
                }

                dgEstoques.DataSource = dtTemp;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                regraPecaEstoque = null;
                dtPecaEstoqueTela = null;
                dtPecaEstoqueBanco = null;
            }
        }
        /// <summary>
        /// Insere os dados que estão no model
        /// </summary>
        private void Insere()
        {
            rPecaEstoque regra = new rPecaEstoque();

            try
            {
                this.PopulaListaModel();
                if (this._telaMotor == false)
                {
                    this.ValidaDadosNulos();

                    // exclui antes tudo antes de inserir
                    this.DeletaTudoPorMotor();

                    foreach (mMotorEstoque modelMotorEstoque in this._listaModelMotorEstoque)
                    {
                        regra.ValidarInsere(modelMotorEstoque);
                    }
                    this.btnLimpa_Click(null, null);
                    MessageBox.Show("Registro Salvo com Sucesso!", "Atenção", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1);
                }
                else
                {
                    this.DialogResult = DialogResult.OK;
                    base.FechaTela(this);
                }
            }
            catch (BUSINESS.Exceptions.MotorEstoque.MotorVazioException)
            {
                MessageBox.Show("É Necessário Buscar um Motor", "Atenção", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1);
                this.btnBuscaMotor.Focus();
            }
            catch (BUSINESS.Exceptions.MotorEstoque.EstoqueNaoEscolhidoException)
            {
                MessageBox.Show("É Necessário Selecionar um Estoque", "Atenção", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                regra = null;
            }
        }
 private void DeletaTudoPorPeca()
 {
     rPecaEstoque regraPecaEstoque = null;
     try
     {
         regraPecaEstoque = new rPecaEstoque();
         regraPecaEstoque.DeletaPecaEstoqueporPeca(_modelPeca.IdPeca);
     }
     catch (Exception ex)
     {
         throw ex;
     }
     finally
     {
         regraPecaEstoque = null;
     }
 }