LuckyLuke82 LuckyLuke82 - 1 year ago 155 Question

Binding checkbox to Datatable with YES/NO values

I have checkboxes that needs to be bound to a DataTable. Problem is that checkboxes store "YES" and "NO" values in Oracle DB fields, NOT Boolean. How can I bind to those values ?

This doesn't work:

ChkInUse.DataBindings.Add("Checked", dtb, "IN_USE")

I get error: "String was not recognized as a valid Boolean".

EDIT (My full code):

Dim SQL As String = "SELECT * from MyTable WHERE ID=" & Form1.DataGridView1.CurrentRow.Cells(0).Value.ToString

Dim dtb As New DataTable()

Using con As OracleConnection = New OracleConnection("Data Source=MyDB;User Id=Lucky;Password=MyPassword;")



Using dad As New OracleDataAdapter(SQL, con)
End Using

ChkInUse.DataBindings.Add("Checked", dtb, "IN_USE")

Answer Source

Since you are just loading one row, I would just set and fetch it manually:

' set the check
chkInUse.Checked = (dtb.Rows(0).Field(Of String)("IN_USE") = "YES")

(dtb.Rows(0).Field(Of String)("IN_USE") = "YES") is an expression resulting in a Boolean which is then used to set the CheckState.

Then in the check changed event:

' early exit if there is no datatable yet or no rows
If dtb Is Nothing OrElse dtb.Rows.Count < 1 Then Return
dtb.Rows(0)("INUSE") = If(chkInUse.Checked, "YES", "NO")

This does the reverse: sets the table text to "YES"|"NO" depending on the checked state.

The table would have to have form level scope (that is not clear in the question) and you will need to check if the table is valid and if there are any rows at all in the table (shown).

If you were dealing with multiple rows in the table and perhaps displaying it in a DataGridView, you could do something very similar in the CellFormatting and CellParsing events to translate back and forth from YES to True/Checked.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download