0

我已經綁定了2級數據到MS DataGridView。通過Complex屬性填充的子表不可見。是否有任何定製需要我在DataGridView中啓用嵌套表格視圖請參考附件。如何將2級項目加載到DataGridview?

這是我的DataSource類。

public class Level1 : INotifyPropertyChanged 
{ 
    bool a; 
    bool dispatch, abandon; 
    List<Level2> sample2data = new List<Level2>(); 

    // private Sample2 sampleval; 
    public Level1(bool value, bool disp, bool aban, List<Level2> val) 
    { 
     a = value; 
     dispatch = disp; 
     abandon = aban; 
     sample2data = val; 

    } 
    public bool Status 
    { 
     get { return a; } 
     set { a = value; OnPropertyChanged("A"); } 
    } 

    public bool Dispatch 
    { 
     get { return dispatch; } 
     set 
     { 
      dispatch = value; 
      sample2data.ForEach(item => item.SampleA = false); 
      OnPropertyChanged("Dispatch"); 
     } 

    } 
    public bool Abandon 
    { 
     get { return abandon; } 
     set { abandon = value; sample2data.ForEach(item => item.SampleB = true); OnPropertyChanged("Abandon"); } 
    } 

    public List<Level2> Sample2 
    { 
     get { return sample2data; } 
     set 
     { 
      sample2data = value; OnPropertyChanged("Sample2"); 
     } 
    } 

    public void OnPropertyChanged(string name) 
    { 
     if (PropertyChanged != null) 
      PropertyChanged(this, new PropertyChangedEventArgs(name)); 
    } 

    public event PropertyChangedEventHandler PropertyChanged; 
} 

public class Level2 : INotifyPropertyChanged 
{ 
    public Level2() 
    { 

    } 
    public bool _a, _b; 
    public bool SampleA 
    { 
     get { return _a; } 
     set { _a = value; OnPropertyChanged("SampleA"); } 
    } 
    public bool SampleB 
    { 
     get { return _b; } 
     set { _b = value; OnPropertyChanged("SampleB"); } 
    } 
    public bool SampleC 
    { 
     get; 
     set; 
    } 
    public void OnPropertyChanged(string name) 
    { 
     if (PropertyChanged != null) 
      PropertyChanged(this, new PropertyChangedEventArgs(name)); 
    } 
    public event PropertyChangedEventHandler PropertyChanged; 
} 


class Data 
{ 
    public List<Level1> level1Data = new List<Level1>(); 
    public List<Level2> level2Data = new List<Level2>(); 
    public List<Level1> Level1Data 
    { 
     get { return level1Data; } 
     set { level1Data = value; } 
    } 
    public List<Level2> Level2Data 
    { 
     get { return level2Data; } 
     set { level2Data = value; } 
    } 
    public Data() 
    { 
     for (int i = 0; i < 5; i++) 
     { 
      var newLevel2data = new Level2 
      { 
       SampleC = false 
      }; 
      level2Data.Add(newLevel2data); 
     } 
     for (int i = 0; i < 5; i++) 
     { 
      level1Data.Add(new Level1(false, false, false, level2Data)); 
     } 
    } 

} 

這裏是數據源加載代碼DataGridView

BindingSource bindingSource = new BindingSource(); 
bindingSource.DataSource = new Data(); 
this.dataGridView1.DataSource = bindingSource; 
this.dataGridView1.DataMember = "Level1Data"; 

而且我也有一個問題,當使用複雜屬性數據DataGridView工作,我已經實現INotifyPropertyChanged通知屬性更改到電網的綁定數據源。儘管子表不可見,但我已經更改了子類屬性的一些屬性值,子類的事件始終爲空,所以更改屬性不會通知。但對於父類而言,更改的屬性總是會被通知。

爲了解決PropertyChanged對於Child類變爲空,我在爲子表類數據創建了PropertyChanged事件的同時創建了子表列表的值。由此,屬性被正確地通知。

這裏我的問題是,是否有任何具體的原因或必要鉤子的PropertyChanged事件的子類?因爲沒有爲父級別類屬性完成連接。以及如何使用我的數據源在DataGridView中顯示子表格?

請讓我知道掛鉤PropertyChanged事件的必要性。

回答

0

Windows Forms DataGridView不支持嵌套對象的綁定。你必須延長原控制,以支持它,所以我建議你看看下面的文章:

DataGridView: How to bind nested objects

爲本文的源代碼可以在GitHub找到。

如果上述文章不適合您,只需在您喜歡的搜索引擎上搜索Nested Windows Forms DataGridView即可。由於您遇到的問題非常普遍,因此有許多類似的資源。

編輯:我錯過了關於PropertyChanged事件的問題。你需要在兩個類中實現INotifyPropertyChanged才能正常工作。

相關問題