Hiding columns TStringgrid gives stange result with Columns.VisibleIndex lazarus / 31-LCL ---------------------------------------------------------------------- flaza · 2014-12-02 18:44 Hi. I have a TStringGrid component with 10 named columns. The Names are defined in a set. The 3 column showes the name of tennisplayers. I have a TEdit, type a letter and the Grid selects the first row where 3 column starts with that letter. I do this with the following OnChange Code: [Select]procedure TForm.StringGrid1Change(Sender: TObject); var   i, k: Integer;   s1, s2: string; begin   k := Ord(sName);   for i := 1 to StringGrid1.RowCount - 1 do   begin     s1 := upcase(StringGrid1.Cells[k, i]);     s2 := upcase(TEdit(Sender).Text);     if LeftStr(s1, Length(s2)) = s2 then     begin       StringGrid1.Row := i;       Break;     end;   end; end; This is my working example, and it works fine. In the official version I hide the second column and the Grid doesn't react anymore. So I change my routine as follows Code: [Select]procedure TForm.StringGrid1Change(Sender: TObject); var   i, k: Integer;   s1, s2: string; begin   k := StringGrid1.Columns.VisibleIndex(Ord(sName));   for i := 1 to StringGrid1.RowCount - 1 do   begin     s1 := upcase(StringGrid1.Cells[k, i]);     s2 := upcase(TEdit(Sender).Text);     if LeftStr(s1, Length(s2)) = s2 then     begin       StringGrid1.Row := i;       Break;     end;   end; end; Now the problems begin. k=1 but s1 gives the contents of StringGrid1.Cells[0, i]). I have to add 1 to k. Now I make column 1 visible again, all columns ar shown. k=2 but s1 gives the contents of StringGrid1.Cells[3, i]). I don't get it. Eventually I make a new change as follows Code: [Select]procedure TForm.StringGrid1Change(Sender: TObject); var   i, k: Integer;   s1, s2: string; begin   if StringGrid1.ColCount = StringGrid1.Columns.VisibleCount then     k := Ord(sName)   else k := StringGrid1.Columns.VisibleIndex(Ord(sName)) + 1;   for i := 1 to StringGrid1.RowCount - 1 do   begin     s1 := upcase(StringGrid1.Cells[k, i]);     s2 := upcase(TEdit(Sender).Text);     if LeftStr(s1, Length(s2)) = s2 then     begin       StringGrid1.Row := i;       Break;     end;   end; end; This seems to work fine but VisibleIndex gives always the same result as ColCount. Ang yet it works. I can't figure this out.  Can anyone give me the clou, or a hint? ---------------------------------------------------------------------- Blaazen · 2014-12-02 19:52 Are columns of your grid defined simply via ColCount property or did you defined it with StringGrid.Columns editor? ---------------------------------------------------------------------- flaza · 2014-12-02 20:10 I Used the Object Inspector, component TStringGrid.Columns and then AddColumns. There I modified the columns with Caption and Width. ---------------------------------------------------------------------- Blaazen · 2014-12-02 20:46 I did a quick test with five columns defined in StringGrid.Columns editor. This: Code: [Select]procedure TForm1.Button1Click(Sender: TObject); begin   writeln(StringGrid1.Columns.VisibleIndex(2));   writeln(StringGrid1.Columns.VisibleIndex(3));   StringGrid1.Cells[2, 1]:='hhh';   StringGrid1.Columns[2].Visible:=False;   writeln(StringGrid1.Columns.VisibleIndex(2));   writeln(StringGrid1.Columns.VisibleIndex(3));   StringGrid1.Cells[2, 1]:='ggg';   StringGrid1.Columns[2].Visible:=True;   writeln(StringGrid1.Columns.VisibleIndex(2));   writeln(StringGrid1.Columns.VisibleIndex(3)); end;rewrites cell 'hhh' with 'ggg'. That means you should write to the cell regardless if its coulmn is visible or not. IMO, you don't have to use VisibleIndex() function. EDIT: Output is: Code: [Select]2 3 -1 2 2 3(Invisible column returns -1). ---------------------------------------------------------------------- flaza · 2014-12-03 11:10 Hi. I did the same as you, I think. But my output is very different. The first 2 lines give -1 and the 4th line gives a List Index (2) out of bounds error. Strangely anough, the line to put 'hhh' in Cell[2,1] works. According to the ObjectInspector teh StringGrid1.ColCount=5 but the StringGrid1.Columns=0 items. Am I forgetting something? ---------------------------------------------------------------------- Blaazen · 2014-12-03 12:14 I use trunk, r.47047. If you are on 1.2.6 then maybe something changed in meanwhile. ---------------------------------------------------------------------- flaza · 2014-12-03 18:08 Ok, I'll check the SVN. But I'm afraid I asked the wrong question. Trying this and that gives always the wanted result with k=Ord(sName). But I have to keep column 0 visible. The moment I hide column 0, the function doesn't work. By the way, FixedCols=0.