What's the best way to delete columns if you don't want them due to their title caption?
I have this, but I keep getting out of bounds errors:
...
StringGrid1.LoadFromCSVFile(OpenDialog1.FileName, #9, true);
....
ColumnCount := StringGrid1.ColCount -1;
for i := ColumnCount downto 0 do
begin
if (StringGrid1.Columns[i].Title.Caption <> 'Column I Want A') or // if the title caption IS NOT 'Column I Want A'
(StringGrid1.Columns[i].Title.Caption <> 'Column I Want B') or // if the title caption IS NOT 'Column I Want B'
(StringGrid1.Columns[i].Title.Caption <> 'Column I Want C') or // if the title caption IS NOT 'Column I Want C'
(StringGrid1.Columns[i].Title.Caption <> 'Column I Want D') then // if the title caption IS NOT 'Column I Want D'
begin
StringGrid1.Columns.Delete(i); // then get rid of the column. Otherwise, keep it.
end;
end;
I have also tried
for i := StringGrid1.ColCount -1 downto 0 do
but I got further index out of range errors.
I have also tried
for i := StringGrid1.Columns.Count -1 downto 0 do
All give the same problem. My first code paste is the only one that actually returns (if I use SHowMessage or whatever) the number of columns. It says there are "41 columns" or whatever. The others say 0 columns, and so the loop never executes. My first suggestion, though, returns out of bounds errors.
Does anyone have code for sanitising columns based on on their title captions that you might share? \If not, is there a better way to do this (other than verbally telling the user to make sure their input files do not contain extra columns)
??