3

I have a database field value, which is an integer like 0 and 1. Is it possible to convert this integer values to Boolean while loading the data in to a DB Grid. I'm expecting without condition checking, like direct typecasting.

Thanks

8
  • 1
    I don't know about databases, but the question in the title is very easy. If i: integer then i = 1 is a boolean. Commented Oct 5, 2011 at 11:15
  • 8
    @Andreas: i <> 0 is probably a bit more idiomatic and robust. Commented Oct 5, 2011 at 11:21
  • 1
    @Ulrich: You are absolutely right. Commented Oct 5, 2011 at 11:25
  • AFAIK you can not do this. boolean type has 2 possible values, and integer much much more possible values. So, NO you can not. Commented Oct 5, 2011 at 11:51
  • 1
    @RBA - MySQL, for example, does not have a "boolean" type (it uses tinyint(1) to store truth values.) So long as semantics are the same, you are safe using if (val <> 0) then true else false or something like that. Commented Oct 5, 2011 at 13:35

5 Answers 5

5

There is no way to convert Integer to Boolean. you can implement a function like this

function IntToBool(const AnInt: Integer): Boolean;
begin
   if AnInt = 0 then Result := False
                else Result := True;     
end;
Sign up to request clarification or add additional context in comments.

1 Comment

Or simpler: Result := AnInt <> 0; Alternatively Result := LongBool(AnInt);
3

I guess that you want to show a database field in DBGrid as a CheckBox. If so, read article by Zarko Gajic. It is about Boolean fields, but you can easily modify the code for your needs.

Comments

1

The most simple solution to your problem would probably be to use a boolean calcfield.

If you need to edit it from the DBGrid, it gets a little bit more tricky (But still possible).

Comments

1

If you want to show the words "True" and "False" in DBGrid, you should use OnGetText event of Field like this :

procedure TMyForm.MyDataSetFieldGetText(Sender: TField;
  var Text: string; DisplayText: Boolean);
begin
 case Sender.AsInteger of
  0 : Text := 'False';
  1 : Text := 'True';
 else
  Text := '-';
 end;
end;

Comments

0

try this sample function here:

 function IntToBooleanStr(AInteger: Integer): string;
 begin
   case AInteger of
     0:begin
         Result := 'False';
       end;
     1:begin
         Result := 'True';
       end 
       else
           Result := 'False';
   end;
 end;

that's all and you can use it inside the combobox onChange Event for FILTERING Some Logical Data that has the boolean values inside.
like here:

procedure TFrm_Books.ComBox_AvailableFilterChange(Sender: TObject);
begin   
   Table_Book.Filtered := False;
   Table_Book.FilterOptions := [foCaseInsensitive];
   Table_Book.Filter := '';
   Table_Book.Filter := 'Available = ' + IntToBooleanStr(ComBox_AvailableFilter.ItemIndex);
   Table_Book.Filtered := True;
end;

and here is the DFM Code for this combobox:

object ComBox_AvailableFilter: TComboBox
  Left = 336
  Top = 120
  Width = 193
  Height = 21
  ItemHeight = 13
  Items.Strings = (
    'Not Available'
    'Available')
  TabOrder = 0
end

i hope this function resolve your question Above.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.