1

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 : STRINGGRID

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.

mizkyd
  • 75
  • 1
  • 11

1 Answers1

1

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.

fpiette
  • 11,983
  • 1
  • 24
  • 46
  • hi, your answer is really helpful! It did work, however I got a case like this. let's say I want data range from 20-08-2020 and 21-08-2020. the file also consist of data from 19-08-2020. so my stringgrid will return empty grid as many as the 19-08-2020 file. how can I make the blanks doesn't show and my stringgrid only show the desired data? big thanks! – mizkyd Sep 23 '20 at 08:59
  • @mizkyd "your answer is really helpful! It did work" Please mark the answer as "accepted" (Check mark on the left of the answer). – fpiette Oct 15 '20 at 11:38
  • @mizkyd Sorry, I don't understand your last comment. My code only add a grid row for data in range. Maybe you have done a small error copying/adapting my code? – fpiette Oct 15 '20 at 11:40