[SOLVED... ish] TStringGrid.LoadFromCSV clobbers the fixed column lazarus / 21-General ---------------------------------------------------------------------- EganSolo · 2021-02-08 22:01 Using TStringGrid.LoadFromCSV file loads the data starting with the fixed column.  Even when I specify goFixedRowAutoNumbering, the data from the file is loaded starting from the first (fixed) column. Any suggestion on how to fix this? Am I missing something obvious? A configuration that I need to set beyond FixCols = 1? ---------------------------------------------------------------------- jamie · 2021-02-08 22:14 Don't knowl… but  LoadFromCSVFile('FileName', False); Try that .. ---------------------------------------------------------------------- winni · 2021-02-08 23:12 Hi! If you leave all parameters for StringGrid.loadFromCSVfile on the default values you will never solve that problem. There are a lot of parameters. One of them is  UseTitles which by default is true. Set it to false. Done. RTFSRC! Winni ---------------------------------------------------------------------- wp · 2021-02-08 23:22 Quote from: EganSolo on February 08, 2021, 11:01:16 pmUsing TStringGrid.LoadFromCSV file loads the data starting with the fixed column.  Even when I specify goFixedRowAutoNumbering, the data from the file is loaded starting from the first (fixed) column. "From" the first column: what does this mean? Do you want to load the data file so that its first field in each row becomes the fixed column, or do you want to load the data file so that the fixed column of the grid remains untouched? The first case is default, therefore, I guess you need the second case. Here you must add two lines of code: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i   StringGrid2.LoadFromCSVFile('test.txt', ',', true, 0, true);  StringGrid2.InsertColRow(true, 0); // add an empty column at the very left  StringGrid2.FixedCols := 1;  // Make sure that the old fixed column becomes a normal column ---------------------------------------------------------------------- EganSolo · 2021-02-09 00:52 Thank you so much for your helpful answers! The code by wp did it.  Please bear with me a little longer; I'm getting used to TStringGrid and I'm still not getting some of its behavior. As I said, the code from wp runs like a charm. After that bit, I added the following (the project is attached. Should you like to run it, you can unzip the project anywhere; there are no paths dependencies): Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i   With Memo2.Lines do  begin    Clear;    Add('Number of Rows        : ' + IntToStr(SG.RowCount )); //SG is the name of the TStringGrid    Add('Number of Fixed Rows  : ' + IntToStr(SG.FixedRows));    Add('Number of Columns     : ' + IntToStr(SG.ColCount ));    Add('Number of Fixed Cols  : ' + IntToStr(SG.FixedCols));  End;  //The code below breaks: I get an Index out of range message.  With SG do  For i := 0 to ColCount - 1 do      If Assigned(Columns[i])        then If Assigned(Columns[i].Title) then             with Columns[i].Title do             begin               Memo2.Lines.Add('Column  ' + IntToStr(i+1));               Memo2.Lines.Add('Caption ' + Caption    );             End             else Memo2.Lines.Add('Title at column ' + IntToStr(i) + ' not assigned')      else Memo2.Lines.Add('Column ' + IntTostr(i) + ' not assigned');  When the code runs, the string grid populates properly (please ignore the memo1) but then I get an exception: List Index (0) out of bounds. But ColCount returns 5, so why am I not able to access these columns via the columns property? Essentially, I want to edit the titles for each column. Is that not the right way to access the titles? Thanks for your help! ---------------------------------------------------------------------- jamie · 2021-02-09 01:42 I believe your issue is that you are mixing an upper layer support of the grid, something that isn't native to the original TStringGrid The Columns property is like another set of control functions on top of the grid and what you are doing accessing a column that does not exist.. Columns.Count is the real number of columns of you are using this overlay feature.. If you stay with the raw string grid for now, I believe what you want to use is COLS or ROWS, those are the direct route to the cells.  or you can use CELLS[x,y] := ? Getting back to the Columns property, that is an overlay of functionality that uses the RAW grid functions but for you to use the COLUMNS property you need to create them. For example  Columns.Add(TGridColumn.Create(….)); etc... if you type out StringGrid1.Columns.    and wait, you will see the members of that property.. so before you can access any columns you first need to create them, when you do this it will also create the raw cols in the base grid if needed.. Columns is just an added feature for the grid you don't need to use them, use CELLS ,ROWS or COLS otherwise.. ---------------------------------------------------------------------- EganSolo · 2021-02-09 08:19 I've come up with a solution to this problem and ... I don't I like it. It must be my own ignorance of TStringGrid and I hope someone smarter and with deeper experience could look at this and show us the way  All the details are in the attached project. Here's a quick summary so that you don't have to open the project. Objective : Load the content of a CSV file into an instance of TStringGrid and configure the property of the column, particularly its button style. Terminology This is a bit confusing, but bear with me: A COLUMN is an instance of the class TGridColumn. A COLUMN has properties that allow us to control the behavior of the column. Most likely, there are alternative ways to achieve the same thing without using COLUMN but I find the concept of COLUMN quite natural: it's a bit of meta-data about the column.A col is an accessor to a column in the grid. It's not related to the object COLUMN. Pain Point:: TStringGrid.LoadFromCSVFile won't create COLUMNS. It will actually create cols. Objective: Get LoadFromCSVFile to load its content into COLUMNS. Additionally, have one fixed col (NOT COLUMN) which displays the line numbers. Solution: My solution involves the following five steps: TStringGrid.LoadFromCSVFile will use COLUMNS if TStringGrid.Columns.Enabled returns true. Since Enabled is true ONLY when the count of COLUMNS is greater than zero, we start by creating a COLUMN in the grid.We invoke tt]TStringGrid.LoadFromCSVFile[/tt], which will delete our initial column and create the exact number of columns required to load the CSV.Next, we need to add the fixed col. But there again, the interaction between cols and COLUMNS is subtle, so bear with me. We will insert a COLUMN at index 0. Now, the grid contains five COLUMNS: one empty, and four that hold the data for Index, FirstName, LastName, and Rank, the four fields in the test file Test.csvWe set TStringGrid.FixedCols to 1. This statement will create the fixed col AND it has an unwanted side-effect: it shifts the content of the COLUMNS to the left by one! But since we inserted a COLUMN in the leftmost position, all the content of the CSV file is stored in COLUMNS (and not in cols)We now shift the captions of the titles leftward by one and delete the rightmost column. If you want to see all this in action, download the attached project, compile and run. You can experiment by skipping steps or changing their order. As I said, I don't like this solution. If you have a more elegant way to load the content of CSV into a TStringGrid and configure the style, color, caption, index, etc of each column separately, please lead the way ---------------------------------------------------------------------- wp · 2021-02-09 09:13 There are two operating modes of the LCL TStringGrid: the classical one of Delphi, and a new LCL column-oriented mode. When I saw this for the first time I was positively surprised because it gives easy possibilities for formatting or introducing alternative cell editors. But, on the other hand, the column mode adds LOTS of confusion. The most obvious one is that there are two conflicting properties ColCount and Columns.Count: Columns.Count reports the count of the non-fixed columns, while ColCount is the count of all columns including the fixed cols. And ColCount cannot be written in Column mode where every column must be added separately. The other confusion is the addressing of cells and columns: When there is 1 fixed row and 1 fixed col the first editable cell is in Col=1 and Row=1, but the index of the related Column is 0... I have a TStringGrid in most of my projects, but I never used the Columns property to keep my head clear of these headaches. The CSV import/export methods obviously were written not having column mode in mind. What exactly are the benefits that you are expecting from using column mode? Usually everything in the Column properties can be achieved by a few lines of code in the OnPrepareCanvas event. ---------------------------------------------------------------------- EganSolo · 2021-02-09 09:21 Hi WP, Thank you for your reply. Glad to know the confusion wasn't only stuffed in my head. You ask a great question: what do I want to achieve? I have a "database" consisting of csv files. Each file has a header. As you know, a csv file does not hold any meta-data about the columns (or fields). I have a separate csv file that carries the meta-data, most of which maps to the properties in the COLUMNS (not cols) of TStringGrid. Basically, I want a method that allows me to configure the columns: length, justification, what type is it (pick-list, check- box, etc), is it visible? Is it read-only? The TColumns offer all of these properties. I admit I don't know how to manage them without TColumns. Initially, I went with TZMSQL and the DB bound controls, but that has it's own host of issues which are non-trivial either, which is why I chose to use to TStringGrid. Bottom line: if you know of a method or methods to control the meta- data of a column without using TColumns, I'm all ears. Thanks! Egan. ---------------------------------------------------------------------- wp · 2021-02-09 09:37 OK, this look like good reason to use the Columns. Otherwise you'd have to set up storage for the column properties on your own. So the problem is in reading the CSV file which ignores column mode. In fact, I never liked the idea that the TStringGrid contains code for this purpose - there are many other file formats which can be displayed in the StringGrid, but why should only CSV have a built-in importer? So, having this in mind, why don't you put StringGrid.LoadFromCSVFile aside and read the CSV yourself? In simple cases, you could use a TStringlist to read the file; its lines are the rows of the grid; you must split each line into the cells, either by using a second TStringList, or by using the Split string helper. If the contents of the CSV file is complex, e.g. contains line breaks within cells, or complex quoting situations, you could try the specialized csv units of the fcl which comply with the RFC 4180 standard. There is a wiki article about how to import a csv into a string grid even in column mode (https://wiki.freepascal.org/CsvDocume nt#Loading_a_csv_document_into_a_StringGrid).