private DuplicateWord WordPairAlreadyExits(IEnumerable <WordPairForDataGridView> wordsListExcludingCurrentlyEditingWord, string firstLanguageWord, string secondLanguageWord) { var duplicateWord = new DuplicateWord { WordAlreadyExist = false }; foreach (var word in wordsListExcludingCurrentlyEditingWord) { bool firstLanguageWordAlreadyExist = _wordsService.CheckIfWordsMatches(word.FirstLanguageWord, firstLanguageWord); bool secondLanguageWordAlreadyExist = _wordsService.CheckIfWordsMatches(word.SecondLanguageWord, secondLanguageWord); if (firstLanguageWordAlreadyExist || secondLanguageWordAlreadyExist) { duplicateWord.WordAlreadyExist = true; duplicateWord.DuplicateWordValue = firstLanguageWordAlreadyExist ? firstLanguageWord : secondLanguageWord; break; } } return(duplicateWord); }
private void AllWordsDataGridView_CellValidating(object sender, DataGridViewCellValidatingEventArgs e) { if (AllWordsDataGridView.IsCurrentRowDirty) { DataGridViewRow editingRow = AllWordsDataGridView.Rows[e.RowIndex]; DataGridViewCell firstLanguageWordCell = editingRow.Cells[_firstLanguageWordCellIndex]; DataGridViewCell secondLanguageWordCell = editingRow.Cells[_secondLanguageWordCellIndex]; string initialFirstLanguageWord = firstLanguageWordCell.EditedFormattedValue.ToString(); string initialSecondLanguageWord = secondLanguageWordCell.EditedFormattedValue.ToString(); var dirtyRowUuidValue = editingRow.Cells[_dirtyRowUuidCellIndex].EditedFormattedValue; if (string.IsNullOrWhiteSpace(dirtyRowUuidValue.ToString()) || Guid.Empty.ToString() == dirtyRowUuidValue.ToString()) { editingRow.Cells[_dirtyRowUuidCellIndex].Value = Guid.NewGuid(); } int wordPairId = Int32.Parse(editingRow.Cells[_wordPairIdCellIndex].EditedFormattedValue.ToString()); Guid dirtyRowIdentifier = Guid.Parse(editingRow.Cells[_dirtyRowUuidCellIndex].EditedFormattedValue.ToString()); var currentWords = (IEnumerable <WordPairForDataGridView>)_allWordsBindingSource.DataSource; if (string.IsNullOrEmpty(firstLanguageWordCell.EditedFormattedValue.ToString()) || firstLanguageWordCell.EditedFormattedValue.ToString() != initialFirstLanguageWord && !string.IsNullOrEmpty(initialFirstLanguageWord)) { // somehow cell value is wiped after i set _dirtyRowUuidCell value in few lines above // so restoring to initial entered value firstLanguageWordCell.Value = initialFirstLanguageWord; } if (string.IsNullOrEmpty(secondLanguageWordCell.EditedFormattedValue.ToString()) || secondLanguageWordCell.EditedFormattedValue.ToString() != initialSecondLanguageWord && !string.IsNullOrEmpty(initialSecondLanguageWord)) { // somehow cell value is wiped after i set _dirtyRowUuidCell value in few lines above // so restoring to initial entered value secondLanguageWordCell.Value = initialSecondLanguageWord; } string firstLanguageWord = firstLanguageWordCell.EditedFormattedValue.ToString(); string secondLanguageWord = secondLanguageWordCell.EditedFormattedValue.ToString(); DuplicateWord duplicateWord = CheckIfWordAlreadyExists(wordPairId, currentWords, firstLanguageWord, secondLanguageWord, dirtyRowIdentifier); if (duplicateWord.WordAlreadyExist) { AllWordsDataGridView.Rows[e.RowIndex].ErrorText = $"Žodis '{duplicateWord.DuplicateWordValue}' jau egzistuoja!"; e.Cancel = true; } } }