You are here:

Delphi/Adding data to StringGrid and deleting by button click

Advertisement


Question
Hello!

I would be very thankful if you could tell how to add data into an row of a StringGrid component by a button click from source that is not important. I want to add data by button "Save" and to delete that data by button "Delete". Now the problem is that when i add data into a row and delete it i don't know how to position myself into a empty row.

Thank you for your time!

Answer
Try this example and see if it answers the question:

unit Unit2;

interface

uses
 Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
 Dialogs, Grids, StdCtrls;

type
 TForm2 = class(TForm)
   StringGrid: TStringGrid;
   edtName: TEdit;
   edtCity: TEdit;
   edtEmail: TEdit;
   btnAdd: TButton;
   Label1: TLabel;
   Label2: TLabel;
   Label3: TLabel;
   btnDelete: TButton;
   procedure FormCreate(Sender: TObject);
   procedure btnAddClick(Sender: TObject);
   procedure btnDeleteClick(Sender: TObject);
 private
   procedure DeleteStringGridRow(n: Integer; var Grid: TStringGrid);
 public
 end;

var
 Form2: TForm2;

implementation

{$R *.dfm}

procedure TForm2.FormCreate(Sender: TObject);
begin
 StringGrid.ColCount := 4;
 StringGrid.RowCount := 2;
 StringGrid.FixedRows := 1;
 StringGrid.ColWidths[0] := 50;
 StringGrid.ColWidths[1] := 150;
 StringGrid.ColWidths[2] := 100;
 StringGrid.ColWidths[3] := 200;

 StringGrid.DefaultRowHeight := StringGrid.Canvas.TextHeight('X') + 8;

 StringGrid.Cells[0, 0] := 'Row';
 StringGrid.Cells[1, 0] := 'Name';
 StringGrid.Cells[2, 0] := 'City';
 StringGrid.Cells[3, 0] := 'Email';
end;

procedure TForm2.btnAddClick(Sender: TObject);
begin
 if Trim(edtName.Text) = '' then begin
   ShowMessage('Name Required');
   Exit;
 end;

 if (StringGrid.RowCount > 2) or (StringGrid.Cells[1, StringGrid.RowCount-1] > '') then
   StringGrid.RowCount := StringGrid.RowCount + 1;

 StringGrid.Cells[0, StringGrid.RowCount-1] := IntToStr(StringGrid.RowCount-1);
 StringGrid.Cells[1, StringGrid.RowCount-1] := edtName.Text;
 StringGrid.Cells[2, StringGrid.RowCount-1] := edtCity.Text;
 StringGrid.Cells[3, StringGrid.RowCount-1] := edtEmail.Text;

 edtName.Text := '';
 edtCity.Text := '';
 edtEmail.Text := '';

 ActiveControl := edtName;
end;


procedure TForm2.btnDeleteClick(Sender: TObject);
begin
 if (StringGrid.Row > 0)
 and (StringGrid.Cells[1, StringGrid.RowCount-1] > '')
 and (MessageDlg(Format('Delete %s?', [StringGrid.Cells[1, StringGrid.RowCount-1]]), mtConfirmation, [mbYes, mbNo], 0) = mrYes) then begin
   DeleteStringGridRow(StringGrid.Row, StringGrid);
   if StringGrid.RowCount < 2 then
     StringGrid.RowCount := 2;
 end;

 ActiveControl := StringGrid;
end;

procedure TForm2.DeleteStringGridRow(n: Integer; var Grid: TStringGrid);
var
 i: integer;
begin
 if Grid.RowCount > Grid.FixedRows + 1 then begin
   for i := (n + 1) to Grid.RowCount do
     Grid.Rows[i-1] := Grid.Rows[i];
   Grid.RowCount := Grid.RowCount -1;
 end else
   Grid.Rows[n].Clear;
end;

end.



unit2.dfm:

object Form2: TForm2
 Left = 0
 Top = 0
 ActiveControl = StringGrid
 Caption = 'Form2'
 ClientHeight = 334
 ClientWidth = 635
 Color = clBtnFace
 Font.Charset = DEFAULT_CHARSET
 Font.Color = clWindowText
 Font.Height = -11
 Font.Name = 'Tahoma'
 Font.Style = []
 OldCreateOrder = False
 OnCreate = FormCreate
 DesignSize = (
   635
   334)
 PixelsPerInch = 96
 TextHeight = 13
 object Label1: TLabel
   Left = 8
   Top = 256
   Width = 27
   Height = 13
   Anchors = [akLeft, akBottom]
   Caption = 'Name'
 end
 object Label2: TLabel
   Left = 8
   Top = 282
   Width = 19
   Height = 13
   Anchors = [akLeft, akBottom]
   Caption = 'City'
 end
 object Label3: TLabel
   Left = 8
   Top = 308
   Width = 24
   Height = 13
   Anchors = [akLeft, akBottom]
   Caption = 'Email'
 end
 object StringGrid: TStringGrid
   Left = 8
   Top = 8
   Width = 619
   Height = 236
   Anchors = [akLeft, akTop, akRight, akBottom]
   TabOrder = 4
 end
 object edtName: TEdit
   Left = 41
   Top = 253
   Width = 172
   Height = 21
   Anchors = [akLeft, akBottom]
   TabOrder = 0
 end
 object edtCity: TEdit
   Left = 41
   Top = 279
   Width = 121
   Height = 21
   Anchors = [akLeft, akBottom]
   TabOrder = 1
 end
 object edtEmail: TEdit
   Left = 41
   Top = 305
   Width = 147
   Height = 21
   Anchors = [akLeft, akBottom]
   TabOrder = 2
 end
 object btnAdd: TButton
   Left = 235
   Top = 251
   Width = 75
   Height = 25
   Anchors = [akLeft, akBottom]
   Caption = 'Add'
   Default = True
   TabOrder = 3
   OnClick = btnAddClick
 end
 object btnDelete: TButton
   Left = 235
   Top = 277
   Width = 75
   Height = 25
   Anchors = [akLeft, akBottom]
   Caption = 'Delete'
   TabOrder = 5
   OnClick = btnDeleteClick
 end
end

Delphi

All Answers


Answers by Expert:


Ask Experts

Volunteer


Randy Sill

Expertise

Turbo Pascal and Delphi developer since 1986. Strengths - UI, Windows API, Database, SQL, Internet, Threading, NT Services.

Experience

Turbo Pascal and Delphi developer since 1986.

Education/Credentials
Delphi 5 Certification, Borland

Awards and Honors
2005 Indiana IT/Software Million Dollar Award, Key Technical Contributor

©2016 About.com. All rights reserved.