Как удалить строку в StringGrid в run-time?
Falk0ner, вс, 06/07/2008 - 15:34.
а не так:
Можно сделать наследника от TCustomGrid. А у последнего есть метод - DeleteRow.
Автор: Song
Взято с Vingrad.ru http://forum.vingrad.ru
Например удаление текущей строки:
Type TFakeGrid=class(TCustomGrid);
{©Drkb v.3(2007): <a href="http://www.drkb.ru" title="www.drkb.ru">www.drkb.ru</a>,
®Vit (Vitaly Nevzorov) - nevzorov@yahoo.com}
procedure TForm1.MyDelete(Sender: TObject);
begin
TFakeGrid(Grid).DeleteRow(Grid.row);
end;
{©Drkb v.3(2007): <a href="http://www.drkb.ru" title="www.drkb.ru">www.drkb.ru</a>,
®Vit (Vitaly Nevzorov) - nevzorov@yahoo.com}
procedure TForm1.MyDelete(Sender: TObject);
begin
TFakeGrid(Grid).DeleteRow(Grid.row);
end;
Автор: Vit (www.delphist.com, www.drkb.ru, www.unihighlighter.com, www.nevzorov.org)
Взято с Vingrad.ru http://forum.vingrad.ru
Примечание от bur80 (Sources.ru):
Предлагаю в разделе VCL -> StringGrid внести корректировочку в статью "Как удалить строку в StringGrid в run-time", что данный метод(!) будет работать только в случае если форма создаётся вот так:
...
Form1.ShowModal;
...
Form1.ShowModal;
...
а не так:
...
var
fr1 : TForm1;
begin
fr1 := Tform1.Create(Application);
fr1.Show;
...
var
fr1 : TForm1;
begin
fr1 := Tform1.Create(Application);
fr1.Show;
...
{ **** UBPFD *********** by delphibase.endimus.com ****
>> Удаление строки из StringGrid
Удаляет из StringGrid указанную строку, сдвигая остальные.
Зависимости: Grids
Автор: Борис Новгородов (MBo), <a href="mailto:mbo@mail.ru">mbo@mail.ru</a>, Новосибирск
Copyright: MBo
Дата: 27 апреля 2002 г.
***************************************************** }
procedure SGDeleteRow(SG: TStringGrid; RowToDelete: Integer);
var
i: Integer;
begin
with SG do
begin
if (RowToDelete >= 0) and (RowToDelete < RowCount) then
begin
for i := RowToDelete to RowCount - 2 do
Rows[i].Assign(Rows[i + 1]);
RowCount := RowCount - 1;
end;
end;
end;
>> Удаление строки из StringGrid
Удаляет из StringGrid указанную строку, сдвигая остальные.
Зависимости: Grids
Автор: Борис Новгородов (MBo), <a href="mailto:mbo@mail.ru">mbo@mail.ru</a>, Новосибирск
Copyright: MBo
Дата: 27 апреля 2002 г.
***************************************************** }
procedure SGDeleteRow(SG: TStringGrid; RowToDelete: Integer);
var
i: Integer;
begin
with SG do
begin
if (RowToDelete >= 0) and (RowToDelete < RowCount) then
begin
for i := RowToDelete to RowCount - 2 do
Rows[i].Assign(Rows[i + 1]);
RowCount := RowCount - 1;
end;
end;
end;
procedure GridDeleteRow(RowNumber: Integer; Grid: TstringGrid);
var
i: Integer;
begin
Grid.Row := RowNumber;
if (Grid.Row = Grid.RowCount - 1) then
{ On the last row}
Grid.RowCount := Grid.RowCount - 1
else
begin
{ Not the last row}
for i := RowNumber to Grid.RowCount - 1 do
Grid.Rows[i] := Grid.Rows[i + 1];
Grid.RowCount := Grid.RowCount - 1;
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
GridDeleteRow(3, stringGrid1);
end;
Взято с сайта: http://www.swissdelphicenter.ch
var
i: Integer;
begin
Grid.Row := RowNumber;
if (Grid.Row = Grid.RowCount - 1) then
{ On the last row}
Grid.RowCount := Grid.RowCount - 1
else
begin
{ Not the last row}
for i := RowNumber to Grid.RowCount - 1 do
Grid.Rows[i] := Grid.Rows[i + 1];
Grid.RowCount := Grid.RowCount - 1;
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
GridDeleteRow(3, stringGrid1);
end;
Отправить комментарий