[Delphi (Object Pascal)] Delphi想TStringGrid中添加和删除行的方法 →→→→→进入此内容的聊天室

来自 , 2019-05-25, 写在 Delphi (Object Pascal), 查看 154 次.
URL http://www.code666.cn/view/5e98d23a
  1. {...}
  2. type
  3.   TForm1 = class(TForm)
  4.     StringGrid1: TStringGrid;
  5.     Button1: TButton;
  6.     procedure Button1Click(Sender: TObject);
  7.   private
  8.     {...}
  9.   public
  10.     {...}
  11.   end;
  12. //from http://www.sharejs.com
  13. type
  14.   TStringGridHack = class(TStringGrid)
  15.   protected
  16.     procedure DeleteRow(ARow: Longint); reintroduce;
  17.     procedure InsertRow(ARow: Longint);
  18.   end;
  19.  
  20. var
  21.   Form1: TForm1;
  22.  
  23. implementation
  24.  
  25. {$R *.DFM}
  26.  
  27. procedure TStringGridHack.DeleteRow(ARow: Longint);
  28. var
  29.   GemRow: Integer;
  30. begin
  31.   GemRow := Row;
  32.   if RowCount  FixedRows + 1 then
  33.     inherited DeleteRow(ARow)
  34.   else
  35.     Rows[ARow].Clear;
  36.   if GemRow then Row := GemRow;
  37. end;
  38.  
  39. procedure TStringGridHack.InsertRow(ARow: Longint);
  40. var
  41.   GemRow: Integer;
  42. begin
  43.   GemRow := Row;
  44.   while ARow do Inc(ARow);
  45.   RowCount := RowCount + 1;
  46.   MoveRow(RowCount - 1, ARow);
  47.   Row := GemRow;
  48.   Rows[Row].Clear;
  49. end;
  50.  
  51. procedure TForm1.Button1Click(Sender: TObject);
  52. begin
  53.   // Insert Row, Zeile hinzufügen
  54.   TStringGridHack(StringGrid1).InsertRow(1);
  55.   // Remove Row, Zeile entfernen
  56.   TStringGridHack(StringGrid1).DeleteRow(2);
  57. end;
  58.  
  59. end.
  60. //delphi/8760

回复 "Delphi想TStringGrid中添加和删除行的方法"

这儿你可以回复上面这条便签

captcha