0

I am using a bindingsource whose datasource is set to a Datatable that is loaded from the database. The columns for integer and float in the database have default values assigned as 0. The query returns either 0 or the actual value for those columns.

I have several Textboxes bound to integer columns and also float columns. My Textboxes that are bound to the float columns show 0 when ever there is a 0 value. My Integer Textboxes show EMPTY when there is a 0 value. I want it to show 0.

EDIT For the Down Vote and Vote to Close. I have seen examples on SO dealing with this issue - some declare to set it in the datatable - which I used that example in my code and it has not changed the result - I type zero in my text box and the box goes blank, I type 1 and it takes it. I know the issue has something to do with what it is bound to. It is an Integer type which is not really nullable - I know you can. So why is it when I type 0 the box goes blank, but if I type 1 it stays. I know the value gets updated whether 0 or whatever. But why the difference between Single and Integer ? Single Works using the same Exact code, Integer does not. Please explain. I read here and other places. How to set default values for data bound controls for addition in VB.NET

EDIT I can paste a value 1.25 in to my Textbox with single values, and it works but I cannot type a value of 1.25 in , when I press the decimal point the cursor automatically moves back to the first character position.

I am not sure why this occurs - I do update the source from the text changed event. I have a flag set though when I enter the textbox IsEditing = True. So the text change event is only effective when the user enters the textbox. I do not understand why this happens - I know it is in the databinding but I do not know what to change to fix it.

My Relevant Code is.

Private Sub LoadTable()

dtBins = DatabaseFetch

SetDefaults()

AddBindings()

End Sub


Private Sub SetDefaults

       Dim columnDefs As String() = New String() 
       {"Fill_Part", "Fill_Pulse", "Fill_Fails", "Fill_Value"}

        For i As Integer = 0 To columnDefs.Length - 1

            If dtBins.Columns.Contains(columnDefs(i)) Then
                dtBins.Columns.Item(columnDefs(i)).DefaultValue = 0
            End If
        Next

End Sub

Private AddBindings()
        bindingtxtFillPulse = New System.Windows.Forms.Binding("Text", 
                                bsBins, 
                                "Fill_Pulse", 
                                True, 
                                DataSourceUpdateMode.OnPropertyChanged)


        bindingtxtFillPulse.NullValue = 0
        bindingtxtFillPulse.DataSourceNullValue = 0

        Me.txtFillPulse.DataBindings.Add(bindingtxtFillPulse)

End Sub
Community
  • 1
  • 1
Ken
  • 2,518
  • 2
  • 27
  • 35
  • What are the values from the database? are you Integer fields null or 0 in the table? Default values only work on new records, they wont fix bad values in tables. Add checks for nulls and replace data values with 0, or add validation which doesn't allow null values. – Marc Lyon Oct 18 '16 at 21:54
  • @MarcLyon The values are ranging from 0 to whatever - there are no nulls. When I try to input a 0 in my textbox it refuses it as if it is bad data, moves the cursor back to the beginning. I do use the txtboxchange event to post the data into my binding source so my datagridview cell can show it. – Ken Oct 18 '16 at 21:59
  • Do you have any code in events such as `Keypress`, `KeyDown`, `KeyUp`, etc? Also, maybe search the document for something like `IsDigit` – David Oct 20 '16 at 15:47
  • @David I did not have any code there I did have a textchanged event that I wanted to use for instant update of the values in the ReadOnly datagridview. I decided to remove databinding from these controls and manually handle everything including validations. Took me all day as I have some single values with decimal and formatting. I now capture them on KeyPress and then update dtable in textchanged event if (focus and entered). When I change gridrow my textboxes do not have focus or entered. so I skip the text changed. I had spent days looking for an answer wish I could find one for the future. – Ken Oct 20 '16 at 19:15

0 Answers0