Reputation: 75
guys!
I have this txt file and I want to load the content into a stringgrid. Here's my code :
procedure PopulateStringGrid(Grid: TStringGrid; const FileName: string);
var
TextFile, Line: TStringList;
Row, col: Integer;
begin
Grid.RowCount := 0;//clear any previous data
TextFile := TStringList.Create;
try
Line := TStringList.Create;
try
Line.Delimiter := ' ';
TextFile.LoadFromFile(FileName);
Grid.RowCount := TextFile.Count;
for Row := 0 to TextFile.Count-1 do
begin
Line.DelimitedText := TextFile[Row];
for Col := 0 to Grid.ColCount-1 do
if Col<Line.Count then
Grid.Cells[Col, Row] := Line[Col]
else
Grid.Cells[Col, Row] := '0';
end;
finally
Line.Free;
end;
finally
TextFile.Free;
end;
end;
procedure TForm5.sButton1Click(Sender: TObject);
var filename1:string;
begin
if opendialog1.Execute then
begin
sedit1.Text:=opendialog1.FileName;
filename1:=sedit1.Text;
PopulateStringGrid(StringGrid1, FileName1);
showmessage(filename1);
end
else showmessage('file failed to load');
end;
It works nicely, but the data is too big so I want to use data I need. I need to get data in date range.
here's the pic to explain :
I want to only show the data in range of dates I select in dateedit at top. Any idea how to do that? Thank you in advance! I'm using delphi 7 for this.
Upvotes: 1
Views: 683
Reputation: 12322
It looks the date stored in the text files are formatted YYYY-MM-DD, you can compare dates like strings (The order a string is the same as date).
You should add two arguments in your PopulateStringGrid function:
procedure PopulateStringGrid(
Grid: TStringGrid;
const FileName: string;
const DateFrom: string;
const DateTo: String);
When you call PopulateStringGrid , you pass the date from the two date fields:
PopulateStringGrid(StringGrid1, FileName1,
FormatDateTime('YYYY-MM-DD', DateFrom.Date),
FormatDateTime('YYYY-MM-DD', DateTo.Date));
Finally, in the loop in PopulateStringGrid, check for the column number containing the date and check the date range, and maintain a different row number for the grid because some lines are not added. At the end, fix the number of rows to the exact found number of rows:
procedure PopulateStringGrid(
Grid: TStringGrid;
const FileName: string;
const DateFrom: string;
const DateTo: String);
var
TextFile, Line: TStringList;
Row, col: Integer;
RowGrid : Integer;
begin
Grid.RowCount := 0;//clear any previous data
TextFile := TStringList.Create;
try
Line := TStringList.Create;
try
Line.Delimiter := ' ';
TextFile.LoadFromFile(FileName);
Grid.RowCount := TextFile.Count;
RowGrid := 0;
for Row := 0 to TextFile.Count-1 dobegin
Line.DelimitedText := TextFile[Row];
if (Line[COL_WITH_DATE] >= DateFrom) and
(Line[COL_WITH_DATE] <= DateTo) then begin
for Col := 0 to Grid.ColCount-1 do begin
if Col<Line.Count then
Grid.Cells[Col, RowGrid] := Line[Col]
else
Grid.Cells[Col, RowGrid] := '0';
end;
Inc(RowGrid);
end;
end;
Grid.RowCount := RowGrid;
finally
Line.Free;
end;
finally
TextFile.Free;
end;
end;
This code is out of my head. I have not tested it! Note: Maybe it is more efficient to change the grid RowCount at each line than pre-allocate enough rows for everything and the adjust at the end like I have done.
Upvotes: 1