我有一种情况,例如我有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'。'