gpt4 book ai didi

delphi - 在 StringGrid-Delphi 中删除行

转载 作者:行者123 更新时间:2023-12-03 15:54:45 27 4
gpt4 key购买 nike

我想做一个类似的东西。我的 StringGrid 中有一个列表,我想通过选择单元格然后单击按钮来删除一行。然后该列表应再次显示在 StringGrid 中,但不包含该行。我在删除行时遇到的最大问题是,我尝试了一个过程,但我认为它只删除了 StringGrid 中的行,而不是列表中的行。

    procedure DeleteRow(Grid: TStringGrid; ARow: Integer);
var
i: Integer;
begin
for i := ARow to Grid.RowCount - 2 do
Grid.Rows[i].Assign(Grid.Rows[i + 1]);
Grid.RowCount := Grid.RowCount - 1;
end;

请有人帮忙。 :)

最佳答案

如果您使用的是标准 VCL TStringGrid (不使用最新版本中提供的实时绑定(bind)),您可以使用插入器类来访问 protected TCustomGrid.DeleteRow方法。

下面的代码已经在Delphi 2007中进行了测试。它使用了一个简单的TStringGrid放在表单上,​​具有默认的列和单元格以及标准 TButton

TForm.OnCreate事件处理程序只是用一些数据填充网格,以便更容易查看已删除的行。每次单击按钮单击事件时,都会从字符串网格中删除第 1 行。

注意:代码不会进行错误检查来确保有足够的行。这是一个演示应用程序,而不是生产代码的示例。您的实际代码应在尝试删除行之前检查可用行数。

// Interposer class, named to indicate it's use
type
THackGrid=class(TCustomGrid);

// Populates stringgrid with test data for clarity
procedure TForm1.FormCreate(Sender: TObject);
var
i, j: Integer;
begin
for i := 1 to StringGrid1.ColCount - 1 do
StringGrid1.Cells[i, 0] := Format('Col %d', [i]);
for j := 1 to StringGrid1.RowCount - 1 do
begin
StringGrid1.Cells[0, j] := Format('Row #d', [j]);
for i := 1 to StringGrid1.ColCount - 1 do
begin
StringGrid1.Cells[i, j] := Format('C: %d R: %d', [i, j]);
end;
end;
end;

// Deletes row 1 from the stringgrid every time it's clicked
// See note above for info about lack of error checking code.
procedure TForm1.Button1Click(Sender: TObject);
begin
THackGrid(StringGrid1).DeleteRow(1);
end;

如果您使用的是更新版本,并且已使用实时绑定(bind)将数据附加到网格,则只需从基础数据中删除该行,然后让实时绑定(bind)处理删除该行的操作。

关于delphi - 在 StringGrid-Delphi 中删除行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20425121/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com