Link to home
Start Free TrialLog in
Avatar of Erik N
Erik N

asked on

Inserting and deleting string from a stringgrid..

Is there some simple method for using insert and delete-functions in a stringgrid ? I just want to delete and insert like using recordsets in a TDBgrid. Of course I can do this by delete a string in a  stringlist, and then mirror the content of the stringlist into the stringgrid, but this require some work, wich I am not willing to do. If I just delete a row like this:

StringGrid1.Rows[3].Clear;

this row will still be there, even if it is empty.


It doesn't seem to be convenient to use the stringgrid-component at all...

Thanx !
Erik N
ASKER CERTIFIED SOLUTION
Avatar of kretzschmar
kretzschmar
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
appendix

this (above) deletes the line where the focus is.

meikl
hi again erik,

for inserting

procedure TForm1.Button2Click(Sender: TObject);
var i,j : Integer;
begin
  StringGrid1.RowCount := StringGrid1.RowCount + 1;
  for I := stringgrid1.RowCount - 1 Downto stringgrid1.Row + 1 do
    for J := 0 to stringgrid1.ColCount -1 do
      StringGrid1.Cells[J,I] := StringGrid1.Cells[J,I-1];
  stringgrid1.Rows[stringgrid1.Row].Clear;
end;

this (above) inserts a line where the focus is.

meikl
Avatar of Epsylon
Epsylon

Here's both insert and remove, also using the focused row as reference:

procedure TForm1.InsertClick(Sender: TObject);
var x, y: Integer;
begin
  with StringGrid1 do
  begin
    rowcount := rowcount + 1;
    for y := RowCount - 1 downto Row + 1 do
      for x := 0 to ColCount - 1 do
         Cells[x, y] := Cells[x, y - 1];
    Rows[row].Clear;
  end;
end;

procedure TForm1.RemoveClick(Sender: TObject);
var x, y: Integer;
begin
  with StringGrid1 do
  begin
    for y := Row + 1 to RowCount - 1 do
      for x := 0 to ColCount - 1 do
         Cells[x, y - 1] := Cells[x, y];
    rowcount := rowcount - 1;
  end;
end;
hi eps,
long time not seen you,
how are you ?
meikl
I have found tStringGrid to be a pain in the butt.  Here, we use a tHeaderControl with an OwnerDraw listbox, where each item is either tab (#9) delimited, or they have an object tied to them (using addObject), in which all of the values reside...

If you want the editors to work, to be able to edit the strings, then some fancy OnMouseDown event code is needed to make editboxes or comboboxes appear in the right place at the right time...

Tricky, I know, but as I say we have had far too many problems with tStringGrid (grid Index out of range, deleting/inserting rows, moving rows, moving columns, etc...)

Just thought I would point you in another possible direction...

Good luck,

Tim.
Avatar of Erik N

ASKER

Great answers !

Thanx !
Hi Meikl, I'm fine, thanks.

I'm very busy at work so I have little time to spare for E-E. I have been answering/commenting a few questions here the past time so I think we just missed each other...

Eps.