private void AddButton_Click(object sender, EventArgs e)
 {
     if (action == "add")
     {
         Contact c = new Contact();
         c.FirstName   = FirstName.Text;
         c.LastName    = LastName.Text;
         c.Address     = Address.Text;
         c.HomePhone   = HomePhone.Text;
         c.MobilePhone = MobilePhone.Text;
         c.Email       = Email.Text;
         db.Contacts.Add(c);
     }
     else
     {
         Contact c;
         c             = db.Contacts.Find(id);
         c.FirstName   = FirstName.Text;
         c.LastName    = LastName.Text;
         c.Address     = Address.Text;
         c.HomePhone   = HomePhone.Text;
         c.MobilePhone = MobilePhone.Text;
         c.Email       = Email.Text;
     }
     db.SaveChanges();
     (frm as MainForm).ReloadGrid();
     Close();
 }
Example #2
0
        private void ContactGrid_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0)
            {
                int id = Convert.ToInt32(ContactGrid.SelectedRows[0].Cells[3].Value.ToString());
                // show contact infos;
                if (e.ColumnIndex == 0)
                {
                    ShowContact contact = new ShowContact(id);
                    contact.ShowDialog();
                }
                //Edit contact infos;

                if (e.ColumnIndex == 1)
                {
                    AddEditContact edit = new AddEditContact(this, id, "edit");
                    edit.WindowTitle.Text = edit.Text = "Edit contact information";
                    var contact = db.Contacts.Find(id);
                    edit.FirstName.Text   = contact.FirstName;
                    edit.LastName.Text    = contact.LastName;
                    edit.Address.Text     = contact.Address;
                    edit.HomePhone.Text   = contact.HomePhone;
                    edit.MobilePhone.Text = contact.MobilePhone;
                    edit.Email.Text       = contact.Email;
                    edit.AddButton.Text   = "Update Contact";
                    edit.ShowDialog();
                }
                if (e.ColumnIndex == 2)
                {
                    string f = ContactGrid.Rows[e.RowIndex].Cells[4].Value.ToString();
                    string l = ContactGrid.Rows[e.RowIndex].Cells[5].Value.ToString();

                    DialogResult result = MessageBox.Show("Do you want to delete this contact, " + f + " " + l + " ?", "Delete Contact", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
                    if (result == DialogResult.Yes)
                    {
                        var contact = db.Contacts.Find(id);
                        db.Contacts.Remove(contact);
                        db.SaveChanges();
                        ReloadGrid();
                    }
                }
            }
        }