コード例 #1
0
ファイル: frmCategory.cs プロジェクト: luonganhtu/Mobile
        private void btnLuu_Click(object sender, EventArgs e)
        {
            var  index = dgvListCategory.CurrentCell.RowIndex;
            bool trung = false;

            if (txtName.Text == "")
            {
                MessageBox.Show("Tên Danh Mục Không Được Trống!", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            #region Lưu khi Thêm
            else if (g == 1)
            {
                CategoryDTO category = new CategoryDTO();
                category.Name = txtName.Text.ToString().Trim();
                trung         = _CategoryBusiness.ExisCattName(txtName.Text.ToString(), -1);
                if (trung == false)
                {
                    MessageBox.Show("Trùng Tên Danh Mục! Vui Lòng Chọn Tên Khác", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
                else
                {
                    if (_CategoryBusiness.AddCategory(category))
                    {
                        MessageBox.Show("Thêm mới thành công!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        LoadData();
                    }
                    else
                    {
                        MessageBox.Show("Thêm mới không thành công!", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }
                }
            }
            #endregion

            #region Lưu khi sửa
            else if (g == 2)
            {
                CategoryDTO category = ListCategory.ElementAtOrDefault(index);
                category.Name = txtName.Text.ToString().Trim();
                //category.Id = int.Parse(dgvListCategory.Rows[index].Cells[0].Value.ToString());
                trung = _CategoryBusiness.ExisCattName(txtName.Text.ToString(), int.Parse(dgvListCategory.Rows[index].Cells[0].Value.ToString()));
                if (trung == false)
                {
                    MessageBox.Show("Trùng Tên Danh Mục! Vui Lòng Chọn Tên Khác", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
                else
                {
                    if (_CategoryBusiness.EditCategory(category))
                    {
                        MessageBox.Show("Sửa thành công!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        LoadData();
                    }
                    else
                    {
                        MessageBox.Show("Sửa không thành công!", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }
                }
            }
            #endregion

            DieuKhien((int)DIEUKHIEN.MO);
            g = 0;
        }