Пример #1
0
        private async void BtnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                bool   activeStatus = bool.Parse(dataGridView1.Rows[selectedRowIndex].Cells[9].Value.ToString());
                string message      = activeStatus ? "deactivate" : "active";
                var    result       = MessageBox.Show($"Are you sure you want to {message} this record?", message.ToUpper(),
                                                      MessageBoxButtons.YesNo,
                                                      MessageBoxIcon.Question);

                // If the no button was pressed ...
                if (result == DialogResult.No)
                {
                    // cancel the closure of the form.
                }
                else
                {
                    ContactModel contactModel = new ContactModel(db);
                    int          id           = int.Parse(dataGridView1.Rows[selectedRowIndex].Cells[0].Value.ToString());

                    await contactModel.ChangeContactStatus(id, !activeStatus);

                    if (activeStatus)
                    {
                        MessageBox.Show("Successfully deactivated");
                    }
                    else
                    {
                        MessageBox.Show("Successfully activated");
                    }

                    GetContacts();
                    btnEdit.Enabled   = false;
                    btnDelete.Enabled = false;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Failed to delete record");
            }
        }