Ver Mensaje Individual
  #2  
Antiguo 04-12-2007
bustio bustio is offline
Miembro
 
Registrado: oct 2003
Posts: 231
Reputación: 21
bustio Va por buen camino
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, necesitassi 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.
__________________
Muchas Gracias...
Responder Con Cita