private void Insert_Button_Click(object sender, EventArgs e)
        {
            VoucherCover selected = VoucherCover_LookupEdit.GetSelectedDataRow().CastTo <VoucherCover>();

            if (selected == null)
            {
                MessageBoxHelper.ShowErrorMessage(BSMessage.BSM000032);
                return;
            }

            VoucherCoverDetail detailSelected = Detail_GridView.GetFocusedRow().CastTo <VoucherCoverDetail>();

            if (detailSelected == null)
            {
                MessageBoxHelper.ShowErrorMessage(BSMessage.BSM000032);
                return;
            }

            int index = VoucherCoverDetailData.FindIndex(o => o.VoucherCoverID == selected.VoucherCoverID && o.SEQ == detailSelected.SEQ);

            if (index < 0)
            {
                index = 0;
            }

            VoucherCoverDetailData.Insert(index, new VoucherCoverDetail
            {
                VoucherCoverID = selected.VoucherCoverID,
                Status         = ModifyMode.Insert
            });

            SetSEQ();
            Detail_GridView.RefreshData();
            Detail_GridView.FocusedRowHandle = index;
        }
        public bool DeleteVoucherCoverDetail(VoucherCoverDetail data)
        {
            SqlParameter[] sqlParameters = new SqlParameter[]
            {
                new SqlParameter("@VoucherCoverDetailID", data.VoucherCoverDetailID)
            };

            this.Context.ExecuteDataFromProcedure("VoucherCoverDetailDelete", sqlParameters);

            return(true);
        }
        private void Detail_GridView_RowDeleted(object sender, DevExpress.Data.RowDeletedEventArgs e)
        {
            VoucherCoverDetail row = e.Row.CastTo <VoucherCoverDetail>();

            if (row.Status == ModifyMode.Insert)
            {
                return;
            }

            row.Status = ModifyMode.Delete;
            VoucherCoverDetailDeleteData.Add(row);
        }
        public bool UpdateVoucherCoverDetail(VoucherCoverDetail data)
        {
            SqlParameter[] sqlParameters = new SqlParameter[]
            {
                new SqlParameter("@VoucherCoverDetailID", data.VoucherCoverDetailID),
                new SqlParameter("@VoucherCoverDetailName", data.VoucherCoverDetailName),
                new SqlParameter("@SEQ", data.SEQ),
                new SqlParameter("@VoucherCoverID", data.VoucherCoverID),
                new SqlParameter("@CompanyID", CommonInfo.CompanyInfo.CompanyID),
                new SqlParameter("@UpdateUser", UserInfo.UserID)
            };

            this.Context.ExecuteDataFromProcedure("VoucherCoverDetailUpdate", sqlParameters);

            return(true);
        }
        private void Detail_GridView_RowUpdated(object sender, DevExpress.XtraGrid.Views.Base.RowObjectEventArgs e)
        {
            VoucherCoverDetail row = e.Row.CastTo <VoucherCoverDetail>();
            bool isNewRow          = Detail_GridView.IsNewItemRow(e.RowHandle);

            if (isNewRow)
            {
                row.Status = ModifyMode.Insert;
                return;
            }

            if (row.Status == ModifyMode.Insert)
            {
                return;
            }

            row.Status = ModifyMode.Update;
        }
        private void Detail_GridView_ValidateRow(object sender, DevExpress.XtraGrid.Views.Base.ValidateRowEventArgs e)
        {
            Detail_GridView.ClearColumnErrors();

            VoucherCoverDetail row  = e.Row.CastTo <VoucherCoverDetail>();
            GridView           view = sender as GridView;
            GridColumn         column;

            if ((row.SEQ ?? 0) <= 0)
            {
                e.Valid = false;
                column  = view.Columns[nameof(row.SEQ)];
                view.SetColumnError(column, string.Format(BSMessage.BSM000034, "STT"));
                return;
            }

            // Kiểm tra tồn tại trong grid
            if (VoucherCoverDetailData.ToList().Count(o => o.VoucherCoverID == row.VoucherCoverID && o.SEQ == row.SEQ) > 1)
            {
                e.Valid = false;
                column  = view.Columns[nameof(row.SEQ)];
                view.SetColumnError(column, string.Format(BSMessage.BSM000033, "STT"));
            }
        }