为DataSource分配BindingSources会显示“在DataSource上找不到DataMember属性'Unit:UnitGroup'”。

时间:2018-09-10 14:58:23

标签: c# data-binding dataset relationship bindingsource

我有一个例子,其中有3个表

如果我具有链接到其他两个表(UnitGroup和UnitGroupDetail)的Unit表

如何正确创建其BindingSources

我写了一个我想做的事的例子。我把里面的一切都弄清楚了

请帮助我

这是代码:

    private BindingSource _mainBs;
    private BindingSource _unitGroupBs;
    private BindingSource _unitGroupDetailBs;

    private BindingSource _unitUnitGroupBs;
    private BindingSource _unitUnitGroupDetail;

    private static DataSet GetDataSet()
    {
        var dataSet = new DataSet("UnitGroup");

        // Create unit table that contains units informations 
        var unit = new DataTable("Unit");
        unit.Columns.Add(new DataColumn("UnitID", typeof(int)));
        unit.Columns.Add(new DataColumn("UnitName", typeof(string)));
        dataSet.Tables.Add(unit);

        // Create Unit group 
        var unitGroup = new DataTable("UnitGroup");
        unitGroup.Columns.Add(new DataColumn("GroupID", typeof(int)));
        unitGroup.Columns.Add(new DataColumn("GroupName", typeof(string)));
        unitGroup.Columns.Add(new DataColumn("BaseUnitID", typeof(int)));
        dataSet.Tables.Add(unitGroup);
        // Adding the relation between UnitGroup table and Unit table

        var unitGroupDetail = new DataTable("UnitGroupDetail");
        unitGroupDetail.Columns.Add(new DataColumn("UnitGroupDetailID", typeof(int)));
        unitGroupDetail.Columns.Add(new DataColumn("UnitGroupID", typeof(int)));
        unitGroupDetail.Columns.Add(new DataColumn("UnitID", typeof(int)));
        unitGroupDetail.Columns.Add(new DataColumn("ConversionRate", typeof(decimal)));
        dataSet.Tables.Add(unitGroupDetail);

        // Relation Name : Unit:UnitGroup
        dataSet.Relations.Add("Unit:UnitGroup",unit.Columns["UnitID"], unitGroup.Columns["BaseUnitID"]);
        // Relation Name : UnitGroup:UnitGroupDetail
        dataSet.Relations.Add("UnitGroup:UnitGroupDetail",unitGroup.Columns["GroupID"], unitGroupDetail.Columns["UnitGroupID"]);
        // Relation Name : Unit:UnitGroupDetail
        dataSet.Relations.Add("Unit:UnitGroupDetail",unit.Columns["UnitID"], unitGroupDetail.Columns["UnitID"]);

        return dataSet;
    }


    private void PrepareBindingSources()
    {
        _mainBs = new BindingSource(GetDataSet(), string.Empty);

        _unitGroupBs = new BindingSource(_mainBs, "UnitGroup");

        _unitGroupDetailBs = new BindingSource(_unitGroupBs, "UnitGroup:UnitGroupDetail");

        _unitUnitGroupBs = new BindingSource(_unitGroupBs, "Unit:UnitGroup");

        _unitUnitGroupDetail = new BindingSource(_unitGroupDetailBs, "Unit:UnitGroupDetail");

    }


    private void Form1_Load(object sender, EventArgs e)
    {
        PrepareBindingSources();
    }

当我运行代码时,它说 System.ArgumentException:在数据源上找不到'DataMember属性'Unit:UnitGroup'。'

0 个答案:

没有答案