TTMSFNCDataGrid.LoadFromCSVData Method
Loads cell data from a CSV file using the system default (ANSI) encoding.
API unit family: TMSFNCDataGrid
Declaring type: TTMSFNCDataGrid
Inherited from: TTMSFNCCustomDataGrid
Overloads
Overload 1
Loads cell data from a CSV file using the system default (ANSI) encoding.
procedure LoadFromCSVData(AFileName: string); overload; virtual;
virtual void __fastcall LoadFromCSVData(System::UnicodeString AFileName)/* overload */;
Remarks
Values are written from Options.IO.StartRow / Options.IO.StartColumn on; RowCount is adjusted to the loaded rows and ColumnCount grows to the widest line. The delimiter comes from Options.IO.Delimiter and is detected automatically when it is #0; Options.IO.MaxRows limits the number of lines. Each value passes through OnLoadCellData.
Use the overload with an encoding for UTF-8 files.
Parameters
| Name | Description |
|---|---|
AFileName |
Path of the CSV file to load. |
Exceptions
| Exception | Description |
|---|---|
Exception |
Raised when the file is empty. |
Examples
Grid.LoadFromCSVData('data.csv', TEncoding.UTF8);
Related API
Overload 2
Loads cell data from a CSV file using the specified encoding.
procedure LoadFromCSVData(AFileName: string; AEncoding: TEncoding); overload; virtual;
virtual void __fastcall LoadFromCSVData(System::UnicodeString AFileName, System::Sysutils::TEncoding* AEncoding)/* overload */;
Remarks
Uses Options.IO (start row/column, delimiter, maximum rows) like the other CSV overloads.
Parameters
| Name | Description |
|---|---|
AFileName |
Path of the CSV file to load. |
AEncoding |
Text encoding used to read the file, for example TEncoding.UTF8. |
Exceptions
| Exception | Description |
|---|---|
Exception |
Raised when the file is empty. |
Usage examples
Round-trip a DataGrid through a CSV file
Load CSV with a configurable start cell, then save the current contents back as CSV. Custom delimiters and a custom header row are supported.
procedure TForm1.FormCreate(Sender: TObject);
begin
// Where in the grid to drop the CSV content
Grid.Options.IO.StartColumn := 1;
Grid.Options.IO.StartRow := 1;
// Default delimiter is the system list separator. Override per call when needed.
Grid.LoadFromCSVData('CARS.csv');
end;
procedure TForm1.SaveButtonClick(Sender: TObject);
begin
// Save with explicit ; delimiter (regardless of OS locale)
Grid.SaveToCSV('out.csv', ';');
end;
void __fastcall TForm1::FormCreate(TObject *Sender)
{
// Where in the grid to drop the CSV content
Grid->Options->IO->StartColumn = 1;
Grid->Options->IO->StartRow = 1;
// Default delimiter is the system list separator. Override it through Options->IO.
Grid->LoadFromCSVData("CARS.csv");
}
void __fastcall TForm1::SaveButtonClick(TObject *Sender)
{
// Save with explicit ; delimiter (regardless of OS locale)
Grid->Options->IO->Delimiter = L';';
Grid->SaveToCSVData("out.csv");
}