Foros Club Delphi

Foros Club Delphi (https://www.clubdelphi.com/foros/index.php)
-   Tablas planas (https://www.clubdelphi.com/foros/forumdisplay.php?f=20)
-   -   Colorear filas intercaladas en DBGRID con BD Access (https://www.clubdelphi.com/foros/showthread.php?t=50979)

mquiroga 04-12-2007 01:08:16

Colorear filas intercaladas en DBGRID con BD Access
 
Les comento que sigo con problemas al colorear filas intercaladas en un DBGRID con BD Access.
El mismo código (abajo mencionado) utilicé con BD Paradox y DBF con conexión BDE, y me funcionó OK.
Ahora estoy utilizando con BD Access(mdb), conexión ODBC y TDatabase y no funciona.

Código Delphi [-]
procedure TForm_CuentasPagar.DBGrid1DrawColumnCell(Sender: TObject;
  const Rect: TRect; DataCol: Integer; Column: TColumn;
  State: TGridDrawState);
var
  HoldColor: TColor;
begin
HoldColor:=DBGrid1.Canvas.Brush.Color;
    If (SQLProveedores.DataSet.RecNo mod 2 = 0) then
    begin
      DBGrid1.Canvas.Brush.Color:=clMoneyGreen;
      DBGrid1.DefaultDrawColumnCell(Rect, DataCol, Column, State);
      DBGrid1.Canvas.Brush.Color:=HoldColor;
    end;
end;

En BD Paradox y DBF, la consulta SQL en: SQLProveedores.DataSet.RecNo me dá como resultado el número de registros de la consulta.

En BD Access, la consulta SQL en: SQLProveedores.DataSet.RecNo es igual a -1, y esto no permite colorear intercalado las filas.

Preciso de su colaboración.
De antemano, muchas gracias por la ayuda.

Max

bustio 04-12-2007 15:22:06

Filas en colores en un DBGRID
 
Hola, mira, si mal no entiendo lo que tu quieres es mostrar un DBGrid con las filas de colores alternos para tratar de identificar mejor los datos, cierto? Bueno, si la cosa es asi, esto debes hacerlo en el DBgrid y es independiente del Gestor de BD que usas. LA cosa esta en el evento OnDrawColumnCell, que se encarga de dibujar la celda y tu si lo interceptas puedes hacer que esta se dibuje tal y como quieres. Aqui te mando un degmento de codigo qeu hace lo que creo, necesitas:(si no lo entiendes me lo dices y te lo comento mejor)

Código Delphi [-]
procedure TfrmListaPedidos.DBGrid1DrawColumnCell(Sender: TObject;
  const Rect: TRect; DataCol: Integer; Column: TColumn;
  State: TGridDrawState);
var
  X, Y, Index, RecNo: integer;
  DataSet: TDataset;
  Field: TField;
  CellText: string;
begin
  with DBGrid1.Canvas do begin
        //Determino el color de fondo
        with Brush do begin
      if gdFocused in State then begin
        Color := clHighlight;
        Font.Color := clHighlightText;
      end else if gdSelected in State then begin
        Color := clHighlight;
        Font.Color := clHighlightText;
      end else if gdFixed in State then begin
        Color := DBGrid1.FixedColor;
      end else begin
        if DataCol = 0 then
          Color := $AACCFF    // color especial para la primera columna que es donde esta la llave de la tabla
        else begin
          // determino el numero de la fila
          if DBGrid1.Datasource <> nil then begin
            DataSet := DBGrid1.Datasource.DataSet;
            if DataSet <> nil then begin
              RecNo := DataSet.RecNo;
              if RecNo = -1 then begin
                RecNo := DataSet.RecordCount + 1;
                if RecNo = 0 then RecNo := 1;
              end;
            end else
              RecNo := 1;
          end else
            RecNo := 1;
          if (RecNo And 1) = 0 then
            Color := $FFFFEE   // Background para las filas impares
          else
            Color := $EEFFFF;  // Background para las filas pares
        end;
      end;
    end;
    Field := Column.Field;
    if Field <> nil then begin
      // hacer
      if Field.FieldName = 'ItemsTotal' then
        if Field.AsCurrency > 10000 then
          with Font do begin
            Color := $FF;
            Style := Style + [fsBold];
          end;
      // mostrar el texto
      DBGrid1.DefaultDrawColumnCell(Rect, DataCol, Column, State);
    end else begin
      // rellenar la celda con el color de fondo
      FillRect(Rect);
      if DataCol = 7 then begin
        // aqui va el texto para las columnas...
        CellText := 'Custom Text';
        // posicion del texto
        case Column.Alignment of
        taRightJustify:
          X := Rect.Right - TextWidth(CellText) - 2;
        taCenter:
          X := (Rect.Right - Rect.Left
               - TextWidth(CellText)) div 2 + Rect.Left;
        else // taLeftJustify:
          X := Rect.Left + 2;
        end;
        // escribo el texto
        TextOut(X, Rect.Top + 2, CellText);
      end else if DataCol = 8 then begin
        // o por ejemplo, dibujo un grafiquito
        // determino la posicion del grafico dentro de la celda
        case Column.Alignment of
        taRightJustify:
          X := Rect.Right - 2 - 16;
        taCenter:
          X := (Rect.Right - Rect.Left - 16) div 2 + Rect.Left;
        else // taLeftJustify:
          X := Rect.Left + 2;
        end;
        Y := (Rect.Bottom - Rect.Top - 16) div 2 + Rect.Top + 1;
      end;
    end;
    if gdFocused in State then    // la celda tiene el focus?
      DBGrid1.Canvas.DrawFocusRect(Rect);  // pinto el focus
  end;
end;

Espero te sirva.


La franja horaria es GMT +2. Ahora son las 04:05:14.

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