how to exchange the value of a cell in dbgrid

Asked

Viewed 535 times

-2

Good afternoon.

I’m trying to change a cell phone T for Sim and F for Não, because the visualization on DBGrid this being T or F as shown in the Table in the database.

I’ve made many attempts like:

procedure TFrm_Cadastro_Feriado.DBGrid1DrawColumnCell(Sender: TObject;
  const Rect: TRect; DataCol: Integer; Column: TColumn;
  State: TGridDrawState);
Var
  S: String;
begin
  if Column.FieldName = 'nome da coluna' then
  with DBGrid1.Canvas do
  begin
    S := Column.Field.AsString;
    begin
      if S = 'T' then
      begin
        S := 'Sim';
      end;
    end;

2 answers

1

Good morning, what you can do is already in SQL that is connected to your Dbgrid, bring the exchanged value of the columns, for example:

SELECT case NOMECOLUNA when 'T' then 'Sim' when 'F' then 'Não' end as NOMECOLUNA FROM TABELA

This is an example using Firebird 2.5, but the logic would be the same for another bank.

  • This way did not work, because alter the general behavior of the table referring to this column, when I do the case I have to make the alias at the end of the command with this does not recognize in the column because it does not exist in the database

1


I used it that way and it worked.

procedure TFrm_Cadastro_Feriado.DBGrid1DrawColumnCell(Sender: TObject;
      const Rect: TRect; DataCol: Integer; Column: TColumn;
      State: TGridDrawState);
    Var
      S: String;
      aRect : TRect;
    begin
      aRect := Rect;
      if Column.FieldName = '**Nome da coluna**' then
        begin
        if Column.Field.AsString = 'T' then
        begin
          DBGrid1.Canvas.FillRect(Rect);
          DrawText(DBGrid1.Canvas.Handle, PChar('Sim'), Length('Sim'), aRect,
          DT_SINGLELINE or DT_LEFT or DT_VCENTER);
        end
        else
        begin
          DBGrid1.Canvas.FillRect(Rect);
          DrawText(DBGrid1.Canvas.Handle, PChar('Não'), Length('Não'), aRect,
          DT_SINGLELINE or DT_LEFT or DT_VCENTER);
        end;

Browser other questions tagged

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