简体   繁体   中英

How to load a XML/CSV/ODB file in DBGrid in Lazarus/Free Pascal?

I want to open a database/spreadsheet in a TDBGrid in Lazarus, and I've tried DBGrid1.LoadFromFile(filename); with xml, csv, xlsx, odb. But with every file I get an error.

This is the code I'm using, with 'xml' being different each time I try:

procedure TForm1.FormCreate(Sender: TObject);
begin
  DBGrid1.LoadFromFile('/home/pi/Jesse Gielen/PWS/Databeesjes/WoahWeerGegevens.xml');
end; 

When I try xml, I get this error: EDOMHierarchyRequest in Document.InsertBefore

When I try csv, this error appears:

Project project1 raised exception class 'EXMLReadError' (line 1 pos 1): Illegal at document level

When xlsx: Project project1 raised exception class 'EXMLReadError' (line 1 pos 3): Invalid character

When odb same as xlsx: Project project1 raised exception class 'EXMLReadError' (line 1 pos 3): Invalid character

So: How can I open a file in a DBGrid in Lazarus? If this isn't possible in any way (which I would find stange because why is there a procedure for it then?) then is my question: How can I translate a TStringgrid to a TDBGrid? (because loading a file into TStringgrid worked fine)

You are asking the wrong question. The way to think about this is that the TDBGrid displays the data fed to it via a TDataSource from a TDataSet descendant. Therefore what you need to do is to load the TDataSet descendant from the file, and the data will then automatically display in the TDBGrid (while the dataset is open).

Lazarus's standard components include several TDataSet descendants, including TMemDataSet and TCSVDataSet, with LoadFromFile and LoadFromStream methods. Check the online documentation for which one is best suited to your needs.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM