2014-11-29 11 views
0

배경 : 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를 처음 사용하는, 그래서 간단히 간과 한 실수가되기를 바란다.

날 여기있어 내가 발견 한 가장 관련성 연구 : 제가 테스트 프로그램의 코드를 다시 발견

  1. Combobox doesn’t set default value if value is an enum
  2. Making a DataGridView column editable after binding it to a BindingList
  3. DataGridView doesn't update Enum in BindingList
  4. DataGridView Default Error on ComboBox Column

답변

2

상태에 따라 BindingList가 작동합니다. 열거 형 객체의 목록을 유지하지만 첫 번째 열거 형에 남겨 둡니다. 바인딩 목록에는 적어도 열거 형 개체가 있으므로 DataGridView 이벤트를 사용하여 데이터 업데이트를 수행 할 수있었습니다. DataGridView에는 CellValueChanged 이벤트가 있습니다. 다음과 같이 이벤트를 구현했습니다.

private void DataGridView_CellValueChanged(object sender, DataGridViewCellEventArgs e) 
{ 
     this.bindingList[e.RowIndex] = (myEnumeration)DataGridView[e.ColumnIndex, e.RowIndex].Value; 
} 

이렇게하면 몇 줄의 코드 만 추가하면 BindingList에 값이 저장됩니다.