private void dgvListaComestiveis_CellDoubleClick(object sender, DataGridViewCellEventArgs e) { int id = Convert.ToInt32(dgvListaComestiveis.CurrentRow.Cells[0].Value); RepositorioComestiveis repositorio = new RepositorioComestiveis(); Comestivel comestivel = repositorio.ObterPeloId(id); CadastroAlterar cadastroAlterar = new CadastroAlterar(comestivel); cadastroAlterar.ShowDialog(); }
private void ApagarDaLista() { if (dgvListaComestiveis.SelectedRows.Count == 0) { MessageBox.Show("É preciso selecionar uma linha antes"); return; } int id = Convert.ToInt32(dgvListaComestiveis.CurrentRow.Cells[0].Value); RepositorioComestiveis repositorio = new RepositorioComestiveis(); DialogResult resultado = MessageBox.Show("Você tem certeza?", "AVISO", MessageBoxButtons.YesNo); if (resultado == DialogResult.Yes) { repositorio.Deletar(id); AtualizarTabela(); } }
public void AtualizarTabela() { RepositorioComestiveis repositorio = new RepositorioComestiveis(); List <Comestivel> comestiveis = repositorio.ObterTodos(); dgvListaComestiveis.Rows.Clear(); for (int i = 0; i < comestiveis.Count; i++) { Comestivel comestivel = comestiveis[i]; decimal precoTotal = comestivel.Preco * comestivel.Quantidade; string precoTotalTexto = $"R$: {precoTotal}"; string precoTexto = $"R$: {comestivel.Preco}"; dgvListaComestiveis.Rows.Add(new object[] { comestivel.Id, comestivel.Nome, comestivel.Marca, precoTexto, comestivel.Quantidade, precoTotalTexto }); } }
private void SalvarRegistro() { Comestivel comestivel = new Comestivel(); comestivel.Id = Convert.ToInt32(lblID.Text); comestivel.Nome = txtNome.Text; if (comestivel.Nome.Length < 3) { MessageBox.Show("Por favor, registre o nome do produto."); txtNome.Focus(); return; } comestivel.Marca = txtMarca.Text; if (comestivel.Marca.Length < 3) { MessageBox.Show("Por favor, registre a marca do produto."); txtMarca.Focus(); return; } try { comestivel.Preco = Convert.ToDecimal(txtPreco.Text); if (comestivel.Preco < 0) { MessageBox.Show("O valor não pode ser menor que 0"); txtPreco.Focus(); return; } } catch (Exception) { MessageBox.Show("Somente números"); txtPreco.Focus(); return; } try { comestivel.Quantidade = Convert.ToInt32(txtQuantidade.Text); if (comestivel.Quantidade < 0) { MessageBox.Show("Somente números maiores que 0"); txtQuantidade.Focus(); return; } } catch (Exception) { MessageBox.Show("Somente números!"); txtQuantidade.Focus(); return; } comestivel.DataVencimento = dtpDataVencimento.Value; DateTime hoje = DateTime.Now; if (comestivel.DataVencimento <= hoje) { MessageBox.Show("Por favor selecione a data de validade"); dtpDataVencimento.Select(); SendKeys.Send("%{DOWN}"); return; } RepositorioComestiveis repositorio = new RepositorioComestiveis(); repositorio.AtualizarRegistro(comestivel); Close(); }