private void LimpaCampos()
 {
     TxtNotaFiscal.Clear();
     TxtTotalNota.Clear();
     CboFornecedor.Text = "Selecione";
     dgvProdutos.Rows.Clear();
     TxtNotaFiscal.Focus();
     BtnFechaNota.Enabled  = true;
     LblTotalParcial.Text  = "";
     CboFornecedor.Enabled = true;
     TxtProduto.Enabled    = true;
     TxtQuant.Enabled      = true;
     TxtPrecoCusto.Enabled = true;
     TxtTotalNota.Enabled  = true;
     DataVencNF.Enabled    = true;
     CmbCadPor.Text        = "Selecione";
     CmbCadPor.Enabled     = true;
 }
        private void BtnFechaNota_Click(object sender, EventArgs e)
        {
            if (TxtNotaFiscal.Text == "")
            {
                MessageBox.Show("Falta Número Nota Fiscal!", "SIG", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                TxtNotaFiscal.Focus();
            }
            else if (TxtTotalNota.Text == "")
            {
                MessageBox.Show("Falta Total da Nota Fiscal!", "SIG", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                TxtTotalNota.Focus();
            }
            else if (CboFornecedor.Text == "" || CboFornecedor.Text == "Selecione")
            {
                MessageBox.Show("Selecione o Fornecedor!", "SIG", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                CboFornecedor.Focus();
            }
            else if (CmbCadPor.Text == "Selecione")
            {
                MessageBox.Show("Selecione o Usuário", "SIG", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                CmbCadPor.Focus();
            }
            else if (dgvProdutos.RowCount == 0)
            {
                MessageBox.Show("Nenhum Item na Nota Fiscal!", "SIG", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                TxtFabricante.Focus();
            }

            else
            {
                try
                {
                    conn.Open();
                    SqlCommand INSERIRNOTAFISCAL = new SqlCommand
                    {
                        Connection  = conn,
                        CommandType = CommandType.Text,
                        CommandText = "INSERT INTO NotaFiscal(NotaFiscalNumero,TotalNota,DataLanc,DataVenc,Id_Fornecedor,Id_Usuario) VALUES ('" + TxtNotaFiscal.Text + "','" + TxtTotalNota.Text.Replace(',', '.') + "','" + DateTime.Now + "','" + DataVencNF.Value.ToString("dd/MM/yyyy") + "',(select Id_Fornecedor from Fornecedor where fornecedor.RazaoSocial='" + CboFornecedor.Text + "'),(select id_usuario from usuario where usuario.usuario='" + CmbCadPor.Text + "'))",
                    };
                    INSERIRNOTAFISCAL.ExecuteNonQuery();

                    SqlCommand INSERIRNOTAFISCALITENS = new SqlCommand
                    {
                        Connection  = conn,
                        CommandType = CommandType.Text,
                        //CommandText = "insert into NotaFiscalItens values(1, 555, 'c#', 5.55, 1, 3)"
                        CommandText = "INSERT INTO NotaFiscalItens VALUES (@Id_Produto,@Qtd,@Descricao,@PrecoUnitario,'" + Convert.ToInt16(TxtNotaFiscal.Text) + "',select notafiscal.id_notafiscal from NotaFiscal inner join notafiscalitens on notafiscal.Id_NotaFiscal=notafiscalitens.Id_NotaFiscal)",
                    };

                    for (int i = 0; i < dgvProdutos.Rows.Count; i++)
                    {
                        INSERIRNOTAFISCALITENS.Parameters.Clear();
                        INSERIRNOTAFISCALITENS.Parameters.AddWithValue("@Id_Produto",
                                                                       dgvProdutos.Rows[i].Cells[0].Value);
                        INSERIRNOTAFISCALITENS.Parameters.AddWithValue("@Qtd",
                                                                       dgvProdutos.Rows[i].Cells[4].Value);
                        INSERIRNOTAFISCALITENS.Parameters.AddWithValue("@Descricao",
                                                                       dgvProdutos.Rows[i].Cells[2].Value);
                        INSERIRNOTAFISCALITENS.Parameters.AddWithValue("@Precounitario",
                                                                       dgvProdutos.Rows[i].Cells[3].Value);
                        INSERIRNOTAFISCALITENS.ExecuteNonQuery();
                    }

                    INSERIRNOTAFISCALITENS.ExecuteNonQuery();
                    MessageBox.Show("Nota Fiscal Gravada com Sucesso!", "SIG", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    conn.Close();
                    LimpaCampos();
                }
                catch
                {
                    MessageBox.Show("Erro ao Gravar Nota Fiscal! Verifique.", "SIG", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                conn.Close();
            }
        }