コード例 #1
0
        private void btnAddNew_Click(object sender, EventArgs e)
        {
            DepartmentForm addForm = new DepartmentForm();

            addForm.ShowDialog();
            LoadAllDepartments();
        }
コード例 #2
0
        private void grdData_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                var    d  = (DataGridView)sender;
                string df = d.SelectedCells[0].Value.ToString();
                //edit column
                if (df == "Edit")
                {
                    int id   = int.Parse(grdData.Rows[e.RowIndex].Cells["id"].Value.ToString());
                    var dept = _deptRepo.GetDepartment(id);
                    if (dept != null)
                    {
                        DepartmentForm updateForm = new DepartmentForm(dept);
                        updateForm.ShowDialog();
                        //grdData.Refresh();
                        LoadAllDepartments();
                    }
                }

                //delete column
                if (df == "Delete")      //(e.ColumnIndex == 1)
                {
                    DialogResult result1 = MessageBox.Show("Are you sure you want to delete this record?",
                                                           "Confirm Delete",
                                                           MessageBoxButtons.YesNo);
                    if (result1 == DialogResult.Yes)
                    {
                        int    id       = int.Parse(grdData.Rows[e.RowIndex].Cells["id"].Value.ToString());
                        string response = _deptRepo.DeleteDepartment(id);
                        if (response == string.Empty)
                        {
                            MessageBox.Show(this, "Department deleted successfully", "Success", MessageBoxButtons.OK,
                                            MessageBoxIcon.Information);
                        }
                        else
                        {
                            MessageBox.Show(this, response, "Failed", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        }

                        LoadAllDepartments();
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(this, ex.Message, "Error occured", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }