如何将2个级别的项目加载到DataGridview?

时间:2016-07-27 07:00:59

标签: c# winforms datagridview parent-child inotifypropertychanged

我已将2级数据限制在MS DataGridView中。通过Complex属性填充的子表不可见。我是否需要进行任何自定义以在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事件始终为null,因此不会通知属性更改。但是对于父类,更改的属性始终会被通知。

要解决PropertyChanged对于Child Class变为null,我已为子类数据挂钩PropertyChanged事件,同时为子表列表创建值。通过这种方式,可以正确通知属性。

这里我的问题是,是否有任何特定的理由或必要将ChildChanged事件挂钩到子类?因为没有为父级别类属性进行连接。以及如何使用我的数据源在DataGridView中显示子表?

请告诉我挂钩PropertyChanged事件的必要性。

1 个答案:

答案 0 :(得分:0)

Windows窗体DataGridView不支持嵌套对象的绑定。您必须扩展原始控件以支持它,因此我建议您查看以下文章:

DataGridView: How to bind nested objects

该文章的源代码可在GitHub上找到。

如果上述文章对您不起作用,只需在您最喜爱的搜索引擎上搜索Nested Windows Forms DataGridView即可。由于您遇到的问题非常普遍,因此有很多类似的资源。

修改:我错过了有关PropertyChanged事件的问题。您需要在两个类中实现INotifyPropertyChanged才能使其正常工作。