Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Add/insert Arow in TStringGrid

Status
Not open for further replies.

niclas73

Programmer
Sep 19, 2005
7
SE
Hi
Is there a way to insert a new row before another in StringGrid1DrawCell??
I loop out posts from a database and whant to add a new row to group the posts like:

Team 1 // this is a new added row
employee1
employee2
employee3
Team 3 // this is a new added row
employee4
employee5
...


Thanks
Niclas
 
Here's my generic code to add a line into a TStringGrid:
--------------------------------------------------------
procedure LineAdd();
var
sRowNext: Array of string;
sRowPrev: Array of string;
iIndex, iRow, iCol: Integer;
begin
iIndex := Row;
RowCount := RowCount + 1;
SetLength(sRowNext, ColCount);
SetLength(sRowPrev, ColCount);

for iCol := 0 to ColCount - 1 do
sRowNext[iCol] := '';

for iRow := iIndex to RowCount-1 do
for iCol := 0 to ColCount-1 do
begin
sRowPrev[iCol] := Cells[iCol, iRow];
Cells[iCol, iRow] := sRowNext[iCol];
sRowNext[iCol] := sRowPrev[iCol];
end;
sRowNext := nil;
sRowPrev := nil;
end;
--------------------------------------------------------
Hope this helps,

Rej Cloutier
 
Hi and thanks
I solved it like this:

in my database while loop I put this line of code:

-------------------
if new teamgroup blablba then
UpdateGrid(StringGrid2.RowCount + 1);
-------------------

procedure TframAdmSetupSchedule_2.UpdateGrid(rowCount: integer);
begin
//Update rowcount
StringGrid2.RowCount := rowCount;

//If rowcount < 2 fixedrows is set to 0, so we have to set it back if rowcount grows:
if StringGrid2.RowCount > 1 then
StringGrid2.FixedRows := 1;

//Invalidate grid:
StringGrid2.Invalidate;
end;

//niclas
 
niclas,

your code will just add the specified number of rows to the end of the grid, if that's all you want then that's okay, but your original question implied that you want to insert rows before the last row.

Rej, your LineAdd does the right thing, but if you use the Rows property of TStringGrid rather than Cells then it becomes much easier. e.g. This will insert a blank line at a specified position

Code:
procedure LineAdd(aStringGrid: TStringGrid; aPos: integer);
var
  iIndex, iRow, iCol: Integer;
begin
  aStringGrid.RowCount := aStringGrid.RowCount + 1;
  for iRow := aStringGrid.RowCount-1 downto aPos do
    aStringGrid.Rows[iRow] := aStringGrid.Rows[iRow-1];
  aStringGrid.Rows[aPos].Clear;
end;

It would be very easy to extend this to specify the number of rows to add

Steve
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top