unit Unit1;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, Forms, Controls, Graphics, Dialogs, DBGrids, StdCtrls,
csvdataset, DB;
type
{ TForm1 }
TForm1 = class(TForm)
Button1: TButton;
CSVDataset1: TCSVDataset;
DataSource1: TDataSource;
DBGrid1: TDBGrid;
EditFilter: TEdit;
Label1: TLabel;
Label2: TLabel;
OpenDialog1: TOpenDialog;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure DBGrid1TitleClick(Column: TColumn);
procedure EditFilterChange(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
procedure LoadCSVFile(const FileName: string);
public
end;
var
Form1: TForm1;
i:Integer;
implementation
{$R *.lfm}
{ TForm1 }
procedure TForm1.EditFilterChange(Sender: TObject);
var
country: string;
begin
country := EditFilter.Text;
country := country+'*'; //THE TRICK WAS TO ADD A WILDCARD "*" TO THE FILTER STRING AT THE END OF EACH KEYPRESS. THERE HAS TO BE A BETTER WAY?
CSVDataSet1.Filtered := False; // Disable previous filters
CSVDataSet1.Filter := Format('country = ''%s''', [country]);
CSVDataSet1.Filtered := True;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
end;
procedure TForm1.DBGrid1TitleClick(Column: TColumn);
begin
//showmessage('sort clicked');
CSVDataset1.IndexFieldNames := Uppercase(Column.Title.Caption);
end;
procedure TForm1.LoadCSVFile(const FileName: string);
begin
CSVDataset1.Close; // Close any previously opened dataset
// Set the new file name
CSVDataset1.FileName := FileName;
Label1.Caption:=Filename;
// Optionally configure properties
// CSVDataset1.FirstLineAsFieldNames := True;
CSVDataset1.Open; // Open the new dataset
DataSource1.DataSet := CSVDataset1; // Link the DataSource to the new dataset
DBGrid1.DataSource := DataSource1; // Link the DBGrid to the DataSource
for i:=0 to DBGrid1.Columns.Count-1 do
DBGrid1.Columns[i].Width := 150;
Label2.Caption:='Record Count: '+intToStr(CSVDataset1.RecordCount);
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
OpenDialog1.Filter := 'CSV Files|*.csv';
if OpenDialog1.Execute then
begin
LoadCSVFile(OpenDialog1.FileName);
end;
end;
{CSVDataset1.Close;
OpenDialog1.Filter := 'CSV Files|*.csv';
if OpenDialog1.Execute then
CSVDataSet1.FileName := OpenDialog1.FileName;
Label1.Caption:='CSV File Path: '+OpenDialog1.FileName;
CSVDataSet1.Open;
DataSource1.DataSet := CSVDataset1; // Link the DataSource to the new dataset
DBGrid1.DataSource := DataSource1; // Link the DBGrid to the DataSource
for i:=0 to DBGrid1.Columns.Count-1 do
DBGrid1.Columns[i].Width := 150;
Label2.Caption:='Record Count: '+intToStr(CSVDataset1.RecordCount);
CSVDataset1.refresh;
DBGrid1.refresh;
end;}
procedure TForm1.Button2Click(Sender: TObject);
begin
end;
end.