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
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;
Result := AnInt <> 0; Alternatively Result := LongBool(AnInt);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.
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;
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.
i: integertheni = 1is a boolean.i <> 0is probably a bit more idiomatic and robust.tinyint(1)to store truth values.) So long as semantics are the same, you are safe usingif (val <> 0) then true else falseor something like that.