09. december 2006 - 13:05Der er
4 kommentarer og 1 løsning
Slette række i stringgrid
Jeg har 2 problemer med at slette en række i min stringgrid.
For det første får jeg at vide i runtime, når jeg forsøger at slette, at jeg ikek kan slette eller indsætte i min stringgrid. Skal man først slå over i en form for "editmode"?
For det andet har jeg lidt problemer med at finde den rigtige syntaks for at slette en række
ok, jeg har aldrig brugt StringGrid så jeg ved ikke så meget om den men efter at have kikket lidt på den mener jeg ikke at du kan benytte: stringgrid1.Rows[a].Delete(b); Du kan slette en Row ved at skrive: stringgrid1.Rows[a].Clear; men så får du bare en tom Row, for helt at slette denne Row tror jeg du manuelt er nød til at flytte alle Rows under den du vil slette op, men måske tager jeg fejl.
(Og jeg undrer mig meget over, at Borland ikke har åbnet op for muligheden for at slette en række. Mystisk)
--- If you worked with TStringGrid component, then you saw that in this component the Borland developers not provided the method for row deleting. In this tip I describe the few ways for it:
1. navigate by rows and copy the row contains to the prev row:
procedure DeleteRow(yourStringGrid: TStringGrid; ARow: Integer); var i, j: Integer; begin with yourStringGrid do begin for i := ARow to RowCount-2 do for j := 0 to ColCount-1 do Cells[j, i] := Cells[j, i+1]; RowCount := RowCount - 1 end; end;
2. the modificated #1: procedure DeleteRow(yourStringGrid: TStringGrid; ARow: Integer); var i: Integer; begin with yourStringGrid do begin for i := ARow to RowCount-2 do Rows[i].Assign(Rows[i+1]); RowCount := RowCount - 1 end; end;
3. the "hacked" way. The TCustomGrid type (the TStringGrid is TCustomGrid's successor) have the DeleteRow method. But this method allocated not in public section but in protected section. So the all successors can "see" this DeleteRow method.
type THackStringGrid = class(TStringGrid);
procedure DeleteRow(yourStringGrid: TStringGrid; ARow: Integer); begin with THackStringGrid(yourStringGrid) do DeleteRow(ARow); end;
Personally I use the third method but the first and second are more visual.
Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.