private void dgv_Resources_RowValidating(object sender, DataGridViewCellCancelEventArgs e) { if (((DataGridView)sender).Rows[e.RowIndex].IsNewRow) { return; } if (!((DataGridView)sender).IsCurrentRowDirty) { return; } // Constraint: Id must be > 0 var resourceRecord = (ResourceRecord)((DataGridView)sender).Rows[e.RowIndex].DataBoundItem; if (resourceRecord.Id < 1) { MessageBox.Show("The Id must be greater than 0."); e.Cancel = true; } // Identity if (Resources.Count(c => c.Id == resourceRecord.Id) > 1) { MessageBox.Show($"There is already a Resource with Id '{resourceRecord.Id}'."); e.Cancel = true; } // Foreign Key if (ResistConfigurations.All(c => c.Id != resourceRecord.BonusResistConfigurationId)) { MessageBox.Show($"The Resist Id '{resourceRecord.BonusResistConfigurationId}' does not exist."); e.Cancel = true; return; } // Primary Key if (Resources.Count(c => c.BonusResistConfigurationId == resourceRecord.BonusResistConfigurationId) > 1) { MessageBox.Show("This configuration already exists!"); e.Cancel = true; } }
private void dgv_ArmorTypes_RowValidating(object sender, DataGridViewCellCancelEventArgs e) { if (((DataGridView)sender).Rows[e.RowIndex].IsNewRow) { return; } if (!((DataGridView)sender).IsCurrentRowDirty) { return; } var armorRecord = (ArmorRecord)((DataGridView)sender).Rows[e.RowIndex].DataBoundItem; // Constraint: Must be explicit slot if (armorRecord.Slot == SlotTypes.Unknown) { MessageBox.Show(@"The Slot cannot be 'Unknown'."); e.Cancel = true; } // Constraint: Type and Slot must be unique combinations if (ArmorTypes.Where(c => c.Type.ToLower() == armorRecord.Type.ToLower()).Any(c => c.Slot != armorRecord.Slot)) { MessageBox.Show($@"That Type '{armorRecord.Type}' already belongs to another Slot."); e.Cancel = true; } // Constraint: Type and Color must be unique combinations if (ArmorTypes.Count(c => c.Type.ToLower() == armorRecord.Type.ToLower() && c.Color == armorRecord.Color) > 1) { MessageBox.Show($@"There is already a Type '{armorRecord.Type}' and Color '{armorRecord.Color}'."); e.Cancel = true; } // Foreign Key: Resist Id must exist if (ResistConfigurations.All(c => c.Id != armorRecord.BaseResistConfigurationId)) { MessageBox.Show($@"The Resist Id '{armorRecord.BaseResistConfigurationId}' does not exist."); e.Cancel = true; } }