コード例 #1
0
        private async void BtnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                bool   activeStatus = bool.Parse(dataGridView1.Rows[selectedRowIndex].Cells[5].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
                {
                    CategoryModel categoryModel = new CategoryModel(db);
                    int           id            = int.Parse(dataGridView1.Rows[selectedRowIndex].Cells[0].Value.ToString());

                    await categoryModel.DeleteCategoryById(id, !activeStatus);

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

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