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
Erik NAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

kretzschmarCommented:
hi erik,

you can do something like this

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

meikl
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
kretzschmarCommented:
appendix

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

meikl
0
kretzschmarCommented:
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
0
JavaScript Best Practices

Save hours in development time and avoid common mistakes by learning the best practices to use for JavaScript.

EpsylonCommented:
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;
0
kretzschmarCommented:
hi eps,
long time not seen you,
how are you ?
meikl
0
AttarSoftwareCommented:
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.
0
Erik NAuthor Commented:
Great answers !

Thanx !
0
EpsylonCommented:
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.

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.