Discussion:
Adding rows to a TStringGrid
(too old to reply)
QS Computing
2004-10-13 17:19:05 UTC
Permalink
Hi,

I would like to be able to provide an interface whereby users can add
rows to a TStringGrid through a popup menu. I want to allow adding
rows to the end (I can do this already) and adding a row immediately
before or after the currently selected cell (I don't know how to do
this). Obviously I can do the popup menu and so forth (I'm not a
n00b!), but I can't figure out the adding a row bit. Like I said, I'm
not a n00b, but I've never had to use TStringGrid before!

Any help would be appreciated.

Thanks,
- QS Computing.
Maarten Wiltink
2004-10-13 21:18:48 UTC
Permalink
Post by QS Computing
I would like to be able to provide an interface whereby users can add
rows to a TStringGrid through a popup menu. I want to allow adding
rows to the end (I can do this already) and adding a row immediately
before or after the currently selected cell (I don't know how to do
this). Obviously I can do the popup menu and so forth (I'm not a
n00b!), but I can't figure out the adding a row bit. Like I said, I'm
not a n00b, but I've never had to use TStringGrid before!
IIRC, the grid exposes its content through a two-dimensional cells
property, *and* through one-dimensional projections - rows and columns,
which are TStrings properties. That would allow well-defined inserts
and deletes on them.

Groetjes,
Maarten Wiltink
AlanGLLoyd
2004-10-14 08:39:25 UTC
Permalink
There is a protected method of TCustomGrid which is not exposed in TStringgrid.
You may expose it by declaring a descendant class and typecasting the
stringgrid to that class, Then you can increase the SG.RowCount and move the
last row to where you want it. Use LockWindowUpdate to improve speed of insert
and redrawing ...

type
TMoveRowSG = class(TStringGrid); // declare descendant

procedure TForm1.InsertSGRow(SG : TStringGrid; RowOrigin : integer;
InsertBefore : boolean);
begin
LockWindowUpdate(SG.Handle); // prevent incremental drawing of SG
{add row at end}
SG.RowCount := SG.RowCount + 1;
{move row to defined position}
if InsertBefore then
TMoveRowSG(SG).MoveRow(SG.RowCount - 1, RowOrigin)
else
if RowOrigin < SG.RowCount - 2 then // don't move row to same place
TMoveRowSG(SG).MoveRow(SG.RowCount - 1, RowOrigin + 1);
LockWindowUpdate(0); // redraw all SG now
end;

Alan Lloyd
***@aol.com
QS Computing
2004-10-15 07:51:59 UTC
Permalink
Thanks, that's great.

A couple more questions about TStringGrid...

1. How can I delete the current row through code?

2. In my application, the user can either have the program in "view"
or in "edit" mode. Only in edit mode can the data be edited. Hence to
enter edit mode I add [goEditing, goAlwaysShowEditor, goRowMoving] (I
think) to my StringGrid's Options property. To leave edit mode (and go
back into view mode), I subtract that set from the Options property.
However, when I do this, the current cell remains in edit mode until
it is deselected. This is not desirable behaviour for my application.
Any ideas how to solve this - ie. to remove the editor when the user
goes back into view mode?

Thanks,
- QS Computing.
Maarten Wiltink
2004-10-15 09:03:42 UTC
Permalink
"QS Computing" <***@gmail.com> wrote in message news:***@posting.google.com...
[...]
Post by QS Computing
2. In my application, the user can either have the program in "view"
or in "edit" mode. Only in edit mode can the data be edited. Hence to
enter edit mode I add [goEditing, goAlwaysShowEditor, goRowMoving] (I
think) to my StringGrid's Options property. To leave edit mode (and go
back into view mode), I subtract that set from the Options property.
However, when I do this, the current cell remains in edit mode until
it is deselected. This is not desirable behaviour for my application.
Any ideas how to solve this - ie. to remove the editor when the user
goes back into view mode?
Can you deselect it in code?

Don't start screaming right away; you can leave it focused.

Groetjes,
Maarten Wiltink
J French
2004-10-15 10:09:39 UTC
Permalink
Post by QS Computing
Thanks, that's great.
A couple more questions about TStringGrid...
1. How can I delete the current row through code?
TStringGrid has a protected method: DeleteRow( RowNo )
Post by QS Computing
2. In my application, the user can either have the program in "view"
or in "edit" mode. Only in edit mode can the data be edited. Hence to
enter edit mode I add [goEditing, goAlwaysShowEditor, goRowMoving] (I
think) to my StringGrid's Options property. To leave edit mode (and go
back into view mode), I subtract that set from the Options property.
However, when I do this, the current cell remains in edit mode until
it is deselected. This is not desirable behaviour for my application.
Any ideas how to solve this - ie. to remove the editor when the user
goes back into view mode?
StringGrid1.Options := StringGrid1.Options - [goEditing];
StringGrid1.EditorMode := False;
Post by QS Computing
Thanks,
- QS Computing.
AlanGLLoyd
2004-10-15 11:28:18 UTC
Permalink
Post by QS Computing
1. How can I delete the current row through code?
DeleteRow is also a protected method of TStringGrid so use the same technique -
declare a descendant and typecast. Any descendant of TStringGrid will do, the
one you used for MoveRow. or you may want to rename that for clarity.

TMoveRowSG(StringGrid1).DeleteRow(4);
Post by QS Computing
2. In my application, the user can either have the program in "view"
or in "edit" mode. Only in edit mode can the data be edited. Hence to
enter edit mode I add [goEditing, goAlwaysShowEditor, goRowMoving] (I
think) to my StringGrid's Options property. To leave edit mode (and go
back into view mode), I subtract that set from the Options property.
However, when I do this, the current cell remains in edit mode until
it is deselected. This is not desirable behaviour for my application.
Any ideas how to solve this - ie. to remove the editor when the user
goes back into view mode?
Again InPlaceEditor is a protected property of TStringGrid. So code ...

TMoveRowSG(StringGrid1).InPlaceEditor.Hide;
StringGrid1.Refresh;

You can find the code of TStringGrid and TCustomGrid (its ancestor) in
Grids.pas. Its worth looking at.

Alan Lloyd
***@aol.com

Loading...