Excelの「Cell右クリック行挿入」のようにデータの入ったStringGridの行に空白の行を挿入追加したいのですが?
宜しくお願いいたします。
TStringGridには行を挿入するメソッドは用意されていないので、
Colsプロパティなどを利用したコードを書いて目的の操作をすることに。
メソッドが無いとの事で自前で考えました
Colsがポイントとのアドバイスで実現出来ました
有難うございました
以前実装したことがあるので、記載しておきます。
このようにして使ってください。
procedure TForm1.Button1Click(Sender: TObject);
begin
TStrGridAccess(StringGrid1).DeleteRow(0);
//0行目が削除される
end;
--------------
implementation
type
TStrGridAccess = class(TStringGrid)
public
procedure InsertColumn(ACol: Integer);
procedure InsertRow(ARow: Integer);
procedure DeleteColumn(ACol: Longint); override;
procedure DeleteRow(ARow: Longint); override;
end;
procedure TStrGridAccess.DeleteColumn(ACol: Integer);
var
GridRect1: TGridRect;
LeftColBuff: Longint;
begin
if (ACol < 0) or (ColCount - 1 < ACol) then
begin
raise EAccessViolation.Create('Grid削除列指定が間違っています');
end;
GridRect1 := Self.Selection;
LeftColBuff := LeftCol;
if ACol < GridRect1.Left then
begin
GridRect1.Left := GridRect1.Left -1;
GridRect1.Right := GridRect1.Right -1;
end else
if (GridRect1.Left <= ACol) and (ACol <= GridRect1.Right) then
begin
if GridRect1.Left <> GridRect1.Right then
GridRect1.Right := GridRect1.Right -1;
end else
if (GridRect1.Right < ACol) then
begin
end;
Cols[ACol].Text := '';
inherited DeleteColumn(ACol);
LeftCol := LeftColBuff;
Self.Selection := GridRect1;
end;
procedure TStrGridAccess.DeleteRow(ARow: Integer);
var
GridRect1: TGridRect;
TopRowBuff: Longint;
begin
if (ARow < 0) or (RowCount - 1 < ARow) then
begin
raise EAccessViolation.Create('Grid削除行指定が間違っています');
end;
GridRect1 := Self.Selection;
TopRowBuff := TopRow;
if ARow < GridRect1.Top then
begin
GridRect1.Top := GridRect1.Top -1;
GridRect1.Bottom := GridRect1.Bottom -1;
end else
if (GridRect1.Top <= ARow) and (ARow <= GridRect1.Bottom) then
begin
if GridRect1.Top <> GridRect1.Bottom then
GridRect1.Bottom := GridRect1.Bottom -1;
end else
if (GridRect1.Bottom < ARow) then
begin
end;
Rows[ARow].Text := '';
inherited DeleteRow(ARow);
TopRow := TopRowBuff;
Self.Selection := GridRect1;
end;
procedure TStrGridAccess.InsertColumn(ACol: Integer);
var
GridRect1: TGridRect;
begin
if (ACol < 0) or (ColCount-1 < ACol) then
begin
raise EAccessViolation.Create('Grid挿入列指定が間違っています');
end;
GridRect1 := Selection;
if ACol <= GridRect1.Left then
begin
GridRect1.Left := GridRect1.Left +1;
GridRect1.Right := GridRect1.Right +1;
end else
if (GridRect1.Left < ACol) and (ACol <= GridRect1.Right) then
begin
GridRect1.Right := GridRect1.Right +1;
end else
if (GridRect1.Right < ACol) then
begin
end;
ColCount := ColCount + 1;
MoveColumn(ColCount-1, ACol);
Self.Selection := GridRect1;
end;
procedure TStrGridAccess.InsertRow(ARow: Integer);
var
GridRect1: TGridRect;
begin
if (ARow < 0) or (ColCount-1 < ARow) then
begin
raise EAccessViolation.Create('Grid挿入行指定が間違っています');
end;
GridRect1 := Selection;
if ARow <= GridRect1.Top then
begin
GridRect1.Top := GridRect1.Top +1;
GridRect1.Bottom := GridRect1.Bottom +1;
end else
if (GridRect1.Top < ARow) and (ARow <= GridRect1.Bottom) then
begin
GridRect1.Bottom := GridRect1.Bottom +1;
end else
if (GridRect1.Bottom < ARow) then
begin
end;
RowCount := RowCount + 1;
MoveRow(RowCount-1, ARow);
Self.Selection := GridRect1;
end;
ツイート | ![]() |