vb.net Winforms Textbox 绑定到具有货币格式的 bindingsource 和 datagridview 无法正确刷新

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13984288/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 11:38:05  来源:igfitidea点击:

Winforms Textbox bound to bindingsource with currency format and datagridview not refreshing properly

vb.netwinformsbindingsource

提问by ByerRA

I'm kinda new to programming with bindingsources and datagridviews (as well as VB .NET) so here's my question/problem.

我对使用 bindingsources 和 datagridviews(以及 VB .NET)编程有点陌生,所以这是我的问题/问题。

I'm programming an app in VB 2010 in which users will update data in a SQL 2008 R2 server on the back end (they won't be adding or deleting any records and not all fields will be updated). The form in design mode has a bindingsource, dataset and the various textboxes for the data as well as a datagridview at the bottom of the form.

我正在 VB 2010 中编写一个应用程序,其中用户将更新后端 SQL 2008 R2 服务器中的数据(他们不会添加或删除任何记录,也不会更新所有字段)。设计模式下的表单具有绑定源、数据集和各种数据文本框以及表单底部的数据网格视图。

When the form loads a DataAdapter is loaded and then the dataset is loaded. (code for the connection omitted, "text_table" is a test table in the SQL server.

当表单加载时,会加载一个 DataAdapter,然后加载数据集。(连接代码省略,“text_table”是SQL server中的测试表。

frmDataAdapter = New SqlClient.SqlDataAdapter(SQL_Query, frmConnection)
frmConnection.Open()
frmDataAdapter.FillSchema(frmDataSet, SchemaType.Source, "test_table")
frmDataAdapter.Fill(frmDataSet, "test_table")

I bind the BindingSource to the DataSet...

我将 BindingSource 绑定到 DataSet ...

With frmBindingSource
    .DataMember = "test_table"
    .DataSource = frmDataSet
End With

I then bind the DataGridView on the form to the BindingSource...

然后我将表单上的 DataGridView 绑定到 BindingSource ...

frmDataGridView.DataSource = frmBindingSource

Now, in the "test_table" DB table some fields are currency and are not updated buy the user so I bind them as follows...

现在,在“test_table”数据库表中,一些字段是货币并且没有更新购买用户,所以我将它们绑定如下......

Me.Total_Amount.DataBindings.Add("Text", frmBindingSource, "Total", True, DataSourceUpdateMode.Never, vbNull, "c")

Now here's the problem...

现在问题来了……

The users use the DataGridView to move around the records which is working with no problems. Where we run into trouble is that the "Total" field can be "Null" and if the user hits a record with a "Null" in the "Total" field it doesn't blank out in the bound text box for the field, it only changes when there's a "non-Null" value.

用户使用 DataGridView 在没有问题的情况下移动记录。我们遇到麻烦的地方是“总计”字段可以是“空”,如果用户在“总计”字段中点击了带有“空”的记录,它不会在该字段的绑定文本框中空白,它仅在存在“非空”值时才会更改。

So if the user starts out and the first three records have a "Null" in the "Total" field the textbox for that field stays blank. If the fourth record has a value, the text box for the field changes to that value, but if they go back a record the value in the text box for the field doesn't blank but keeps the value and will keep the value until a different "non-Null" value is found.

因此,如果用户开始并且前三个记录在“总计”字段中具有“空”,则该字段的文本框保持空白。如果第四条记录有值,则该字段的文本框将更改为该值,但如果他们返回一条记录,则该字段的文本框中的值不会为空而是保留该值并将保留该值直到发现不同的“非空”值。

But, if I remove the formatting during binding like this...

但是,如果我像这样在绑定期间删除格式...

Me.Total_Amount.DataBindings.Add("Text", frmBindingSource, "Total")

everything works perfect.

一切正常。

What am I doing wrong here, what simple thing am I overlooking for forgot and how can I fix it as I would really like to get this formatted correctly on the form.

我在这里做错了什么,我忽略了什么简单的事情忘记了,我该如何修复它,因为我真的很想在表单上正确地格式化它。

回答by ByerRA

Found the answer to the problem... (helps getting some sleep after working several 13 hours days in a row)

找到了问题的答案......(在连续工作 13 小时后有助于入睡)

Had I read the documentation a little better I would have known to change the "vbNull" in my data binding line...

如果我更好地阅读文档,我就会知道更改数据绑定行中的“vbNull”...

Me.Total_Amount.DataBindings.Add("Text", frmBindingSource, "Total", True, DataSourceUpdateMode.Never, vbNull, "c")

To just be vbNullString...

只是 vbNullString ...

Me.Total_Amount.DataBindings.Add("Text", frmBindingSource, "Total", True, DataSourceUpdateMode.Never, vbNullString, "c")

so it will default to "blank" (i.e. vbNullString) when a DBNull is detected, no need to change my SQL query which works.

因此,当检测到 DBNull 时,它将默认为“空白”(即 vbNullString),无需更改我的 SQL 查询。