我在刷新使用 BindingSource 对象的 Windows 窗体控件时遇到困难。我们有一个 CAB/MVP/SCSF 客户端,我(实际上是“我们”,因为它是团队的成果)正在开发,它将与远程服务器上运行的 WCF 服务进行交互。 (这是我们第一次尝试,所以我们处于学习模式)。对服务的调用之一(来自演示者)返回一个包含 3 个数据表的数据集,分别命名为“Contract”、“Loan”和“Terms”。每个表仅包含一行。当服务返回数据集时,我们通过调用视图中名为 BindData() 的函数并将数据集从演示者类传递到视图中,将其存储在 SmartPart/View 的类成员变量中;
private System.Data.DataSet _ds = null;
public void BindData(System.Data.DataSet ds)
{
string sErr = "";
try
{
_ds = ds; // save to private member variable
// more code goes down here
}
}
我们尝试将三个 DataTable 中的每一个绑定到 Windows 窗体 TextBox、MaskedEditBox 和 Infragistics UltraComboEditor 下拉组合框的分类。我们使用 VS2008 IDE 创建了三个 BindingSource 对象,每个 DataTable 一个。
private System.Windows.Forms.BindingSource bindsrcContract;
private System.Windows.Forms.BindingSource bindsrcLoan;
private System.Windows.Forms.BindingSource bindsrcTerms;
我们像这样绑定值
if (bindsrcContract.DataSource == null)
{
bindsrcContract.DataSource = _ds;
bindsrcContract.DataMember = “contract”;
txtContract.DataBindings.Add(new Binding("Text", bindsrcContract, "contract_id", true));
txtLateFeeAmt.DataBindings.Add(new Binding("Text", bindsrcContract, "fee_code", true));
txtPrePayPenalty.DataBindings.Add(new Binding("Text", bindsrcContract, "prepay_penalty", true));
txtLateFeeDays.DataBindings.Add(new Binding("Text", bindsrcContract, "late_days", true));
}
if (bindsrcLoan.DataSource == null)
{
bindsrcLoan.DataSource = _ds;
bindsrcLoan.DataMember = “loan”;
mskRecvDate.DataBindings.Add(new Binding("Text", bindsrcLoan, "receive_date", true));
cmboDocsRcvd.DataBindings.Add(new Binding("Value", bindsrcLoan, "docs", true));
}
当我们第一次从服务读取并获取数据集时,这会起作用。信息显示在表单的控件上,我们可以使用表单更新它,然后通过将更改的值传递回 WCF 服务来“保存”它。
这是我们的问题。如果我们选择不同的贷款密钥并对服务进行相同的调用并获取一个新的数据集,同样有 3 个表,每个表一行,控件(文本框、屏蔽编辑框等)不会使用新信息进行更新。请注意,smartPart/View 并未关闭或任何其他操作,而是在服务调用之间保持加载状态。在第二次调用中,我们不会重新绑定调用,而只是尝试从更新的数据集中刷新数据。
我们已经尝试了所有能想到的方法,但显然我们遗漏了一些东西。这是我们第一次尝试使用 BindingSource 控件。我们已经尝试过了
bindsrcContract.ResetBindings(false);
and
bindsrcContract.ResetBindings(true);
and
bindsrcContract.RaiseListChangedEvents = true;
and
for (int i = 0; i < bindsrcContract.Count; i++)
{
bindsrcContract.ResetItem(i);
}
以及再次重置 DataMember 属性。
bindsrcContract.DataMember = ”Contract”;
我们已经看过很多例子。许多示例都引用了 BindingNavigator,但由于 DataTables 只有一行,我们认为不需要它。网格的示例有很多,但我们在这里不使用一个。谁能指出我们哪里出了问题,或者向我们指出可以提供更多信息的资源?
我们使用 VisualStudio 2008、C# 和 .Net 2.0、XP 客户端、W2K3 服务器。
提前致谢
wes