private void dataGridView3_CellValueChanged(object sender, DataGridViewCellEventArgs e) { pokemon = new PokemonBaseStat() { PName = (sender as DataGridView).Rows[e.RowIndex].Cells[0].Value.ToString(), Type1 = (sender as DataGridView).Rows[e.RowIndex].Cells[1].Value.ToString(), Type2 = (sender as DataGridView).Rows[e.RowIndex].Cells[2].Value.ToString() }; ValidationContext Valid = new ValidationContext(pokemon, null, null); IList <ValidationResult> errors = new List <ValidationResult>(); if (!Validator.TryValidateObject(pokemon, Valid, errors, true)) { (sender as DataGridView).Rows[e.RowIndex].ErrorText = null; EventArgs.Error = null; foreach (ValidationResult result in errors) { EventArgs.Cancel = true; EventArgs.Error += $"{result.ErrorMessage}\n"; (sender as DataGridView).Rows[e.RowIndex].ErrorText += $"{result.ErrorMessage}\n"; } } else { EventArgs.Cancel = false; (sender as DataGridView).Rows[e.RowIndex].ErrorText = null; } }
private void toolStripComboBox1_SelectedIndexChanged(object sender, EventArgs e) { WPFdataGrid.DataGridControl dataGrid = elementHost1.Child as WPFdataGrid.DataGridControl; Control.DataGrid grid = dataGrid.grid; var PName = (sender as ToolStripComboBox).Text; using (Pokemon db = new Pokemon()) { ListCollectionView view = new ListCollectionView(observable); if (!(PName == null)) { view.Filter = (p) => { PokemonBaseStat baseStat = p as PokemonBaseStat; if (baseStat.PName.ToLower().StartsWith(PName.ToLower())) { return(true); } return(false); }; grid.ItemsSource = view; grid.SelectedIndex = -1; } else { grid.ItemsSource = (from p in db.PokemonBaseStats select p).ToList(); } } }
private void addToolStripMenuItem_Click(object sender, EventArgs e) { if (EventArgs.Cancel != true) { try { using (var context = new Pokemon()) { //create Book object PokemonBaseStat pokemonToAdd = new PokemonBaseStat(); //assign its properties pokemonToAdd.PName = pokemon.PName; pokemonToAdd.HP = pokemon.HP; pokemonToAdd.Attack = pokemon.Attack; pokemonToAdd.Defense = pokemon.Defense; pokemonToAdd.SPAttack = pokemon.SPAttack; pokemonToAdd.SPDefense = pokemon.SPDefense; pokemonToAdd.Speed = pokemon.Speed; pokemonToAdd.Type1 = pokemon.Type1; pokemonToAdd.Type2 = pokemon.Type2; pokemonToAdd.PokemonCapRate = pokemon.PokemonCapRate; //add the Book to the object set Books context.PokemonBaseStats.Add(pokemonToAdd); //save change to the database context.SaveChanges(); } refreshToolStripMenuItem.PerformClick(); } catch (Exception ex) { System.Windows.Forms.MessageBox.Show($" Data: {ex.Data} \n Source: {ex.Source} \n " + $"Message: {ex.Message} \n Inner Error: {ex.InnerException} \n Target Site: {ex.TargetSite} \n Stack Trace: {ex.StackTrace} \n " + $"Result Code: {ex.HResult} \n {ex.HelpLink}"); } } }
private void dataGrid_MouseRightClick(object sender, MouseButtonEventArgs e) { WPFdataGrid.DataGridControl wPFdataGrid = elementHost1.Child as WPFdataGrid.DataGridControl; var dataGridView = wPFdataGrid.grid; DependencyObject Hit = (DependencyObject)e.OriginalSource; while (Hit == null || !(Hit is Control.DataGridRow)) { Hit = VisualTreeHelper.GetParent(Hit); } Control.DataGridRow dataRow = Hit as Control.DataGridRow; var detailGrids = FindVisualChildren <Control.DataGrid>(dataGridView); PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(dataRow.Item); Pokemon = new Pokemon(); string PName = properties["PName"].GetValue(dataRow.Item)?.ToString(); Int16 HP = 0; Int16 Attack = 0; Int16 Defense = 0; Int16 SPAttack = 0; Int16 SPDefense = 0; Int16 Speed = 0; string Type1 = null; string Type2 = null; if (properties["HP"] != null && properties["Attack"] != null && properties["Defense"] != null && properties["SPAttack"] != null && properties["SPDefense"] != null && properties["Speed"] != null) { Int16.TryParse(properties["HP"].GetValue(dataRow.Item).ToString(), out HP); Int16.TryParse(properties["Attack"].GetValue(dataRow.Item).ToString(), out Attack); Int16.TryParse(properties["Defense"].GetValue(dataRow.Item).ToString(), out Defense); Int16.TryParse(properties["SPAttack"].GetValue(dataRow.Item).ToString(), out SPAttack); Int16.TryParse(properties["SPDefense"].GetValue(dataRow.Item).ToString(), out SPDefense); Int16.TryParse(properties["Speed"].GetValue(dataRow.Item).ToString(), out Speed); Type1 = properties["Type1"].GetValue(dataRow.Item)?.ToString(); Type2 = properties["Type2"].GetValue(dataRow.Item)?.ToString(); } else { using (Pokemon db = new Pokemon()) { HP = db.PokemonBaseStats.Where(p => p.PName == PName).Single().HP; Attack = db.PokemonBaseStats.Where(p => p.PName == PName).Single().Attack; Defense = db.PokemonBaseStats.Where(p => p.PName == PName).Single().Defense; SPAttack = db.PokemonBaseStats.Where(p => p.PName == PName).Single().SPAttack; SPDefense = db.PokemonBaseStats.Where(p => p.PName == PName).Single().SPDefense; Speed = db.PokemonBaseStats.Where(p => p.PName == PName).Single().Speed; } } Int16 CapRate = 0; Int16 ExpDrop = 0; if (detailGrids.Count() > 0 && pokemonCapRates.Count > 0 && dataRow.DetailsVisibility == Visibility.Visible) { foreach (var detailGrid in detailGrids.Where(g => g.Items.CurrentItem.GetType().GetProperty("PName").GetValue(g.Items.CurrentItem).ToString() == dataRow.Item.GetType().GetProperty("PName").GetValue(dataRow.Item).ToString())) { PropertyDescriptorCollection detailProperties = TypeDescriptor.GetProperties(detailGrid.Items.CurrentItem); Int16.TryParse(detailProperties["CapRate"].GetValue(detailGrid.Items.CurrentItem).ToString(), out CapRate); Int16.TryParse(detailProperties["ExpDrop"].GetValue(detailGrid.Items.CurrentItem).ToString(), out ExpDrop); } } else { using (Pokemon db = new Pokemon()) { if (pokemonCapRates.Count > 0) { try { CapRate = pokemonCapRates.Where(p => p.PName == PName).Single().CapRate; ExpDrop = pokemonCapRates.Where(p => p.PName == PName).Single().ExpDrop; } catch { CapRate = db.PokemonBaseStats.Where(p => p.PName == PName).Single().PokemonCapRate.CapRate; ExpDrop = db.PokemonBaseStats.Where(p => p.PName == PName).Single().PokemonCapRate.ExpDrop; } } else { try { CapRate = db.PokemonBaseStats.Where(p => p.PName == PName).Single().PokemonCapRate.CapRate; ExpDrop = db.PokemonBaseStats.Where(p => p.PName == PName).Single().PokemonCapRate.ExpDrop; } catch { CapRate = 0; ExpDrop = 0; } } } } if (dataRow.GetIndex() <= dataGridView.Items.Count - 2 && pokemon != null) { PokemonBaseStat monster; try { monster = (from p in Pokemon.PokemonBaseStats where p.PName == PName select p).First(); } catch { monster = null; } if (monster == null) { contextMenuStrip.Items[0].Visible = true; contextMenuStrip.Items[1].Visible = false; contextMenuStrip.Items[2].Visible = false; contextMenuStrip.Show(System.Windows.Forms.Cursor.Position); } } if (dataRow.GetIndex() <= dataGridView.Items.Count - 2 && pokemon != null && Control.Validation.GetHasError(dataRow) == false) { if (properties["HP"] != null && properties["Attack"] != null && properties["Defense"] != null && properties["SPAttack"] != null && properties["SPDefense"] != null && properties["Speed"] != null) { PokemonBaseStat monster; try { monster = (from p in Pokemon.PokemonBaseStats where p.PName == PName select p).Single(); } catch { monster = null; return; } if (monster.PName == pokemon.PName && (monster.HP != pokemon.HP || monster.Attack != pokemon.Attack || monster.Defense != pokemon.Defense || monster.SPAttack != pokemon.SPAttack || monster.SPDefense != pokemon.SPDefense || monster.Speed != pokemon.Speed || monster.Type1 != pokemon.Type1 || monster.Type2 != pokemon.Type2 || monster.PokemonCapRate.CapRate != pokemon.PokemonCapRate.CapRate || monster.PokemonCapRate.ExpDrop != pokemon.PokemonCapRate.ExpDrop)) { contextMenuStrip.Items[0].Visible = false; contextMenuStrip.Items[1].Visible = true; contextMenuStrip.Items[2].Visible = false; contextMenuStrip.Show(System.Windows.Forms.Cursor.Position); } } } if (dataRow.GetIndex() <= dataGridView.Items.Count - 2 && Control.Validation.GetHasError(dataRow) == false) { var monster = (from p in Pokemon.PokemonBaseStats where p.PName == PName select p).First(); pokemon = new PokemonBaseStat() { PName = properties["PName"].GetValue(dataRow.Item).ToString(), HP = HP, Attack = Attack, Defense = Defense, SPAttack = SPAttack, SPDefense = SPDefense, Speed = Speed, Type1 = Type1, Type2 = Type2, PokemonCapRate = new PokemonCapRate() { PName = properties["PName"].GetValue(dataRow.Item).ToString(), CapRate = CapRate, ExpDrop = ExpDrop } }; if (Type1 == "") { Type1 = null; } if (Type2 == "") { Type2 = null; } if (monster.PName == PName && monster.HP == HP && monster.Attack == Attack && monster.Defense == Defense && monster.SPAttack == SPAttack && monster.SPDefense == SPDefense && monster.Speed == Speed && monster.Type1 == pokemon.Type1 && monster.Type2 == pokemon.Type2 && monster.PokemonCapRate.CapRate == CapRate && monster.PokemonCapRate.ExpDrop == ExpDrop) { contextMenuStrip.Items[0].Visible = false; contextMenuStrip.Items[1].Visible = false; contextMenuStrip.Items[2].Visible = true; contextMenuStrip.Show(System.Windows.Forms.Cursor.Position); } } }
private void dataGrid_CellValueChanged(object sender, Control.DataGridCellEditEndingEventArgs e) { if (e.EditAction == Control.DataGridEditAction.Commit) { WPFdataGrid.DataGridControl wPFdataGrid = elementHost1.Child as WPFdataGrid.DataGridControl; var dataGridView = wPFdataGrid.grid; Control.DataGridRow dataRow = e.Row as Control.DataGridRow; PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(dataRow.Item); DataGridDetailsPresenter presenter = FindVisualChild <DataGridDetailsPresenter>(dataRow); DataTemplate detail = presenter.ContentTemplate; Int16.TryParse(properties["HP"].GetValue(dataRow.Item).ToString(), out Int16 HP); Int16.TryParse(properties["Attack"].GetValue(dataRow.Item).ToString(), out Int16 Attack); Int16.TryParse(properties["Defense"].GetValue(dataRow.Item).ToString(), out Int16 Defense); Int16.TryParse(properties["SPAttack"].GetValue(dataRow.Item).ToString(), out Int16 SPAttack); Int16.TryParse(properties["SPDefense"].GetValue(dataRow.Item).ToString(), out Int16 SPDefense); Int16.TryParse(properties["Speed"].GetValue(dataRow.Item).ToString(), out Int16 Speed); Int16 CapRate = 0; Int16 ExpDrop = 0; try { Control.DataGrid detailGrid = detail.FindName("details", presenter) as Control.DataGrid; PropertyDescriptorCollection detailProperties = TypeDescriptor.GetProperties(detailGrid.Items.CurrentItem); Int16.TryParse(detailProperties["CapRate"]?.GetValue(detailGrid.Items?.CurrentItem)?.ToString(), out CapRate); Int16.TryParse(detailProperties["ExpDrop"]?.GetValue(detailGrid.Items?.CurrentItem)?.ToString(), out ExpDrop); } catch { using (Pokemon db = new Pokemon()) { try { CapRate = db.PokemonBaseStats.Where(p => p.PName == properties["PName"].GetValue(dataRow.Item).ToString()).Single().PokemonCapRate.CapRate; ExpDrop = db.PokemonBaseStats.Where(p => p.PName == properties["PName"].GetValue(dataRow.Item).ToString()).Single().PokemonCapRate.ExpDrop; } catch { CapRate = 0; ExpDrop = 0; } } } pokemon = new PokemonBaseStat() { PName = properties["PName"].GetValue(dataRow.Item)?.ToString(), HP = HP, Attack = Attack, Defense = Defense, SPAttack = SPAttack, SPDefense = SPDefense, Speed = Speed, Type1 = properties["Type1"].GetValue(dataRow.Item)?.ToString(), Type2 = properties["Type2"].GetValue(dataRow.Item)?.ToString(), PokemonCapRate = new PokemonCapRate() { PName = properties["PName"].GetValue(dataRow.Item)?.ToString(), CapRate = CapRate, ExpDrop = ExpDrop } }; ValidationContext validate = new ValidationContext(pokemon, null, null); IList <ValidationResult> errors = new List <ValidationResult>(); if (!Validator.TryValidateObject(pokemon, validate, errors, true)) { e.Cancel = true; EventArgs.Cancel = true; EventArgs.Error = null; Control.DataErrorValidationRule validationRule = new Control.DataErrorValidationRule(); Control.ValidationError error = new Control.ValidationError(validationRule, dataRow.BindingGroup.BindingExpressions); EventArgs.Error = $"{errors.First().ErrorMessage}"; error.ErrorContent = $"{errors.First().ErrorMessage}"; foreach (ValidationResult result in errors.Skip(1)) { EventArgs.Error += $"\n{result.ErrorMessage}"; error.ErrorContent += $"\n{result.ErrorMessage}"; } foreach (var binding in dataRow.BindingGroup.BindingExpressions) { Control.Validation.MarkInvalid(dataRow.BindingGroup.BindingExpressions.First(), error); } } else { Control.DataErrorValidationRule validationRule = new Control.DataErrorValidationRule(); Control.ValidationError error = new Control.ValidationError(validationRule, dataRow.BindingGroup.BindingExpressions); e.Cancel = false; EventArgs.Cancel = false; error.ErrorContent = null; } } }