martes, 24 de febrero de 2009

borrar una fila de un stringgrid

//*****
ponemos un stringgrid y un boton en el formulario

y usaremos una variable global que se llama FILA para saber que fila se tiene que borrar cuando pulse el boton



//***********************************************


unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Grids;

type
TForm1 = class(TForm)
StringGrid1: TStringGrid;
Button1: TButton;
procedure FormActivate(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure StringGrid1SelectCell(Sender: TObject; ACol, ARow: Integer;
var CanSelect: Boolean);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

//****** variable global
fila: integer;

implementation

{$R *.dfm}

procedure TForm1.FormActivate(Sender: TObject);
begin

fila:=-1;

with StringGrid1 do
begin
// Título de las columnas
Cells[1, 0] := 'NOMBRE';
Cells[2, 0] := 'APELLIDO1';
Cells[3, 0] := 'APELLIDO2';
Cells[4, 0] := 'NIF';
Cells[5, 0] := 'IMPORTE PTE.';

// Datos
Cells[1, 1] := 'PABLO';
Cells[2, 1] := 'GARCIA';
Cells[3, 1] := 'MARTINEZ';
Cells[4,1] := '67348321D';
Cells[5,1] := '1500,36';

// Datos
Cells[1, 2] := 'MARIA';
Cells[2, 2] := 'SANCHEZ';
Cells[3, 2] := 'PALAZON';
Cells[4, 2] := '44878234A';
Cells[5, 2] := '635,21';

// Datos
Cells[1, 3] := 'CARMEN';
Cells[2, 3] := 'PEREZ';
Cells[3, 3] := 'GUILLEN';
Cells[4, 3] := '76892693L';
Cells[5, 3] := '211,66';
end;


end;

procedure GridDeleteRow(RowNumber: Integer; Grid: TstringGrid);
var
i: Integer;
begin



Grid.Row := RowNumber;

for i := RowNumber to Grid.RowCount - 1 do
Grid.Rows[i] := Grid.Rows[i + 1];

Grid.RowCount := Grid.RowCount - 1;


end;

procedure TForm1.Button1Click(Sender: TObject);
begin

if (fila<>-1) then
begin

GridDeleteRow(fila, stringGrid1);
fila:=-1;
end;

end;

procedure TForm1.StringGrid1SelectCell(Sender: TObject; ACol,
ARow: Integer; var CanSelect: Boolean);
begin

fila:= ARow;

end;

end.







.

No hay comentarios: