本文介绍了数据绑定textbox_leave()事件奇怪的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好。我使用VS2015社区和Sql server 2008.你可以跳转到最后一段来看问题结论。

在WinForm上我有一个TypedDataSet,一个BindingSource,一个SqlDataAdapter(我不用自动生成的TableAdapter)。我在表格中可以猜到,一些DataBinded文本框和一些按钮用于添加,编辑,删除,保存和取消。该程序已经过测试。
一切正常。 

有一个textBoxUserId和一个textBoxUserName。按下[New]按钮后,SqlDataAdapter从数据库中的表中获取零行数据,以便清除表单并获取SqlCommandBuilder。然后它调用dataTable.Add()来添加一行。
字段"userId"现在为空。我在textBoxUserId上键入了一个用户ID,然后点击了TAB。 textBoxUserId.Text被复制到dataTable的UserId字段。这就是它应该是的。 

然后,我添加了一个textBoxUserId_Load事件,其中的代码使用textBoxUserId.Text作为密钥从数据库上的另一个表中获取用户名,并将用户名放入dataTable的UserName字段,并更新textBoxUserName.Text以反映
更改。现在我按[添加]按钮,输入用户ID并点击TAB。 textBoxUserName得到更新。但是dataTable的UserId没有更新为保存textBoxUserId.Text。而是将dataTable的UserId(现在为null)反馈给textBoxUserId.Text,然后将其清空。我知道dataTable的
UserId为null,因为我使用MessageBox.Show()来检查它。即使我将任何字符串值放到dataTable的任何字段上,textBoxUserId.Text也无法更新到dataTable的UserId字段。 
$
简而言之,当一行数据为0行的数据表添加一行时,如果textBoxUserId_Leave()中的代码更改了dataTable的任何字段,一旦输入焦点从textBoxUserId移动到另一个控件,textBoxUserId就不会更新到底层的
dataTable,而是从dataTable重新加载(nullify在这种情况下textBoxUserId.Text)。有什么想法吗?

Hello All. I use VS2015 Community and Sql server 2008. You could jump to the last paragraph to see the problem conclusion first.
On a WinForm I have a TypedDataSet, a BindingSource, a SqlDataAdapter(I don't use the auto-generated TableAdapter). I have on the form as you can guess, some DataBinded textboxes and some buttons for add,edit,delete,save and cancel.The program has been tested. Everything works fine. 
There is a textBoxUserId and a textBoxUserName. After I press the [New] button, SqlDataAdapter fetches zero row of data from a table on the database, so as to clear the form and get SqlCommandBuilder involved. Then it calls dataTable.Add() to add a row. The field "userId" is now null. I type a user id onto textBoxUserId and I hit TAB. textBoxUserId.Text is copied to the dataTable's UserId field. This is what it is supposed to be. 
Then, I added a textBoxUserId_Load Event, the code in it uses the textBoxUserId.Text as a key to fetch the user name from another table on the database, and put the user name into dataTable's UserName Field and textBoxUserName.Text is updated to reflect the change. Now I press the [Add] button, type user id and hit TAB. textBoxUserName gets updated. But dataTable's UserId is not updated to hold textBoxUserId.Text. Instead dataTable's UserId(null now) is fed back to textBoxUserId.Text, empting it . I know dataTable's UserId is null because I use MessageBox.Show() to check it. Even if I put any string value onto any field of dataTable, textBoxUserId.Text cannot be updated to dataTable's UserId field. 
In short, when a dataTable with 0 row of data gets one row added, and if there is code in textBoxUserId_Leave() that changes any field of the dataTable, once input focus is moved from textBoxUserId to another control, textBoxUserId is not updated to the underlying dataTable, but instead gets reloaded from the dataTable (nullify textBoxUserId.Text in this case). Any idea ?

推荐答案

private void tbUserId_Leave(..)

private void tbUserId_Leave(..)

{

    bindingSource1.EndEdit();   //首先发送tbUserId.Text  bindingSource1。

   bindingSource1.EndEdit();  // send tbUserId.Text back bindingSource1 first.

    ....

   ....

}


这篇关于数据绑定textbox_leave()事件奇怪的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 06:52