private void btnSalvar_Click(object sender, RoutedEventArgs e) { //gravar no banco de dados if (this.operacao == "inserir") { contato c = new contato(); c.nome = txtNome.Text; c.email = txtEmail.Text; c.telefone = txtTel.Text; using (agendaEntities ctx = new agendaEntities()) { ctx.contatos.Add(c); ctx.SaveChanges(); } } if (this.operacao == "alterar") { using (agendaEntities ct = new agendaEntities()) { contato c = ct.contatos.Find(Convert.ToInt32(txtID.Text)); if (c != null) { c.nome = txtNome.Text; c.email = txtEmail.Text; c.telefone = txtTel.Text; ct.SaveChanges(); } } } this.ListarContatos(); this.AlterarBotoes(1); this.LimpaCampos(); }
private void dgDados_MouseDoubleClick(object sender, MouseButtonEventArgs e) { if (dgDados.SelectedIndex >= 0) { //contato c = (contato)dgDados.Items[dgDados.SelectedIndex]; contato c = (contato)dgDados.SelectedItem; txtID.Text = c.id.ToString(); txtNome.Text = c.nome; txtEmail.Text = c.email; txtTel.Text = c.telefone; this.AlterarBotoes(3); } }
private void btnExcluir_Click(object sender, RoutedEventArgs e) { using (agendaEntities ctx = new agendaEntities()) { contato c = ctx.contatos.Find(Convert.ToInt32(txtID.Text)); if (c != null) { ctx.contatos.Remove(c); ctx.SaveChanges(); } this.ListarContatos(); this.AlterarBotoes(1); this.LimpaCampos(); } }
private void btnLocalizar_Click(object sender, RoutedEventArgs e) { if (txtID.Text.Trim().Count() > 0) { //buscar pelo código try { int id = Convert.ToInt32(txtID.Text); using (agendaEntities ctx = new agendaEntities()) { var consulta = ctx.contatos; dgDados.ItemsSource = consulta.ToList(); contato c = ctx.contatos.Find(id); dgDados.ItemsSource = new contato[1] { c }; } } catch { } } if (txtNome.Text.Trim().Count() > 0) { //buscar por nome try { using (agendaEntities ctx = new agendaEntities()) { var consulta = from c in ctx.contatos where c.nome.Contains(txtNome.Text) select c; dgDados.ItemsSource = consulta.ToList(); } } catch { } } if (txtEmail.Text.Trim().Count() > 0) { //buscar por email try { using (agendaEntities ctx = new agendaEntities()) { var consulta = from c in ctx.contatos where c.email.Contains(txtEmail.Text) select c; dgDados.ItemsSource = consulta.ToList(); } } catch { } } if (txtTel.Text.Trim().Count() > 0) { //buscar por telefone try { using (agendaEntities ctx = new agendaEntities()) { var consulta = from c in ctx.contatos where c.telefone.Contains(txtTel.Text) select c; dgDados.ItemsSource = consulta.ToList(); } } catch { } } }