Recent

Author Topic: Stringgrid - jump to empty row  (Read 3492 times)

Jakub2016

  • New member
  • *
  • Posts: 7
Stringgrid - jump to empty row
« on: September 24, 2016, 07:42:56 pm »
Hello everyone,

I am trying to solve an issue with a stringgrid in my project. I cant set it up the way I want to.
I have 2 forms , 1st one is with string grid , 2nd one is with edit's , its kind of xml data base. When I filled all in I press 'Add' and data is transferd to stringgrid as  a kind of a record.
Then I can save the file as 'test.txt' , now I am going to close the program and open it again , stringgrid reads previously written file 'test.txt' and now I want to add one more row with the data , here comes the problem , I cant set it to find in the string grid last record or first empty cells , at the moment it rewrites previously typed data :/

Can enyone help me please.

Regards

Jakub2016

  • New member
  • *
  • Posts: 7
Re: Stringgrid - jump to empty row
« Reply #1 on: September 24, 2016, 08:16:34 pm »
I am not that experienced programmer , so the code might be very poor in structure ...
So, the code looks like this , all works smooth until I close the program , then stringgrid reads txt file with xml ID's after that I press add button to add new user , unfortunatley program saves it to the 1st row again , which is overwriting provious data. All the var's are set to edit fields. I would like to add that when program is not closed it works as I want ,it adds 1st row then second and etc , problem is when I open the program again.


This is in unit 2 (form2) :

arow :=arow+ 1;

 form1.stringgrid1.Cells[1,arow]:=name;
 form1.stringgrid1.Cells[2,arow]:=surname;
 form1.stringgrid1.Cells[3,arow]:=batch;
 form1.stringgrid1.Cells[4,arow]:=agency;
 form1.stringgrid1.Cells[5,arow]:=start;
 form1.stringgrid1.Cells[6,arow]:=age;
 form1.stringgrid1.Cells[7,arow]:=nat;
 form1.stringgrid1.Cells[8,arow]:=phone;

 form2.Close;


and that in unit1 ( form1) :

procedure TForm1.FormCreate(Sender: TObject);
begin

 stringgrid1.col:=1;
 stringgrid1.loadfromfile('test.txt');

end;   

procedure TForm1.ButtonSave(Sender: TObject);
begin

  stringgrid1.SaveToFile('test.txt');

end;


procedure TForm1.ButtonAdd(Sender: TObject);
begin

  StringGrid1.SetFocus;
  form2.Visible:=true;


end; 
« Last Edit: September 24, 2016, 08:40:57 pm by Jakub2016 »

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Stringgrid - jump to empty row
« Reply #2 on: September 24, 2016, 09:22:19 pm »
A simple-minded stringgrid-as-database might be coded something like the following, where the data is saved at every click of the Add button.
You would need to adapt this to cater for FixedRows etc., but this should give you something to work from.
I dropped a couple of buttons (Add and Load), 8 edits and a stringgrid on the main form for this project.

Code: Pascal  [Select][+][-]
  1. unit mainStringGridSaving;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   SysUtils, Forms, Grids, StdCtrls;
  9.  
  10. const CSVDataFilename = 'griddata.csv';
  11.  
  12. type
  13.  
  14.   { TForm1 }
  15.  
  16.   TForm1 = class(TForm)
  17.     BAdd: TButton;
  18.     BLoad: TButton;
  19.     EAge: TEdit;
  20.     EAgency: TEdit;
  21.     EBatch: TEdit;
  22.     EName: TEdit;
  23.     ENat: TEdit;
  24.     EPhone: TEdit;
  25.     EStart: TEdit;
  26.     ESurname: TEdit;
  27.     StringGrid1: TStringGrid;
  28.     procedure BAddClick(Sender: TObject);
  29.     procedure BLoadClick(Sender: TObject);
  30.   private
  31.     FCurRow: integer;
  32.   end;
  33.  
  34. var
  35.   Form1: TForm1;
  36.  
  37. implementation
  38.  
  39. {$R *.lfm}
  40.  
  41. { TForm1 }
  42.  
  43. procedure TForm1.BAddClick(Sender: TObject);
  44. begin
  45.   with StringGrid1 do begin
  46.     if (FCurRow > RowCount-1) then
  47.       RowCount:=RowCount + 1;
  48.     Cells[1,FCurRow]:=EName.Text;
  49.     Cells[2,FCurRow]:=ESurname.Text;
  50.     Cells[3,FCurRow]:=EBatch.Text;
  51.     Cells[4,FCurRow]:=EAgency.Text;
  52.     Cells[5,FCurRow]:=EStart.Text;
  53.     Cells[6,FCurRow]:=EAge.Text;
  54.     Cells[7,FCurRow]:=ENat.Text;
  55.     Cells[8,FCurRow]:=EPhone.Text;
  56.     Inc(FCurRow);
  57.     SaveToCSVFile(CSVDataFileName);
  58.   end;
  59. end;
  60.  
  61. procedure TForm1.BLoadClick(Sender: TObject);
  62. begin
  63.   if FileExists(CSVDataFilename) then
  64.     StringGrid1.LoadFromCSVFile(CSVDataFilename);
  65.   FCurRow:=StringGrid1.RowCount;
  66.   StringGrid1.RowCount:=StringGrid1.RowCount + 1;
  67. end;
  68.  
  69. end.

Jakub2016

  • New member
  • *
  • Posts: 7
Re: Stringgrid - jump to empty row
« Reply #3 on: September 25, 2016, 11:09:05 am »
Thanks Man !

It works :)


 

TinyPortal © 2005-2018