如何将父子级数据加载到datagridview? [英] How to load parent child level data to datagridview?
问题描述
Hi All,
I have datasource which implements child and parent level options implemented with 2 inner classes. When i load the data to the dataGridView , the grid is empty.
Here is the code.
public class ViewModel : INotifyPropertyChanged
{
private const int NUM_LEVEL1 = 500;
private const int NUM_LEVEL2_EACH_LEVEL = 6;
public List<Level1Item> Level1Items
{
get { return _level1Items; }
}
private readonly List<Level1Item> _level1Items = new List<Level1Item>();
public ViewModel()
{
for (int lv1Index = 0; lv1Index < NUM_LEVEL1; lv1Index++)
{
var newLv1Item = new Level1Item
{
Status = "300-INTQUA - International Quarterback Pty Ltd",
Currency = "AUD",
Amount = "$9,942.11",
Underwriter = "Fitness Centre Scheme",
Level2Items = new List<Level2Item>(),
};
for (int lv2Index = 0; lv2Index < NUM_LEVEL2_EACH_LEVEL; lv2Index++)
{
var newLv2Item = new Level2Item
{
Status = "Outstanding " + lv1Index + "/" + lv2Index,
Transaction = "300-0166574",
Date = "15/11/2016",
Type = "Renew",
Description = "039246 Industrial Special",
Effective = "01/12/2016",
Currency = "AUD",
Amount = "$10,887.00",
Underwriter = "QBE Insurance (Australia) Ltd",
};
newLv1Item.Level2Items.Add(newLv2Item);
newLv2Item.PropertyChanged += newLv2Item_PropertyChanged;
}
_level1Items.Add(newLv1Item);
}
}
void newLv2Item_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(e.PropertyName));
}
public class Level1Item : INotifyPropertyChanged
{
public List<Level2Item> Level2Items
{
get;
set;
}
private bool _doDispatch;
public bool DoDispatch
{
get { return _doDispatch; }
set
{
_doDispatch = value;
// Other business logic goes here..
Level2Items.ForEach(item => item.DoDispatch = value);
if (value)
{
Level2Items.ForEach(item => item.DoAbandon = false);
}
OnPropertyChanged("DoDispatch");
}
}
private bool _doAbandon;
public bool DoAbandon
{
get { return _doAbandon; }
set
{
_doAbandon = value;
// Other business logic goes here..
Level2Items.ForEach(item => item.DoAbandon = value);
if (value)
{
Level2Items.ForEach(item => item.DoDispatch = false);
}
OnPropertyChanged("DoAbandon");
}
}
public bool DoDispatchPremiumFundingQuote
{
get;
set;
}
public bool DoEmail
{
get;
set;
}
public bool DoPrint
{
get;
set;
}
public string Status
{
get;
set;
}
public string Currency
{
get;
set;
}
public string Amount
{
get;
set;
}
public string Underwriter
{
get;
set;
}
public void OnPropertyChanged(string name)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(name));
}
public event PropertyChangedEventHandler PropertyChanged;
}
public class Level2Item : INotifyPropertyChanged
{
private bool _DoDispatch;
public bool DoDispatch
{
get { return _DoDispatch; }
set
{
_DoDispatch = value;
OnPropertyChanged("DoDispatch");
}
}
internal void UpdateDispatch(bool dispatch)
{
// Other business logic omitted here..
DoDispatch = dispatch;
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs("DoDispatch"));
}
}
private bool _DoAbandon;
public bool DoAbandon
{
get { return _DoAbandon; }
set
{
_DoAbandon = value;
OnPropertyChanged("DoAbandon");
}
}
internal void UpdateAbandon(bool abandon)
{
// Other business logic omitted here..
DoAbandon = abandon;
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs("DoAbandon"));
}
}
public string Status
{
get;
set;
}
public string Transaction
{
get;
set;
}
public string Date
{
get;
set;
}
public string Type
{
get;
set;
}
public string Description
{
get;
set;
}
public string Effective
{
get;
set;
}
public string Currency
{
get;
set;
}
public string Amount
{
get;
set;
}
public string Underwriter
{
get;
set;
}
public void OnPropertyChanged(string name)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(name));
}
public event PropertyChangedEventHandler PropertyChanged;
}
public event PropertyChangedEventHandler PropertyChanged;
}
Could any one share me what i have done in this to load the child data?
Appreciate your response.
Regards,
Amal
我尝试过:
1)this.dataGridView1.DataSource = new ViewModel();使用这样的网格是空的。
2)this.dataGridView1.DataSource = new ViewModel()。Level1Items;第一级数据仅加载到网格。
这里Level1items是包含父数据的列表集合和包含子数据的集合。但网格仅加载1级数据。
What I have tried:
1) this.dataGridView1.DataSource = new ViewModel(); the grid is empty when using like this.
2)this.dataGridView1.DataSource = new ViewModel().Level1Items; the first level of data are only loaded to grid.
Here the Level1items is the list collection contains the parent data and a collection which contains the child data. But the grid loads the level 1 datas only.
推荐答案
9,942.11,
Underwriter = 健身中心计划,
Level2Items = 新列表< Level2Item>( ),
};
for ( int lv2Index = < span class =code-digit> 0 ; lv2Index < NUM_LEVEL2_EACH_LEVEL; lv2Index ++)
{
var newLv2Item = new Level2Item
{
Status = Outstanding + lv1Index + / + lv2Index,
Transaction =300-0166574,
Date = 15/11/2016,
Type = 续订,
说明= 039246 Industrial Special,
Effective = 2016/12/01,
货币= AUD,
金额=
9,942.11", Underwriter = "Fitness Centre Scheme", Level2Items = new List<Level2Item>(), }; for (int lv2Index = 0; lv2Index < NUM_LEVEL2_EACH_LEVEL; lv2Index++) { var newLv2Item = new Level2Item { Status = "Outstanding " + lv1Index + "/" + lv2Index, Transaction = "300-0166574", Date = "15/11/2016", Type = "Renew", Description = "039246 Industrial Special", Effective = "01/12/2016", Currency = "AUD", Amount = "
10,887.00,
Underwriter = QBE Insurance(Australia)Ltd,
};
newLv1Item.Level2Items.Add(newLv2Item);
newLv2Item.PropertyChanged + = newLv2Item_PropertyChanged;
}
_level1Items.Add(newLv1Item);
}
}
void newLv2Item_PropertyChanged( object sender,PropertyChangedEventArgs e)
{
if (PropertyChanged!= null )
PropertyChanged( this , new PropertyChangedEventArgs(e.PropertyName));
}
public class Level1Item:INotifyPropertyChanged
{
public 列表< Level2Item> Level2Items
{
get ;
set ;
}
private bool _doDispatch;
public bool DoDispatch
{
get { return _doDispatch; }
set
{
_doDispatch = value ;
// 其他业务逻辑就在这里..
Level2Items.ForEach(item = > item.DoDispatch = value );
if ( value )
{
Level2Items .ForEach(item = > item.DoAbandon = false );
}
OnPropertyChanged( DoDispatch);
}
}
私人 bool _doAbandon;
public bool DoAbandon
{
get { return _doAbandon; }
set
{
_doAbandon = value ;
// 其他业务逻辑就在这里..
Level2Items.ForEach(item = > item.DoAbandon = value );
if ( value )
{
Level2Items .ForEach(item = > item.DoDispatch = false );
}
OnPropertyChanged( DoAbandon);
}
}
public bool DoDispatchPremiumFundingQuote
{
get ;
set ;
}
public bool DoEmail
{
get ;
set ;
}
public bool DoPrint
{
get ;
set ;
}
public string 状态
{
get ;
set ;
}
public string 货币
{
get ;
set ;
}
public string 金额
{
< span class =code-keyword> get ;
set ;
}
public string 包销商
{
< span class =code-keyword> get ;
set ;
}
public void OnPropertyChanged( string name)
{
if (PropertyChanged!= null )
PropertyChanged( this , new PropertyChangedEventArgs(name));
}
public event PropertyChangedEventHandler PropertyChanged;
}
public class Level2Item:INotifyPropertyChanged
{
private bool _DoDispatch;
public bool DoDispatch
{
get { return _DoDispatch; }
set
{
_DoDispatch = value ;
OnPropertyChanged( DoDispatch);
}
}
内部 void UpdateDispatch( bool dispatch)
{
// 此处省略了其他业务逻辑..
DoDispatch = dispatch;
if (PropertyChanged!= null )
{
PropertyChanged( 此, new PropertyChangedEventArgs( DoDispatch跨度>));
}
}
private bool _DoAbandon;
public bool DoAbandon
{
get { return _DoAbandon; }
set
{
_DoAbandon = value ;
OnPropertyChanged( DoAbandon);
}
}
内部 void UpdateAbandon( bool 放弃)
{
// 此处省略了其他业务逻辑..
DoAbandon =放弃;
if (PropertyChanged!= null )
{
PropertyChanged( 此, new PropertyChangedEventArgs( DoAbandon跨度>));
}
}
public string 状态
{
get ;
set ;
}
public string 交易
{
get ;
set ;
}
public string 日期
{
get ;
set ;
}
public string 输入
{
get ;
set ;
}
public string 描述
{
get ;
set ;
}
public string 有效
{
get ;
set ;
}
public string 货币
{
get ;
set ;
}
public string 金额
{
get ;
set ;
}
public string 承销商
{
get ;
set ;
}
public void OnPropertyChanged( string name)
{
if (PropertyChanged!= null )
PropertyChanged( this , new PropertyChangedEventArgs(name));
}
public event PropertyChangedEventHandler PropertyChanged;
}
public event PropertyChangedEventHandler PropertyChanged;
}
任何人都可以分享我的工作 这个加载子数据?
感谢您的回复。
问候,
Amal
10,887.00", Underwriter = "QBE Insurance (Australia) Ltd", }; newLv1Item.Level2Items.Add(newLv2Item); newLv2Item.PropertyChanged += newLv2Item_PropertyChanged; } _level1Items.Add(newLv1Item); } } void newLv2Item_PropertyChanged(object sender, PropertyChangedEventArgs e) { if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(e.PropertyName)); } public class Level1Item : INotifyPropertyChanged { public List<Level2Item> Level2Items { get; set; } private bool _doDispatch; public bool DoDispatch { get { return _doDispatch; } set { _doDispatch = value; // Other business logic goes here.. Level2Items.ForEach(item => item.DoDispatch = value); if (value) { Level2Items.ForEach(item => item.DoAbandon = false); } OnPropertyChanged("DoDispatch"); } } private bool _doAbandon; public bool DoAbandon { get { return _doAbandon; } set { _doAbandon = value; // Other business logic goes here.. Level2Items.ForEach(item => item.DoAbandon = value); if (value) { Level2Items.ForEach(item => item.DoDispatch = false); } OnPropertyChanged("DoAbandon"); } } public bool DoDispatchPremiumFundingQuote { get; set; } public bool DoEmail { get; set; } public bool DoPrint { get; set; } public string Status { get; set; } public string Currency { get; set; } public string Amount { get; set; } public string Underwriter { get; set; } public void OnPropertyChanged(string name) { if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(name)); } public event PropertyChangedEventHandler PropertyChanged; } public class Level2Item : INotifyPropertyChanged { private bool _DoDispatch; public bool DoDispatch { get { return _DoDispatch; } set { _DoDispatch = value; OnPropertyChanged("DoDispatch"); } } internal void UpdateDispatch(bool dispatch) { // Other business logic omitted here.. DoDispatch = dispatch; if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs("DoDispatch")); } } private bool _DoAbandon; public bool DoAbandon { get { return _DoAbandon; } set { _DoAbandon = value; OnPropertyChanged("DoAbandon"); } } internal void UpdateAbandon(bool abandon) { // Other business logic omitted here.. DoAbandon = abandon; if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs("DoAbandon")); } } public string Status { get; set; } public string Transaction { get; set; } public string Date { get; set; } public string Type { get; set; } public string Description { get; set; } public string Effective { get; set; } public string Currency { get; set; } public string Amount { get; set; } public string Underwriter { get; set; } public void OnPropertyChanged(string name) { if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(name)); } public event PropertyChangedEventHandler PropertyChanged; } public event PropertyChangedEventHandler PropertyChanged; } Could any one share me what i have done in this to load the child data? Appreciate your response. Regards, Amal
我尝试过:
1)this.dataGridView1.DataSource = new ViewModel();使用这样的网格是空的。
2)this.dataGridView1.DataSource = new ViewModel()。Level1Items;第一级数据仅加载到网格。
这里Level1items是包含父数据的列表集合和包含子数据的集合。但是网格仅加载1级数据。
What I have tried:
1) this.dataGridView1.DataSource = new ViewModel(); the grid is empty when using like this.
2)this.dataGridView1.DataSource = new ViewModel().Level1Items; the first level of data are only loaded to grid.
Here the Level1items is the list collection contains the parent data and a collection which contains the child data. But the grid loads the level 1 datas only.
这篇关于如何将父子级数据加载到datagridview?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!