Foros Club Delphi

Foros Club Delphi (https://www.clubdelphi.com/foros/index.php)
-   Varios (https://www.clubdelphi.com/foros/forumdisplay.php?f=11)
-   -   Pintar DBGrid dependiendo del Valor de un Registro (https://www.clubdelphi.com/foros/showthread.php?t=84416)

rodrinig 16-10-2013 20:26:22

Pintar DBGrid dependiendo del Valor de un Registro
 
1 Archivos Adjunto(s)
Hola Foro, que tal, tengo un duda de un mal funcionamiento en el DBGrid, (no por el comp, sino por mi programación). Tengo un cds con un campo que tiene un valor y dependiendo de ese valor pongo el color de la fila. En el sig codigo que hice me hace esto , como me muestra en la imagen adjunto. Este es el codigo:

Ojo esto tiene un timer que va actualizando a cada rato el cds .

Código Delphi [-]
  with Sender as TDBGrid do
  begin
    case cdsDes['OR_TIPO'] of
      1: begin
           Canvas.Brush.Color := tCOLOR(SELF.cdsDesTIO_COLOR.AsString);
           DefaultDrawColumnCell(Rect, DataCol, Column, State);
         end;
      2: begin
           Canvas.Brush.Color := tCOLOR(SELF.cdsDesTIO_COLOR.AsString);
           DefaultDrawColumnCell(Rect, DataCol, Column, State);
         end;
      3: begin
           Canvas.Brush.Color := tCOLOR(SELF.cdsDesTIO_COLOR.AsString);
           DefaultDrawColumnCell(Rect, DataCol, Column, State);
         end;
      5: begin
           Canvas.Brush.Color := tCOLOR(SELF.cdsDesTIO_COLOR.AsString);
           DefaultDrawColumnCell(Rect, DataCol, Column, State);
         end;
      6: begin
           Canvas.Brush.Color := tCOLOR(SELF.cdsDesTIO_COLOR.AsString);
           DefaultDrawColumnCell(Rect, DataCol, Column, State);
         end;
    else
         begin
           Canvas.Brush.Color := tCOLOR(SELF.cdsDesTIO_COLOR.AsString);
           DefaultDrawColumnCell(Rect, DataCol, Column, State);
         end;

Miren que busque pero no puedo hacerlo funcionar.
Muchas gracias por su atencion

Saludos
Rodrigo Iñiguez

ecfisa 16-10-2013 21:34:53

Hola rodrinig.

Crea una nueva aplicación, poné un TClientDataSet, un TDataSource, un TTimer un TDBGrid y en tiempo de diseño hace lo siguiente:
  • Doble click sobre el TClientDataSet
  • Click botón derecho -> Click sobre "New Field..."
  • Name: OR_TIPO, Type: Integer
  • Click sobre botón OK.

Luego proba este ejemplo:
Código Delphi [-]
...
implementation

const
  MAX = 200;

procedure TForm1.FormCreate(Sender: TObject);
var
  i: Integer;
begin
  Timer1.Enabled := False;
  Randomize;
  with ClientDataSet1 do
  begin
    CreateDataSet;
    Open;
    for i:= 1 to MAX do
    begin
      Append;
      FieldByName('OR_TIPO').AsInteger := Random(5)+1;
      Post;
    end;
    First;
  end;
  Timer1.Interval := 3000;
  Timer1.Enabled  := True;
end;

procedure TForm1.Timer1Timer(Sender: TObject);
var
  i: Integer;
begin
  Timer1.Enabled := False;
  with ClientDataSet1 do
  begin
    DisableControls;
    try
      First;
      for i:= 1 to MAX do
      begin
        Edit;
        FieldByName('OR_TIPO').AsInteger := Random(5)+1;
        Next;
      end;
      First;
    finally
      EnableControls;
    end;
  end;
  Timer1.Enabled := True;
end;

procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
  DataCol: Integer; Column: TColumn; State: TGridDrawState);
var
  C : TColor;
begin
  with Sender as TDBGrid do
  begin
    case ClientDataSet1.FieldByName('OR_TIPO').AsInteger of
      1: C:= clLime;
      2: C:= clYellow;
      3: C:= clMoneyGreen;
      4: C:= clRed;
      5: C:= clAqua;
    end;
    Canvas.Brush.Color := C;
    DefaultDrawColumnCell(Rect, DataCol, Column, State);
  end;
end;
...

Saludos :)

Edito: Aunque es obvio, me olvidé de mencionar que asocies el DBgrid al DataSource y este al ClientDataSet para poder visualizar el resultado.


La franja horaria es GMT +2. Ahora son las 14:46:19.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Traducción al castellano por el equipo de moderadores del Club Delphi