我已經綁定了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
事件的必要性。