Today, Delphi is one of the popular desktop programming tool. There are thousand even million programmer in this world using delphi as their favorit tool. This site try to collect the examples, tips and tricks of Delphi programming. We collect, test them and redistribute the collection of delphi programming for you.

Delete Row in TStringGrid Component

Author: admin | Category: Tips and Tricks, VCL

//When we work with TStringGrid Component, There is no methode for deleting row
//This is how we can Delete row in TStringGrid Component
//Just place Button1 (TButton) and StringGrid1 (TStringGrid) on the Form1 then try this code
 
unit Unit1;
 
interface
 
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Grids;
 
type
  TForm1 = class(TForm)
    StringGrid1: TStringGrid;
    Button1: TButton;
    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;
 
var
  Form1: TForm1;
 
implementation
 
{$R *.dfm}
 
//Procedure Below to fill StringGrid1 Cells with its coordinate self
procedure TForm1.FormCreate(Sender: TObject);
var
  i,j:integer;
begin
  for i := 0 to StringGrid1.RowCount -1 do
  begin
    for j := 0 to StringGrid1.ColCount - 1 do
    begin
      StringGrid1.Cells[j,i] := IntToStr(j)+','+IntToStr(i);
    end;
  end;
end;
 
//Copy the parameterized row contains from next row
procedure DeleteStringGridRow1(AStringGrid:TStringGrid;Arow:Integer);
var
  i,j:integer;
begin
  for i := Arow to AStringGrid.RowCount - 2 do
  begin
    for j := 0 to AStringGrid.ColCount - 1 do
    begin
      AStringGrid.Cells[j,i] := AStringGrid.Cells[j,i+1];
    end;
  end;
  AStringGrid.RowCount := AStringGrid.RowCount-1;
end;
 
procedure TForm1.Button1Click(Sender: TObject);
var
  SelectedRow:integer;
begin
  //Click to the row you want to delete
  SelectedRow := StringGrid1.Row;
  DeleteStringGridRow1(StringGrid1,SelectedRow);
end;
 
end.

Tags: