Ver Mensaje Individual
  #4  
Antiguo 28-05-2018
Avatar de duilioisola
[duilioisola] duilioisola is offline
Miembro Premium
 
Registrado: ago 2007
Ubicación: Barcelona, España
Posts: 1.734
Reputación: 20
duilioisola Es un diamante en brutoduilioisola Es un diamante en brutoduilioisola Es un diamante en bruto
Este es un ejemplo:
  • Tengo un StringGrid con varias filas y columnas (5x5 en mi ejemplo).
  • La columna 3 contiene fechas
  • Si entro en la columna 3 muestro un DateTimePicker para pedir la fecha de esa celda.
  • Al modificar el DateTimePicker se modifica el valor de la celda

Código Delphi [-]
type
  TFMPruebas = class(TForm)
[...]
     sgPrueba: TStringGrid;
     dtpPrueba: TDateTimePicker;
[...]
     procedure sgPruebaSelectCell(Sender: TObject; ACol, ARow: integer; var CanSelect: boolean);
     procedure dtpPruebaChange(Sender: TObject);
  private
     { Private declarations }
     // En la clase declaro estas variables en la parte privada
     ColumnaSeleccionada : integer;
     FilaSeleccionada : integer;
[...]
end;

procedure TFMPruebas.FormCreate(Sender: TObject);
begin
  inherited;
  // Inicialmente el componente debe estar invisible
  dtpPrueba.Visible := False;
[...]
end;

procedure TFMPruebas.sgPruebaSelectCell(Sender: TObject; ACol, ARow: integer; var CanSelect: boolean);
var
  r : TRect;
begin
  // Posicion de la columna seleccionada en el StringGrid
  r := sgPrueba.CellRect(ACol, ARow);

  with dtpPrueba do
  begin
     // Guardo la fila-columna en la que estoy
     FilaSeleccionada := ARow;
     ColumnaSeleccionada := ACol;

     // Si estoy en la columna que contiene fechas, hago visible el DateTimePicker
     if (ACol = 3) then
     begin
        Left := sgPrueba.Left + sgPrueba.CellRect(ACol, ARow).Left + 2;
        Top := sgPrueba.Top + r.Top + 2;
        Width := r.Right - r.Left - 2;
        Height := r.Bottom - r.Top;

        Visible := True;
        // Trato de convertir el texto de la celda en fecha. Si no puedo, utilizo la fecha de hoy
        try
           Date := StrToDate(sgPrueba.Cells[ACol, ARow])
        except
           Date := Today;
        end;
     end
     else
        Visible := False;
  end;
end;

procedure TFMPruebas.dtpPruebaChange(Sender: TObject);
begin
  // Modifico el valor de la celda, con el texto de la fecha
  sgPrueba.Cells[ColumnaSeleccionada, FilaSeleccionada] := DateToStr(dtpPrueba.Date);
end;
Responder Con Cita