Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

CSV File - String Grid 1

Status
Not open for further replies.

minckle

Programmer
Mar 17, 2004
142
GB
How do i go about read a csv file and placing the contents into a string Grid.
 
How is the csv file formatted?

You can use the loadfromfile command to load the file into, for example, a stringlist and run through its contents a line at a time adding each section to the correct StringGrid line (See the StringGrid Cells features in the help file).



"It's 106 miles to Chicago, we've got a full tank of gas, half a pack of cigarettes, it's dark and we're wearing sunglasses"

"Hit it!"
 
Play around with this:

Note that the CommaText method will treat spaces as delimiters unless inside a quoted string. So, depending on your actual data you may need to tweak this code a bit.

Also if you don't want to auto-size the columns, you can simply ignore that part of the code.

Code:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Grids;

type
  TForm1 = class(TForm)
    StringGrid1: TStringGrid;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure GridColAutoSize( Grid:TStringGrid );
var
  nCol, nRow, nWidth, nMaxWidth: integer;
begin
  with Grid as TStringGrid do
  begin
    for nCol := 0 to ColCount - 1 do
    begin
      nMaxWidth := 0;
      for nRow := 0 to RowCount - 1 do
      begin
        nWidth := Canvas.TextWidth( Cells[nCol,nRow] );
        if nWidth > nMaxWidth then nMaxWidth := nWidth;
      end; {for nRow}
      ColWidths[nCol] := nMaxWidth + 7;
    end; {for nCol}
  end; {with Grid}
end;


procedure TForm1.Button1Click(Sender: TObject);
var
  oFileStrings:TStringList;
  oRowStrings:TStringList;
  i:integer;
begin
  oFileStrings := TStringList.Create;
  oRowStrings := TStringList.Create;
  try
    StringGrid1.FixedCols := 1;
    StringGrid1.FixedRows := 1;
    StringGrid1.RowCount := 2;
    StringGrid1.ColCount := 2;
    oFileStrings.LoadFromFile('c:\test.csv');
    StringGrid1.RowCount := oFileStrings.Count;
    for i := 0 to oFileStrings.Count - 1 do
    begin
      oRowStrings.Clear;
      oRowStrings.CommaText := oFileStrings[i];
      oRowStrings.Insert(0,IntToStr(i));
      if oRowStrings.Count > StringGrid1.ColCount then
        StringGrid1.ColCount := oRowStrings.Count;
      StringGrid1.Rows[i].Assign(oRowStrings);
    end;
    StringGrid1.Cells[0,0] := '';
    GridColAutoSize( StringGrid1 );
  finally
    oFileStrings.Free;
    oRowStrings.Free;
  end;
end;

end.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top