来自 BindingSource 的 C# 刷新文本框 [英] C# refreshing textbox from BindingSource

查看:20
本文介绍了来自 BindingSource 的 C# 刷新文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法刷新使用 BindingSource 对象的 Windows 窗体控件.我们有一个 CAB/MVP/SCSF 客户端,我(实际上是我们",因为这是一个团队努力)正在开发,它将与运行在远程服务器上的 WCF 服务进行交互.(这是我们第一次尝试,所以我们处于学习模式).对服务的调用之一(来自演示者)返回一个包含 3 个数据表的数据集,名为合同"、贷款"和条款".每个表只包含一行.当服务返回数据集时,我们通过在视图中调用名为 BindData() 的函数并将数据集从演示者类传递给视图,将其存储在 SmartPart/View 中的类成员变量中;

I am having difficulty refreshing windows forms controls that are using a BindingSource object. We have a CAB/MVP/SCSF client that I (actually "we" since it is a team effort) are developing that will interact with WCF services running on a remote server. (This is our first attempt at this, so we are in a learning mode). One of the calls (from the Presenter) to the service returns a DataSet that contains 3 DataTables, named "Contract", "Loan" and "Terms". Each table contains just one row. When the service returns the dataset, we store it in the SmartPart/View in a class member variable, by calling a function in the view called BindData() and passing the dataset in to the view from the presenter class;

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 Forms TextBox、MaskedEditBox 和 Infragistics UltraComboEditor Dropdown 组合框.我们创建了三个 BindingSource 对象,使用 VS2008 IDE 为每个 DataTable 创建了一个对象.

We are trying to bind each of the three DataTables to an assortment of Windows Forms TextBoxes, MaskedEditBoxes, and Infragistics UltraComboEditor Dropdown comboboxes We created three BindingSource objects, one for each DataTable using the VS2008 IDE.

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 服务来保存"它.

This works when we do the first read from the service and get a dataset back. The information is displayed on the form's controls, we can update it using the form, and then "save" it by passing the changed values back to the WCF service.

这是我们的问题.如果我们选择不同的贷款键并对服务进行相同的调用并获得一个新的数据集,同样有 3 个表,每个表一行,控件(文本框、屏蔽编辑框等)不会用新信息更新.请注意, smartPart/View 没有关闭或任何东西,但在调用服务之间保持加载.在第二次调用时,我们不会重新绑定调用,而只是尝试从更新后的 DataSet 中刷新数据.

Here is our problem. If we select a different loan key and make the same call to the service and get a new DataSet, again with 3 tables with one row each, the controls (textboxes, masked edit boxes, etc.) are not being updated with the new information. Note that the smartPart/View is not closed or anything, but remains loaded in between calls to the service. On the second call we are not rebinding the calls, but simply trying to get the data to refresh from the updated DataSet.

我们已经尝试了我们能想到的一切,但显然我们遗漏了一些东西.这是我们第一次尝试使用 BindingSource 控件.我们试过了

We have tried everything we can think of, but clearly we are missing something. This is our first attempt at using the BindingSource control. We have tried

bindsrcContract.ResetBindings(false);

bindsrcContract.ResetBindings(true);

bindsrcContract.RaiseListChangedEvents = true;

for (int i = 0; i < bindsrcContract.Count; i++)
{
    bindsrcContract.ResetItem(i);
}

以及再次重置 DataMember 属性.

As well as resetting the DataMember property again.

bindsrcContract.DataMember = "Contract";

我们看过很多例子.许多示例都引用了 BindingNavigator,但由于 DataTables 只有一行,我们认为我们不需要它.有很多关于网格的例子,但我们没有在这里使用一个.任何人都可以指出我们哪里出错了,或者向我们指出可以提供更多信息的资源吗?

We’ve looked at a lot of examples. Many examples make reference to the BindingNavigator but since the DataTables only have one row, we did not think we needed that. There are a lot of examples for grids, but we’re not using one here. Can anyone please point out where we are going wrong, or point us to resources that will provide some more information?

我们使用的是 VisualStudio 2008、C# 和 .Net 2.0、XP 客户端、W2K3 服务器.

We’re using VisualStudio 2008, C# and .Net 2.0, XP client, W2K3 server.

提前致谢

我们

推荐答案

我今天遇到了类似的问题,发现这个方法有效.

I was having a similar issue today and found this works.

private void btnCancel_Click(object sender, EventArgs e)
{
    this.MyTable.RejectChanges();
    this.txtMyBoundTextBox.DataBindings[0].ReadValue();
    this.EditState = EditStates.NotEditting;
}

这篇关于来自 BindingSource 的 C# 刷新文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆