배경 : XML 직렬화를 통해 기록한 데이터를 쿼리하는 개인용 간단한 winform 앱을 만듭니다. 이 데이터는 챔피언과 관련 레이스, 클래스 등으로 구성됩니다. 내 문제는 새 챔피언을 추가하도록 설계된 UserControl에서 발생합니다.Enums가있는 DataGridView는 BindingList의 기본값 만 설정합니다.
문제점 : DataGridViews를 사용하여 하나 이상의 Race, Class 등을 새 챔피언에 추가하고 있습니다. 이러한 각 속성은 열거 형입니다. 각 DataGridView는 하나의 열거 형에 묶여 있으며 하나의 열에는 원하는 값을 선택하기위한 ComboBox가 있습니다. [| _ChampRace ________________ _____]
[_ ... _ | _Dragon __________________ V_]
(. 미안 해요, 난 스크린 샷을했지만, 충분한 담당자가없는): 인종에 대한
예 DataGridView에
[__ * __ | ________________________ V는 _]이 DataGridViews에 대한
소스는 각 각 열거 형의 BindingLists 연결되어 있습니다. 뷰에서 값 (예 : ChampRace.Dragon)을 선택하면 BindingList는 기본 추가 값 (ChampRace.None) 만받습니다. 내가 원하는대로 바인딩 목록이 업데이트되지 않는 이유는 무엇입니까?
예 열거 형 :
public enum ChampRace
{
None,
Angel,
Beast,
Dragon,
Etc
}
직렬화 된 클래스 :
public class Champion
{
[XmlAttribute]
public string Name { get; set; }
[XmlElement]
public List<ChampRace> Races { get; set; }
[XmlElement]
public List<ChampClass> Classes { get; set; }
[XmlElement]
public List<Faction> Factions { get; set; }
}
UserControl을 코드 숨김
public partial class NewChampion : UserControl
{
public NewChampion()
{
this.InitializeComponent();
this.InitializeAllData();
}
public Master Master { get; set; }
public BindingList<ChampRace> Races { get; set; }
public BindingList<ChampClass> Classes { get; set; }
public BindingList<Faction> Factions { get; set; }
private void AddChampionButton_Click(object sender, EventArgs e)
{
Champion champion = new Champion();
champion.Name = this.nameTextBox.Text;
champion.Classes = new List<ChampClass>();
champion.Factions = new List<Faction>();
champion.Races = new List<ChampRace>();
foreach (ChampClass cc in this.Classes)
{
champion.Classes.Add(cc);
}
foreach (ChampRace cr in this.Races)
{
champion.Races.Add(cr);
}
foreach (Faction f in this.Factions)
{
champion.Factions.Add(f);
}
System.Diagnostics.Debug.WriteLine("Break point here during debug to verify data...");
// Note: At this point based on the screenshot, this.Races has one value.
// Instead of the expected value "Dragon", the value is always "None".
}
private void InitializeAllData()
{
this.Races = new BindingList<ChampRace>();
this.Classes = new BindingList<ChampClass>();
this.Factions = new BindingList<Faction>();
/*
* The following event handlers were added to show a new editable row in the dgView.
* Without them, no rows appear.
*/
this.Classes.AddingNew += (s, e) =>
{
// e.NewObject = ChampClass.None;
};
this.Races.AddingNew += (s, e) =>
{
// e.NewObject = ChampRace.None;
};
this.Factions.AddingNew += (s, e) =>
{
// e.NewObject = Faction.None;
};
this.raceData.DataSource = this.Races;
this.classData.DataSource = this.Classes;
this.factionData.DataSource = this.Factions;
this.SetupDataOptions<ChampRace>(ref this.raceData);
this.SetupDataOptions<ChampClass>(ref this.classData);
this.SetupDataOptions<Faction>(ref this.factionData);
}
private void SetupDataOptions<T>(ref DataGridView data)
where T : struct, IComparable, IFormattable, IConvertible
{
if (!typeof(T).IsEnum)
{
throw new ArgumentException("Type must be an enumeration");
}
DataGridViewComboBoxColumn combo = new DataGridViewComboBoxColumn();
combo.ValueType = typeof(T);
combo.DataSource = Enum.GetValues(typeof(T));
combo.DataPropertyName = typeof(T).ToString();
combo.Name = typeof(T).ToString().Split('.').Last();
data.AutoGenerateColumns = false;
data.AutoSize = true;
data.Columns.Add(combo);
}
}
이 내가 DataGridViews 및 BindingLists를 처음 사용하는, 그래서 간단히 간과 한 실수가되기를 바란다.
날 여기있어 내가 발견 한 가장 관련성 연구 : 제가 테스트 프로그램의 코드를 다시 발견
- Combobox doesn’t set default value if value is an enum
- Making a DataGridView column editable after binding it to a BindingList
- DataGridView doesn't update Enum in BindingList
- DataGridView Default Error on ComboBox Column