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.Data" />
<Reference Include="System.Xml" /> <Reference Include="System.Xml" />
<Reference Include="xunit"> <Reference Include="xunit">
<HintPath>G:\xunit.net\xunit.dll</HintPath> <HintPath>..\d20 SRD Spell Lists\lib\xunit.dll</HintPath>
</Reference> </Reference>
</ItemGroup> </ItemGroup>
<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.printDoc = new System.Drawing.Printing.PrintDocument();
this.dataGridViewImageColumn1 = new System.Windows.Forms.DataGridViewImageColumn(); this.dataGridViewImageColumn1 = new System.Windows.Forms.DataGridViewImageColumn();
this.dataGridViewImageColumn2 = 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.tableLayoutPanel1.SuspendLayout();
this.groupBox1.SuspendLayout(); this.groupBox1.SuspendLayout();
this.mainToolStrip.SuspendLayout(); this.mainToolStrip.SuspendLayout();
@@ -106,7 +105,6 @@ namespace d20_SRD_Spell_Lists {
this.splitContainer1.Panel1.SuspendLayout(); this.splitContainer1.Panel1.SuspendLayout();
this.splitContainer1.Panel2.SuspendLayout(); this.splitContainer1.Panel2.SuspendLayout();
this.splitContainer1.SuspendLayout(); this.splitContainer1.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.mainAppUpdater)).BeginInit();
this.SuspendLayout(); this.SuspendLayout();
// //
// tableLayoutPanel1 // tableLayoutPanel1
@@ -871,13 +869,6 @@ namespace d20_SRD_Spell_Lists {
this.dataGridViewImageColumn2.ReadOnly = true; this.dataGridViewImageColumn2.ReadOnly = true;
this.dataGridViewImageColumn2.Resizable = System.Windows.Forms.DataGridViewTriState.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 // FrmMain
// //
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
@@ -900,7 +891,6 @@ namespace d20_SRD_Spell_Lists {
this.splitContainer1.Panel2.ResumeLayout(false); this.splitContainer1.Panel2.ResumeLayout(false);
((System.ComponentModel.ISupportInitialize)(this.splitContainer1)).EndInit(); ((System.ComponentModel.ISupportInitialize)(this.splitContainer1)).EndInit();
this.splitContainer1.ResumeLayout(false); this.splitContainer1.ResumeLayout(false);
((System.ComponentModel.ISupportInitialize)(this.mainAppUpdater)).EndInit();
this.ResumeLayout(false); this.ResumeLayout(false);
this.PerformLayout(); this.PerformLayout();
@@ -977,7 +967,6 @@ namespace d20_SRD_Spell_Lists {
private System.Windows.Forms.DataGridViewTextBoxColumn descColumn; private System.Windows.Forms.DataGridViewTextBoxColumn descColumn;
private System.Windows.Forms.DataGridViewImageColumn editColumn; private System.Windows.Forms.DataGridViewImageColumn editColumn;
private System.Windows.Forms.DataGridViewImageColumn deleteColumn; 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 System.IO;
using Printing.DataGridViewPrint.Tools; using Printing.DataGridViewPrint.Tools;
using System.Reflection; using System.Reflection;
using System.Collections.ObjectModel;
namespace d20_SRD_Spell_Lists { namespace d20_SRD_Spell_Lists {
public partial class FrmMain : Form { 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); var result = MessageBox.Show(question, title, MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1);
if (result == System.Windows.Forms.DialogResult.Yes) { if (result == System.Windows.Forms.DialogResult.Yes) {
character.addAllClassSpells(); character.addAllClassSpells();
refreshSpellList();
} }
} }
private void refreshSpellList() {
spellsDataGridView.DataSource = null;
setupSpells();
}
private void updateClassInformation(ComboBox classList) { private void updateClassInformation(ComboBox classList) {
string charClass = classList.SelectedItem.ToString(); string charClass = classList.SelectedItem.ToString();
character.CharacterClass = (Character.SpellCastingClasses)Enum.Parse(typeof(Character.SpellCastingClasses), charClass); character.CharacterClass = (Character.SpellCastingClasses)Enum.Parse(typeof(Character.SpellCastingClasses), charClass);
@@ -235,7 +230,6 @@ namespace d20_SRD_Spell_Lists {
txtCharacter.Text = character.Name; txtCharacter.Text = character.Name;
txtSpellCastingAttribute.Text = character.SpellCastingAttribute.ToString(); txtSpellCastingAttribute.Text = character.SpellCastingAttribute.ToString();
charClassComboBox.SelectedItem = Character.getSpellcastingClassName(character.CharacterClass); charClassComboBox.SelectedItem = Character.getSpellcastingClassName(character.CharacterClass);
refreshSpellList();
} }
private void btnAdd_Click(object sender, EventArgs e) { private void btnAdd_Click(object sender, EventArgs e) {
@@ -244,7 +238,6 @@ namespace d20_SRD_Spell_Lists {
if (result == System.Windows.Forms.DialogResult.OK) { if (result == System.Windows.Forms.DialogResult.OK) {
character.Spells.Add(addForm.spell); character.Spells.Add(addForm.spell);
character.Spells.Sort(new SpellInequalityComparer()); character.Spells.Sort(new SpellInequalityComparer());
refreshSpellList();
dirtyCharacter = true; dirtyCharacter = true;
} }
} }
@@ -256,7 +249,6 @@ namespace d20_SRD_Spell_Lists {
var result = editForm.ShowDialog(); var result = editForm.ShowDialog();
if (result == System.Windows.Forms.DialogResult.OK) { if (result == System.Windows.Forms.DialogResult.OK) {
character.Spells[character.Spells.IndexOf(editItem)] = editForm.spell; character.Spells[character.Spells.IndexOf(editItem)] = editForm.spell;
refreshSpellList();
dirtyCharacter = true; dirtyCharacter = true;
} }
} else if (isDeleteButtonCell(e)) { } else if (isDeleteButtonCell(e)) {
@@ -266,7 +258,6 @@ namespace d20_SRD_Spell_Lists {
var result = MessageBox.Show(msg, title, MessageBoxButtons.YesNo, MessageBoxIcon.Warning); var result = MessageBox.Show(msg, title, MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
if (result == System.Windows.Forms.DialogResult.Yes) { if (result == System.Windows.Forms.DialogResult.Yes) {
character.Spells.Remove(deleteItem); character.Spells.Remove(deleteItem);
refreshSpellList();
dirtyCharacter = true; dirtyCharacter = true;
} }
} }

View File

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

View File

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

View File

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