Click here to Skip to main content
15,895,084 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
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 


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.
Posted
Comments
RickZeeland 23-Jul-16 3:08am    
I think it would be better to "flatten" your data. Or you could try using a BindingList in combination with a master-detail construction, there is an excellent article on CodeProject about this: http://www.codeproject.com/Articles/24656/A-Detailed-Data-Binding-Tutorial

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900