Click here to Skip to main content
15,885,048 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:

Quote:

when using binding checkbox checked (slow and sometimes error at run time)


while using "checkstate" works correctly.

Is there way to bind checkbox with checked and unchecked only with null represented as unchecked




VB
Sub bindingTest(container As Control, sql As String, BindingSource1 As BindingSource, BindingNavigator1 As BindingNavigator)
    Dim field As String

    Dim str As String = ""
    da = New SqlDataAdapter(sql, CSql.sqlDatabaseConnString)
    ds = New DataSet()
    da.Fill(ds)
    BindingSource1.DataSource = ds.Tables(0)
    '  BindingSource1.DataSource = dt.TableName
    BindingNavigator1.BindingSource = BindingSource1
    For Each oc As Control In container.Controls
        If (TypeOf (oc) Is CheckBox) Then
            Dim oType As CheckBox
            oType = CType(oc, CheckBox)
            field = oType.Tag.ToString
            oc.DataBindings.Clear()
            If varcheckState = True Then
                '' working fine
                '   oType.DataBindings.Add(New Windows.Forms.Binding("checkstate", BindingSource1, field, True))
                oType.DataBindings.Add("checkstate", BindingSource1, field, True)
            Else
                ''' slow and sometimes not working
                '   oType.DataBindings.Add(New Windows.Forms.Binding("Checked", BindingSource1, field, False))
                oType.DataBindings.Add("Checked", BindingSource1, field, True)
            End If
        End If


    Next
End Sub


What I have tried:

I tried using checkstate and not worked but I need my checkbox state null as unchecked

oType.DataBindings.Add("Checked", BindingSource1, field, True)
Posted
Updated 5-Jan-19 9:08am

1 solution

Well, true is true, false is false and null is null.
What this meant to you? You have to handle it. But... please read it before:
Nullable Value Types (Visual Basic) | Microsoft Docs[^]
Databinding and Nullable types in WinForms.NET[^]

How to achieve that? Please, see:
Bound CheckBoxes when value is null show as Indeterminate on the form instead of Unchecked. VS 2010 Express SQL Server CE 3.5 and 4.0 bit data type.[^]
 
Share this answer
 
Comments
Wendelius 6-Jan-19 11:49am    
Useful links!
Maciej Los 6-Jan-19 11:55am    
Thank you, Mika.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900