private void Save_Data(bool msg)
        {
            try
            {
                _KTNHOMHHRepo = new KTNHOMHHRepo();
                int i = 0;
                foreach (int pos in _listUpdate)
                {
                    int id = Utils.CIntDef(gridView1.GetRowCellValue(pos, "ID"), 0);
                    KT_NHOMHH obj = _KTNHOMHHRepo.GetById(id);
                    if (obj != null)
                    {
                        obj.MA_NHOM = Utils.CStrDef(gridView1.GetRowCellValue(pos, "MA_NHOM"), "");
                        obj.TEN_NHOM = Utils.CStrDef(gridView1.GetRowCellValue(pos, "TEN_NHOM"), "");

                        _KTNHOMHHRepo.Update(obj);
                        i++;
                    }
                }
                _listUpdate = new List<int>();
                if (i > 0 && msg)
                {
                    //MessageBox.Show("Lưu thành công!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
 private void LoadDMNhom()
 {
     _KTNHOMHHRepo = new KTNHOMHHRepo();
     cboMaNhom.DataSource = _KTNHOMHHRepo.GetAll();
     cboMaNhom.DisplayMember = "MA_NHOM";
     cboMaNhom.ValueMember = "ID";
     cboMaNhom.DropDownColumns = "MA_NHOM,TEN_NHOM";
     cboMaNhom.SelectedIndex = -1;
 }
 private void Load_Data()
 {
     try
     {
         _KTNHOMHHRepo = new KTNHOMHHRepo();
         gridData.DataSource = _KTNHOMHHRepo.GetAll();
         //_db = new dbVstoreAppDataContext(Const.builder.ConnectionString);
         //gridData.DataSource = null;
         //gridData.DataSource = _db.KT_NHOMHHs;
         //gridData.RefreshDataSource();
         //gridView1.RefreshData();
     }
     catch (Exception) { }
 }
 private void cboMaNhom_SelectedIndexChanged(object sender, EventArgs e)
 {
     if (cboMaNhom.Text != "" && cboMaNhom.Text != null)
     {
         _KTNHOMHHRepo = new KTNHOMHHRepo();
         var item = _KTNHOMHHRepo.GetById(Utils.CIntDef(cboMaNhom.SelectedValue, 0));
         if (item != null)
             txtTenNhom.Text = item.TEN_NHOM;
     }
 }
        private void Save_Duplicate()
        {
            try
            {
                if (MessageBox.Show("Bạn có muốn copy dòng này thành dòng mới?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    _KTNHOMHHRepo = new KTNHOMHHRepo();
                    KT_NHOMHH obj = new KT_NHOMHH();
                    obj.MA_NHOM = Utils.CStrDef(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "MA_NHOM"), "");
                    obj.TEN_NHOM = Utils.CStrDef(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "TEN_NHOM"), "");

                    _KTNHOMHHRepo.Create(obj);
                    MessageBox.Show("Đã copy dòng này vào cuối bảng!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        private void gridView1_ValidateRow(object sender, DevExpress.XtraGrid.Views.Base.ValidateRowEventArgs e)
        {
            try
            {
            GridView view = sender as GridView;
                _KTNHOMHHRepo = new KTNHOMHHRepo();
            //Kiểm tra đây là dòng dữ liệu mới hay cũ, nếu là mới thì mình insert
            if (view.IsNewItemRow(e.RowHandle))
            {
                //e.RowHandle trả về giá trị int là thứ tự của dòng hiện tại
                KT_NHOMHH obj = new KT_NHOMHH();
                obj.MA_NHOM = Utils.CStrDef(view.GetRowCellValue(e.RowHandle, "MA_NHOM"), "");
                obj.TEN_NHOM = Utils.CStrDef(view.GetRowCellValue(e.RowHandle, "TEN_NHOM"), "");

                _KTNHOMHHRepo.Create(obj);

            }
            //Cũ thì update
            else
            {
                int id = Utils.CIntDef(view.GetRowCellValue(e.RowHandle, "ID"), 0);
                KT_NHOMHH obj = _KTNHOMHHRepo.GetById(id);
                if (obj != null)
                {
                    obj.MA_NHOM = Utils.CStrDef(view.GetRowCellValue(e.RowHandle, "MA_NHOM"), "");
                    obj.TEN_NHOM = Utils.CStrDef(view.GetRowCellValue(e.RowHandle, "TEN_NHOM"), "");

                    _KTNHOMHHRepo.Update(obj);
                }

            }
            Load_Data();
            }
            catch (Exception ex)
            {
                e.Valid = false;
                MessageBox.Show(ex.Message, "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
 private void Remove_Data()
 {
     try
     {
         //if (_listUpdate.Count > 0)
         //{
         //    MessageBox.Show("Hãy thực hiện lưu trước khi xóa!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
         //    return;
         //}
         Save_Data(false);
         _KTNHOMHHRepo = new KTNHOMHHRepo();
         int Id = Utils.CIntDef(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "ID"), 0);
         _KTNHOMHHRepo.Remove(Id);
         //MessageBox.Show("Xóa dòng ID:" + Id + " thành công!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
         Load_Data();
     }
     catch (Exception ex)
     {
         MessageBox.Show(ex.Message, "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
     }
 }