BindingSource error with relation dataset

BindingSource之后与关系数据集错误

Tags: c#
标签: c#

问题 (Question)

I have two tables: Customer and Order. I fill the tables to dataset, add relation to dataset and bind to datasources:

var customerDs = _customerDAO.ExecuteSelectCommand(_customerCmd.SelectCommand, _customerCmd.TableName);
var orderDs = _orderDAO.ExecuteSelectCommand(_orderCmd.SelectCommand, _orderCmd.TableName);

_ds = new DataSet();

_ds.Tables.Add(customerDs.Tables[0].Copy());
_ds.Tables.Add(orderDs.Tables[0].Copy());

var relation = new DataRelation("CustomerOrderRelation", _ds.Tables["Customer"].Columns["id"],
                            _ds.Tables["CustomerOrder"].Columns["Customer_id"]);
_ds.Relations.Add(relation);

_customerBs = new BindingSource();
_customerBs.DataSource = _ds;
_customerBs.DataMember = "Customer";

_orderBs = new BindingSource();
_orderBs.DataSource = _customerBs;
_customerBs.DataMember = "CustomerOrderRelation";

But the error

"DataMember property 'CustomerOrderRelation' cannot be found on the DataSource" thrown on "_customerBs.DataMember = "CustomerOrderRelation";"

What the problem? I can't figure that out!

我有两个表:客户和订单。我填的表数据集,添加数据集和绑定到数据源:

var customerDs = _customerDAO.ExecuteSelectCommand(_customerCmd.SelectCommand, _customerCmd.TableName);
var orderDs = _orderDAO.ExecuteSelectCommand(_orderCmd.SelectCommand, _orderCmd.TableName);

_ds = new DataSet();

_ds.Tables.Add(customerDs.Tables[0].Copy());
_ds.Tables.Add(orderDs.Tables[0].Copy());

var relation = new DataRelation("CustomerOrderRelation", _ds.Tables["Customer"].Columns["id"],
                            _ds.Tables["CustomerOrder"].Columns["Customer_id"]);
_ds.Relations.Add(relation);

_customerBs = new BindingSource();
_customerBs.DataSource = _ds;
_customerBs.DataMember = "Customer";

_orderBs = new BindingSource();
_orderBs.DataSource = _customerBs;
_customerBs.DataMember = "CustomerOrderRelation";

但是这个错误

“数据成员属性“CustomerOrderRelation”无法找到数据源”“_customerBs抛出。数据成员= " CustomerOrderRelation”;“

出了什么问题?我不知道,出去!

最佳答案 (Best Answer)

It seems you are trying to implement Master/Detail binding.

Based on this example, I think that this part of code only is wrong:

_customerBs.DataMember = "CustomerOrderRelation";

Just try to set the relation in DataMember to the correct bindingsource (the detail bindingsource), and this should work:

_orderBs.DataMember = "CustomerOrderRelation";

Hope this helps.

看来你正试图实现主/明细绑定。

基于这个例子,我认为这部分的代码是错误的:

_customerBs.DataMember = "CustomerOrderRelation";

本文翻译自StackoverFlow,英语好的童鞋可直接参考原文:http://stackoverflow.com/questions/16925930