Hola
Bueno ten en cuenta que este codigo es de un novato, asi que no critiques mucho, solo te lo pongo de ejemplo.
Esta es una parte que uso en un sistema de facturacion con stringrid.
Código Delphi
[-]
procedure TFFactura.RJustifyEdit(var ThisEdit : TEdit);
var
Left, Width : Integer;
GString : String;
Rgn : TRect;
TheCanvas : TControlCanvas;
begin
TheCanvas := TControlCanvas.Create;
try
TheCanvas.Control := ThisEdit;
GString := ThisEdit.Text;
Rgn := ThisEdit.ClientRect;
TheCanvas.FillRect(Rgn);
Width := TheCanvas.TextWidth(GString);
Left := Rgn.Right - Width - 1;
TheCanvas.TextRect(Rgn, Left, 0, GString);
finally
TheCanvas.Free;
end;
end;
Procedure GridRemoveRow(StrGrid: TStringGrid; DelRow: Integer);
Var Row: Integer;
begin
If DelRow <= StrGrid.RowCount then
Begin
If (DelRow = StrGrid.RowCount-1) AND (DelRow = 1) then
begin
StrGrid.Rows[DelRow].Clear;
StrGrid.Cells[3,1] := '0';
StrGrid.Cells[4,1] := '0.00';
StrGrid.Cells[5,1] := '0.00';
end
else if DelRow = StrGrid.RowCount - 1 then StrGrid.RowCount := StrGrid.RowCount-1
else
begin
For Row := DelRow To StrGrid.RowCount-2 do
StrGrid.Rows[Row].Assign(StrGrid.Rows[Row+1]);
StrGrid.RowCount := StrGrid.RowCount-1;
end;
End;
end;
procedure TFFactura.CalculaTotales;
var i:integer;
begin
SubTotal := 0;
Impuesto := 0;
Desc := 0;
Total := 0;
For i:=1 to SGFact.RowCount-1 do
SubTotal := SubTotal + StrToFloat(SGFact.Cells[5,i]);
For i:=1 to SGFact.RowCount-1 do
If SGFact.Cells[6,i] = 'Si' then Impuesto := Impuesto + StrToFloat(SGFact.Cells[5,i]);
SubTotal := Round(SubTotal);
Desc := (SubTotal * FLEPorcDesc.Value / 100);
Desc := Round(Desc);
Impuesto := (Impuesto * 0.13);
Impuesto := Round(Impuesto);
Total := (SubTotal - Desc + Impuesto);
SGTotal.Cells[1,1] := Format('%8.2n',[desc]);
SGTotal.Cells[1,2] := Format('%8.2n',[Impuesto]);
SGTotal.Cells[1,0] := Format('%8.2n',[SubTotal]);
SGTotal.Cells[1,3] := Format('%8.2n',[Total]);
end;
Esta es otra:
Código Delphi
[-]
procedure TFFactura.BitBtn2Click(Sender: TObject);
var i : integer;
begin
FSelProdFact:=TFSelProdFact.Create(self);
FSelProdFact.Precio := Label15.Caption;
If (SGFact.RowCount = 2) AND (SGFact.Cells[2,1] = '') then FSelProdFact.Filtro := ''
else
begin
FSelProdFact.Filtro := 'NumSerie <> '+ SGFact.Cells[2,1];
i:=2;
While i < SGFact.RowCount do
begin
FSelProdFact.Filtro := FSelProdFact.Filtro + ' AND NumSerie <> '+SGFact.Cells[2,i];
i:=i+1;
end;
end;
try
FSelProdFact.ShowModal;
finally
If FSelProdFact.Cancela = False then
Begin
If Cuenta > 1 then SGFact.RowCount := SGFact.RowCount + 1;
QTemp.Close;
QTemp.SQL.Text := 'SELECT Descripcion+" "+Categoria+" "+SubCategoria AS Descr, Precio1 FROM Articulos '+
'WHERE CodParte = '+QuotedStr(FSelProdFact.Edit1.Text);
QTemp.Open;
SGFact.Cells[0,cuenta] := FSelProdFact.Edit1.Text;
SGFact.Cells[1,cuenta] := QTemp.Fields[0].AsString;
SGFact.Cells[2,cuenta] := FSelProdFact.Edit2.Text;
SGFact.Cells[3,cuenta] := FSelProdFact.Edit3.Text;
SGFact.Cells[4,cuenta] := Format('%8.2f',[FSelProdFact.FloatEdit1.Value]);
SGFact.Cells[5,cuenta] := Format('%8.2f',[FSelProdFact.FloatEdit1.Value * StrToFloat(SGFact.Cells[3,cuenta])]);
If FSelProdFact.CBIV.Checked then SGFact.Cells[6,cuenta] := 'Si' else SGFact.Cells[6,cuenta] := 'No';
Cuenta := Cuenta +1;
QTemp.Close;
SortGrid(SGFact,0,0);
CalculaTotales;
end;
FSelProdFact.Free;
end;
end;
procedure TFFactura.SGFactDrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
Var
X:Integer;
begin
x:=0;
With Sender As TStringGrid do begin
Case Acol of
0: Begin
SetTextAlign(Canvas.Handle,TA_LEFT);
X:= rect.left+5;
end;
1: Begin
SetTextAlign(Canvas.Handle,TA_LEFT);
X:= rect.left+5;
end;
2: Begin
SetTextAlign(Canvas.Handle,TA_LEFT);
X:= rect.left+5;
end;
3: Begin
SetTextAlign(Canvas.Handle,TA_CENTER);
X:=(rect.right+rect.left) div 2;
end;
4: Begin
SetTextAlign(Canvas.Handle,TA_RIGHT);
X:= rect.right-5;
end;
5: Begin
SetTextAlign(Canvas.Handle,TA_RIGHT);
X:= rect.right-5;
end;
6: Begin
SetTextAlign(Canvas.Handle,TA_CENTER);
X:=(rect.right+rect.left) div 2;
end;
END;
if Arow=0 then
begin
SetTextAlign(Canvas.Handle,TA_CENTER);
X:=(rect.right+rect.left) div 2;
end;
Canvas.textrect(rect,X,rect.top+2,Cells[Acol,Arow]);
end;
end;
No te pongo todo por que es un poco extenso, pero mas o menos obtendrás una idea de esto.
Saludos