예제 #1
0
        private void tbSalvar_Click(object sender, System.EventArgs e)
        {
            try
            {
                var Medico = (Medico)dgResultado.CurrentRow.DataBoundItem;
                int index  = Util.lstMedicos.IndexOf(Medico);
                validationInsertUpdate(Medico);

                if (MedicoDao.Select(new List <Tuple <string, object, string> >()
                {
                    new Tuple <string, object, string>("CPF", Medico.CPF, "="),
                    new Tuple <string, object, string>("ID", Medico.ID, "<>")
                }).Count > 0)
                {
                    throw new Exception("Já existe Medico com o CPF informado");
                }

                if (!String.IsNullOrEmpty(Medico.CRM) &&
                    MedicoDao.Select(new List <Tuple <string, object, string> >()
                {
                    new Tuple <string, object, string>("CRM", Medico.CRM, "="),
                    new Tuple <string, object, string>("ID", Medico.ID, "<>")
                }).Count > 0)
                {
                    throw new Exception("Já existe Medico com o CRM informado");
                }

                Util.lstMedicos.RemoveAt(index);
                MedicoDao.Update(Medico);
                Util.lstMedicos.Add(Medico);
                Util.lstMedicos = Util.lstMedicos.OrderBy(x => x.ID).ToList();
                MessageBox.Show(this, "Medico alterado com sucesso", "Medico");

                dgResultado.DataSource = null;
                dgResultado.DataSource = Util.lstMedicos;
                formOnEndTask();
            }
            catch (Exception ex)
            {
                MessageBox.Show(this, ex.Message, "Erro", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }