{"id":1490,"date":"2013-10-03T15:07:21","date_gmt":"2013-10-03T05:07:21","guid":{"rendered":"http:\/\/www.malcolmgroves.com\/blog\/?p=1490"},"modified":"2014-07-02T16:42:31","modified_gmt":"2014-07-02T06:42:31","slug":"livebindings-how-to-treat-an-integer-field-as-a-boolean","status":"publish","type":"post","link":"http:\/\/www.malcolmgroves.com\/blog\/?p=1490","title":{"rendered":"LiveBindings : How to treat an Integer field as a Boolean?"},"content":{"rendered":"<p>I had a question recently from someone, and it seemed a common enough requirement that I thought the solution might be worth sharing.<\/p>\n<p>Let&#8217;s say you&#8217;ve got a database that has an Integer column which is really used as a Boolean. Zero for False, One for True. Further, for whatever reason, you can&#8217;t change the DB structure. How do you display this column to the end user as a boolean, so that they can use a Checkbox column in a grid, a TSwitch control, or similar to edit it?<!--more--><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-1497 alignnone\" style=\"line-height: 1.6;\" src=\"http:\/\/www.malcolmgroves.com\/blog\/wp-content\/uploads\/2013\/10\/LiveBindingsIntegerAsBoolean3.png\" alt=\"LiveBindingsIntegerAsBoolean3\" width=\"302\" height=\"362\" srcset=\"http:\/\/www.malcolmgroves.com\/blog\/wp-content\/uploads\/2013\/10\/LiveBindingsIntegerAsBoolean3.png 302w, http:\/\/www.malcolmgroves.com\/blog\/wp-content\/uploads\/2013\/10\/LiveBindingsIntegerAsBoolean3-250x300.png 250w\" sizes=\"auto, (max-width: 302px) 100vw, 302px\" \/><br \/>\nNote, t<span style=\"line-height: 1.6;\">his could also apply to binding from something other than a<\/span><\/p>\n<p>database, such as a list of objects (see my post\u00a0<a title=\"LiveBindings in XE3: TAdapterBindSource and binding to Objects\" href=\"http:\/\/www.malcolmgroves.com\/blog\/?p=1084\">TAdapterBindSource and binding to Objects<\/a>), but the solution applies anyway.<\/p>\n<p>You could of course do this at the dataset field level, using the <a href=\"http:\/\/docwiki.embarcadero.com\/Libraries\/XE5\/en\/Data.DB.TField.OnGetText\" target=\"_blank\">TField.OnGetText<\/a>\u00a0and <a href=\"http:\/\/docwiki.embarcadero.com\/Libraries\/XE5\/en\/Data.DB.TField.OnSetText\" target=\"_blank\">TField.OnSetText<\/a>\u00a0fields to convert the values back and forth, but let&#8217;s also look at what options the LiveBinding framework gives us.<\/p>\n<p>I actually covered the key parts of the solution in my post <a title=\"LiveBindings in XE3: Formatting your Fields\" href=\"http:\/\/www.malcolmgroves.com\/blog\/?p=1226\">LiveBindings in XE3: Formatting your Fields<\/a>. In that post I used the CustomFormat and CustomParse expression in my links to add Currency formatting before displaying a field, and subsequently remove the Currency formatting on the way back to the field. We can use the same technique to actually change the datatype back and forth.<\/p>\n<p>If you have a look at the <a href=\"https:\/\/github.com\/malcolmgroves\/delphi-samples\/tree\/master\/LiveBindings\/Treating%20an%20Integer%20as%20a%20Boolean\" target=\"_blank\">sample code<\/a>, I&#8217;ve got a ClientDataset with one field of type TSmallIntField, which I&#8217;ve imaginatively titled IntegerField. That is hooked up through a <a href=\"http:\/\/docwiki.embarcadero.com\/Libraries\/XE5\/en\/Data.Bind.DBScope.TBindSourceDB\" target=\"_blank\">TBindSourceDB<\/a> to a <a href=\"http:\/\/docwiki.embarcadero.com\/Libraries\/XE5\/en\/FMX.Grid.TGrid\" target=\"_blank\">TGrid<\/a>. The <a href=\"http:\/\/docwiki.embarcadero.com\/Libraries\/XE5\/en\/Data.Bind.Grid.TLinkGridToDataSource\" target=\"_blank\">TLinkGridToDataSource<\/a> that is connecting the data to the TGrid has two columns. In the first I want to display the field as a Boolean, using a checkbox column, so I set its <a href=\"http:\/\/docwiki.embarcadero.com\/Libraries\/XE5\/en\/Data.Bind.Grid.TLinkGridToDataSourceColumn.ColumnStyle\" target=\"_blank\">ColumnStyle<\/a> property to CheckColumn. In the second I&#8217;ll display the raw Integer value from the field so I leave the Column properties as default.<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" wp-image-1496 alignnone\" style=\"margin-left: 10px; margin-right: 10px;\" src=\"http:\/\/www.malcolmgroves.com\/blog\/wp-content\/uploads\/2013\/10\/LiveBindingsIntegerAsBoolean1.png\" alt=\"LiveBindingsIntegerAsBoolean1\" width=\"521\" height=\"261\" srcset=\"http:\/\/www.malcolmgroves.com\/blog\/wp-content\/uploads\/2013\/10\/LiveBindingsIntegerAsBoolean1.png 521w, http:\/\/www.malcolmgroves.com\/blog\/wp-content\/uploads\/2013\/10\/LiveBindingsIntegerAsBoolean1-300x150.png 300w\" sizes=\"auto, (max-width: 521px) 100vw, 521px\" \/><\/p>\n<p>The bindings are pretty straight forward, but note the two columns are both bound to the one field.<\/p>\n<p>We still need to set up the logic to convert between the Integer and Boolean.\u00a0If we go back to the same column where we set the ColumnStyle to CheckColumn, we can use expressions in the CustomFormat and CustomParse properties to alter the value on the way from the field to the column (CustomFormat) and on the way back from the column to the field (CustomParse). In the screenshot below you can see the expressions I&#8217;ve used.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-1498 alignnone\" src=\"http:\/\/www.malcolmgroves.com\/blog\/wp-content\/uploads\/2013\/10\/LiveBindingsIntegerAsBoolean2.png\" alt=\"LiveBindingsIntegerAsBoolean2\" width=\"350\" height=\"244\" srcset=\"http:\/\/www.malcolmgroves.com\/blog\/wp-content\/uploads\/2013\/10\/LiveBindingsIntegerAsBoolean2.png 350w, http:\/\/www.malcolmgroves.com\/blog\/wp-content\/uploads\/2013\/10\/LiveBindingsIntegerAsBoolean2-300x209.png 300w\" sizes=\"auto, (max-width: 350px) 100vw, 350px\" \/><\/p>\n<p>In both cases, %s represents the value prior to any manipulation. I think the expressions are relatively straightforward, although perhaps the IfThen statement needs explanation. It tests whether the value of %s is True. If it is, it sets the field value to 1, otherwise it sets it to 0.<\/p>\n<p>&nbsp;<\/p>\n<p>A few things to note:<\/p>\n<ul>\n<li>In this particular case, you can remove the CustomFormat expression entirely, as the LiveBinding engine will \u00a0cope with this datatype conversion automatically. I&#8217;ve included it here to show what would be required in the general case.<\/li>\n<li>This actually treats any non-zero value as True. You can test this by editing the value in the second column and seeing how the Checkbox in the first column responds.<\/li>\n<\/ul>\n<p>Big thanks to <a href=\"http:\/\/blogs.embarcadero.com\/jimtierney\/\" target=\"_blank\">Jim Tierney<\/a> for patiently listening to my ill-informed ideas for a solution to this problem before gently reminding me of a simpler, easier solution that was staring me in the face.<\/p>\n<p>Update: Mathias makes a great point in the comments below. This solution is tied to the binding link. If you need to display this in multiple places in your UI, you&#8217;ll be repeating these expressions every time. Check out the link to his custom TValueRefConverterFactory.<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I had a question recently from someone, and it seemed a common enough requirement that I thought the solution might be worth sharing. Let&#8217;s say you&#8217;ve got a database that has an Integer column which is really used as a Boolean. Zero for False, One for True. Further, for whatever reason, you can&#8217;t change the [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[116],"tags":[64,19,48,89],"class_list":["post-1490","post","type-post","status-publish","format-standard","hentry","category-livebindings","tag-cbuilder","tag-delphi","tag-embarcadero","tag-livebindings"],"_links":{"self":[{"href":"http:\/\/www.malcolmgroves.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1490","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.malcolmgroves.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.malcolmgroves.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.malcolmgroves.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/www.malcolmgroves.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1490"}],"version-history":[{"count":24,"href":"http:\/\/www.malcolmgroves.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1490\/revisions"}],"predecessor-version":[{"id":1600,"href":"http:\/\/www.malcolmgroves.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1490\/revisions\/1600"}],"wp:attachment":[{"href":"http:\/\/www.malcolmgroves.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1490"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.malcolmgroves.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1490"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.malcolmgroves.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1490"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}