Vb.net – Winforms Textbox bound to bindingsource with currency format and datagridview not refreshing properly

bindingsourcevb.netwinforms

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

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.

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.

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…

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

I then bind the DataGridView on the form to the 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…

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.

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.

Best Answer

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

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

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

To just be 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.

Related Topic