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();
        }
Beispiel #2
0
        private async void SubstituteEditButton_Click(object sender, EventArgs e)
        {
            if (!_substituteDataGridViewService.TryGetSelectedCellIndices(out var columnIndex, out var rowIndex, true))
            {
                return;
            }

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

            var columnName = _substituteDataGridViewService.GetColumnName(columnIndex);

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

            if (!_substituteDataGridViewService.TryParseIntCell(SubstituteColumnName.MedicamentId,
                                                                rowIndex,
                                                                out var substituteMedicamentId))
            {
                return;
            }
            var updateModel = await _substitutesService.ReadByIdAsync(_medicament.Id, substituteMedicamentId);

            switch (columnName)
            {
            case SubstituteColumnName.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 RefreshSubstituteDataGridViewAsync();
        }