private async void EditButton_Click(object sender, EventArgs e)
        {
            if (!_dataGridViewService.TryGetSelectedCellIndices(out var columnIndex, out var rowIndex, true))
            {
                return;
            }

            if (columnIndex == -1)
            {
                MessageBoxService.ShowIncorrectSelectionWarning("Select cell you want to edit.");
                return;
            }

            var columnName = _dataGridViewService.GetColumnName(columnIndex);

            if (columnName == ColumnName.Id)
            {
                MessageBoxService.ShowIncorrectSelectionWarning("You can't edit id.");
                return;
            }

            if (!_dataGridViewService.TryParseIntCell(ColumnName.Id, rowIndex, out var medicamentId))
            {
                return;
            }
            var medicament = await _medicamentsService.ReadByIdAsync(medicamentId);

            switch (columnName)
            {
            case ColumnName.Name:
            {
                var oldValue = _dataGridViewService.GetCellValue(columnIndex, rowIndex);
                var editForm = new EditStringDialogForm(oldValue);
                editForm.ShowDialog(this);

                var editResult = editForm.DialogResult;
                var newValue   = editForm.NewValue;
                editForm.Close();

                if (editResult == DialogResult.Cancel)
                {
                    return;
                }

                medicament.Name = newValue;
                break;
            }

            case ColumnName.StockQuantity:
            {
                if (!_dataGridViewService.TryParseDoubleCell(columnName, rowIndex, out var oldValue))
                {
                    return;
                }
                var editForm = new EditDoubleDialogForm(oldValue);
                editForm.ShowDialog(this);

                var editResult = editForm.DialogResult;
                var newValue   = editForm.NewValue;
                editForm.Close();

                if (editResult == DialogResult.Cancel)
                {
                    return;
                }

                medicament.StockQuantity = newValue;
                break;
            }

            default:
                MessageBox.Show(@"Check database and debug code.",
                                @"Unknown field.",
                                MessageBoxButtons.OK,
                                MessageBoxIcon.Error);
                return;
            }

            await _medicamentsService.UpdateAsync(medicament);

            await RefreshDataViewGridAsync();
        }
Exemple #2
0
        private async void OriginalEditButton_Click(object sender, EventArgs e)
        {
            if (!_originalDataGridViewService.TryGetSelectedCellIndices(out var columnIndex, out var rowIndex, true))
            {
                return;
            }

            if (columnIndex == -1)
            {
                MessageBoxService.ShowIncorrectSelectionWarning("Select cell you want to edit.");
                return;
            }

            var columnName = _originalDataGridViewService.GetColumnName(columnIndex);

            if (columnName == OriginalColumnName.MedicamentId || columnName == OriginalColumnName.MedicamentName)
            {
                MessageBoxService.ShowIncorrectSelectionWarning("You can't edit this field.");
                return;
            }

            if (!_originalDataGridViewService.TryParseIntCell(OriginalColumnName.MedicamentId,
                                                              rowIndex,
                                                              out var originalMedicamentId))
            {
                return;
            }
            var updateModel = await _substitutesService.ReadByIdAsync(originalMedicamentId, _medicament.Id);

            switch (columnName)
            {
            case OriginalColumnName.DosageMultiplier:
            {
                if (!_substituteDataGridViewService.TryParseDoubleCell(columnName, rowIndex, out var oldValue))
                {
                    return;
                }
                var editForm = new EditDoubleDialogForm(oldValue);
                editForm.ShowDialog(this);

                var editResult = editForm.DialogResult;
                var newValue   = (float)editForm.NewValue;
                editForm.Close();

                if (editResult == DialogResult.Cancel)
                {
                    return;
                }

                updateModel.DosageMultiplier = newValue;
                break;
            }

            default:
                MessageBox.Show(@"Check database and debug code.",
                                @"Unknown field.",
                                MessageBoxButtons.OK,
                                MessageBoxIcon.Error);
                return;
            }

            await _substitutesService.UpdateByIdAsync(updateModel);

            await RefreshOriginalDataGridViewAsync();
        }