Delphi - Dbgrid does not replace first line content

Asked

Viewed 381 times

2

Good morning, everyone...

I have a table in an Sqlite database with an "integer" type field to store "Boolean" values. This field is linked, on the registration screen, to a Dbcheckbox. So, to be able to mark and unmark the Dbcheckbox, I needed to write the Ongettext and Onsettext events from the field.

Now I need to show "Yes" and "No" values based on this field in a Dbgrid. I wrote the code below in the Ondrawcolumncell event:

procedure TfrmPesquisar.dbgPesquisarDrawColumnCell(Sender: TObject;
  const Rect: TRect; DataCol: Integer; Column: TColumn; State: TGridDrawState);
begin
   if Column.Field = dsPesquisar.DataSet.FieldByName('claAtiva') then
  begin
    dbgPesquisar.Canvas.FillRect(Rect);
    if Column.Field.AsString = '0' then
      dbgPesquisar.Canvas.TextOut(Rect.Left+10,Rect.Top+3,'Não')
    else
      dbgPesquisar.Canvas.TextOut(Rect.Left+10,Rect.Top+3,'Sim');
  end;
end;

However, only the first line is showing, erroneously, the value to be replaced (No) and the real value (0 - zero), as shown below:

inserir a descrição da imagem aqui

Has anyone gone through something similar or knows the solution ?

Thank you so much for your help.

  • I had not noticed... The two data are presented on the selected line. Then if I select the second or third line the data "Yes" and "1".

  • And if you create a Calculated Field in the Dataset that feeds this Grid?

  • Try to add at the end of the code, after the IF: dbgPesquisar.DefaultDrawDataCell(Rect,Column.Field,State);

  • Andrey. When this line is added, the display of the information is reversed. In all three lines appear the real values (0, 1, 1) and in the selected row also appears "Yes" or "No" depending on the value (whether it is 0 or 1).

  • @Junior Moreira. I am not using Fields Editor because it is a generic screen and the data is brought through an SQL statement in a Tfdquery. So I guess you can’t do it that way.

  • @Andrey. When this line is added, the display of the information is reversed. In all three lines appear the real values (0, 1, 1) and in the selected row also appears "Yes" or "No" depending on the value (whether it is 0 or 1).

Show 1 more comment

2 answers

1


I suggest then move to the SQL query itself resolve:

something like:

CASE Ativa
 WHEN '0' THEN
 'Não'
 ELSE
 'Sim'
 END Ativa

This way the result is ready to be displayed.

  • Thanks Junior !!! Problem solved. It worked fine. Thank you very much.

0

Não Sei se funciona no Delphi para Android more on Desktop I use Ongettext

Example

if QryPesquisaclaAtiva.AsString = '0' 
then Text:='Não'
else Text:='Sim';
  • Hi Paulo. Thanks for the return. I forgot to mention... These fields displayed in Dbgrid are not persistent. They are the result of an SQL query of a Tfdquery. That is, they were not added to the Tfdquery via the Fields Editor. So we can’t release the events Ongettext and Onsettext., correct ?

  • Hi Paulo. Thanks for the return. I forgot to mention... These fields displayed in Dbgrid are not persistent. They are the result of an SQL query of a Tfdquery. That is, they were not added to the Tfdquery via the Fields Editor. So we can’t release the events Ongettext and Onsettext., correct ?

  • Just to understand a little bit, it’s like a calculated field?.

  • It is a generic query screen. Then the fields that will be returned in Dbgrid will be different according to the table I am referring to. That is, I have a Tfdquery in which the SQL statement is always changed. Therefore I cannot use the Fields Editor otherwise the fields become persistent in . DFM and causes an error in the application when changing SQL.. I think that’s it. Hug.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.