Fix improper datasource binding refreshes

Switch to a BindingList and use extension methods to have Sort and
AddRange.
This commit is contained in:
2013-12-06 11:07:40 -05:00
parent e59521035a
commit 3831a7c5ce
7 changed files with 38 additions and 31 deletions

View File

@@ -39,7 +39,7 @@
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
<Reference Include="xunit">
<HintPath>G:\xunit.net\xunit.dll</HintPath>
<HintPath>..\d20 SRD Spell Lists\lib\xunit.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>

View File

@@ -0,0 +1,30 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace System.ComponentModel
{
public static class BindingListExtensions
{
public static void AddRange<T>(this BindingList<T> collection, List<T> elements) {
foreach (var e in elements)
{
collection.Add(e);
}
}
public static void ReloadFromList<T>(this BindingList<T> collection, List<T> list)
{
collection.Clear();
collection.AddRange(list);
}
public static void Sort<T>(this BindingList<T> collection, IComparer<T> comparer)
{
List<T> list = collection.ToList();
list.Sort(comparer);
collection.ReloadFromList(list);
}
}
}

View File

@@ -97,7 +97,6 @@ namespace d20_SRD_Spell_Lists {
this.printDoc = new System.Drawing.Printing.PrintDocument();
this.dataGridViewImageColumn1 = new System.Windows.Forms.DataGridViewImageColumn();
this.dataGridViewImageColumn2 = new System.Windows.Forms.DataGridViewImageColumn();
this.mainAppUpdater = new Microsoft.Samples.AppUpdater.AppUpdater(this.components);
this.tableLayoutPanel1.SuspendLayout();
this.groupBox1.SuspendLayout();
this.mainToolStrip.SuspendLayout();
@@ -106,7 +105,6 @@ namespace d20_SRD_Spell_Lists {
this.splitContainer1.Panel1.SuspendLayout();
this.splitContainer1.Panel2.SuspendLayout();
this.splitContainer1.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.mainAppUpdater)).BeginInit();
this.SuspendLayout();
//
// tableLayoutPanel1
@@ -871,13 +869,6 @@ namespace d20_SRD_Spell_Lists {
this.dataGridViewImageColumn2.ReadOnly = true;
this.dataGridViewImageColumn2.Resizable = System.Windows.Forms.DataGridViewTriState.True;
//
// mainAppUpdater
//
this.mainAppUpdater.AutoFileLoad = true;
this.mainAppUpdater.ChangeDetectionMode = Microsoft.Samples.AppUpdater.ChangeDetectionModes.ServerManifestCheck;
this.mainAppUpdater.ShowDefaultUI = true;
this.mainAppUpdater.UpdateUrl = "http://downloads.thecharonsheet.com/d20-srd-spell-lists/manifest.xml";
//
// FrmMain
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
@@ -900,7 +891,6 @@ namespace d20_SRD_Spell_Lists {
this.splitContainer1.Panel2.ResumeLayout(false);
((System.ComponentModel.ISupportInitialize)(this.splitContainer1)).EndInit();
this.splitContainer1.ResumeLayout(false);
((System.ComponentModel.ISupportInitialize)(this.mainAppUpdater)).EndInit();
this.ResumeLayout(false);
this.PerformLayout();
@@ -977,7 +967,6 @@ namespace d20_SRD_Spell_Lists {
private System.Windows.Forms.DataGridViewTextBoxColumn descColumn;
private System.Windows.Forms.DataGridViewImageColumn editColumn;
private System.Windows.Forms.DataGridViewImageColumn deleteColumn;
private Microsoft.Samples.AppUpdater.AppUpdater mainAppUpdater;
}
}

View File

@@ -12,6 +12,7 @@ using System.Xml.Serialization;
using System.IO;
using Printing.DataGridViewPrint.Tools;
using System.Reflection;
using System.Collections.ObjectModel;
namespace d20_SRD_Spell_Lists {
public partial class FrmMain : Form {
@@ -102,15 +103,9 @@ namespace d20_SRD_Spell_Lists {
var result = MessageBox.Show(question, title, MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1);
if (result == System.Windows.Forms.DialogResult.Yes) {
character.addAllClassSpells();
refreshSpellList();
}
}
private void refreshSpellList() {
spellsDataGridView.DataSource = null;
setupSpells();
}
private void updateClassInformation(ComboBox classList) {
string charClass = classList.SelectedItem.ToString();
character.CharacterClass = (Character.SpellCastingClasses)Enum.Parse(typeof(Character.SpellCastingClasses), charClass);
@@ -235,7 +230,6 @@ namespace d20_SRD_Spell_Lists {
txtCharacter.Text = character.Name;
txtSpellCastingAttribute.Text = character.SpellCastingAttribute.ToString();
charClassComboBox.SelectedItem = Character.getSpellcastingClassName(character.CharacterClass);
refreshSpellList();
}
private void btnAdd_Click(object sender, EventArgs e) {
@@ -244,7 +238,6 @@ namespace d20_SRD_Spell_Lists {
if (result == System.Windows.Forms.DialogResult.OK) {
character.Spells.Add(addForm.spell);
character.Spells.Sort(new SpellInequalityComparer());
refreshSpellList();
dirtyCharacter = true;
}
}
@@ -256,7 +249,6 @@ namespace d20_SRD_Spell_Lists {
var result = editForm.ShowDialog();
if (result == System.Windows.Forms.DialogResult.OK) {
character.Spells[character.Spells.IndexOf(editItem)] = editForm.spell;
refreshSpellList();
dirtyCharacter = true;
}
} else if (isDeleteButtonCell(e)) {
@@ -266,7 +258,6 @@ namespace d20_SRD_Spell_Lists {
var result = MessageBox.Show(msg, title, MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
if (result == System.Windows.Forms.DialogResult.Yes) {
character.Spells.Remove(deleteItem);
refreshSpellList();
dirtyCharacter = true;
}
}

View File

@@ -6,14 +6,16 @@ using System.Xml;
using System.Xml.Linq;
using d20_SRD_Spell_Lists.Exceptions;
using System.Xml.Serialization;
using System.Collections.ObjectModel;
using System.ComponentModel;
namespace d20_SRD_Spell_Lists.Models {
public class Character {
public List<Spell> Spells { get; set; }
public BindingList<Spell> Spells { get; set; }
public Character() {
Spells = new List<Spell>();
Spells = new BindingList<Spell>();
}
private int modifier(int score) {

View File

@@ -133,7 +133,7 @@ namespace d20_SRD_Spell_Lists.Models {
string c = Character.getSpellcastingClassName(characterClass);
Regex levelReg = new Regex(@" (\d+),?");
return (from sp in masterSpellList.Elements("spell")
let xmlLevel = (string)sp.Element("level")
let xmlLevel = (string)sp.Element("level") ?? "0"
orderby (string)sp.Element("name")
where (string)sp.Element("name") == name
select new Spell {

View File

@@ -51,9 +51,6 @@
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
</PropertyGroup>
<ItemGroup>
<Reference Include="AppUpdater">
<HintPath>G:\dotnetupdater\AppUpdater\bin\Release\AppUpdater.dll</HintPath>
</Reference>
<Reference Include="Microsoft.ReportViewer.Common, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL" />
<Reference Include="Microsoft.ReportViewer.WinForms, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL" />
<Reference Include="System" />
@@ -68,11 +65,9 @@
<Reference Include="System.Drawing" />
<Reference Include="System.Windows.Forms" />
<Reference Include="System.Xml" />
<Reference Include="xunit">
<HintPath>G:\xunit.net\xunit.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="Extensions\BindingListExtensions.cs" />
<Compile Include="FrmCredits.cs">
<SubType>Form</SubType>
</Compile>