Recent

Author Topic: [SOLVED] Deleting Columns from StringGrid based on Column Title  (Read 12875 times)

Gizmo

  • Hero Member
  • *****
  • Posts: 831
[SOLVED] Deleting Columns from StringGrid based on Column Title
« on: December 15, 2012, 10:06:17 pm »
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:

Code: [Select]
...
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

Code: [Select]
for i := StringGrid1.ColCount -1 downto 0 do


but I got further index out of range errors.

I have also tried

Code: [Select]
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)

??
« Last Edit: December 16, 2012, 01:29:06 am by tedsmith »

Blaazen

  • Hero Member
  • *****
  • Posts: 3241
  • POKE 54296,15
    • Eye-Candy Controls
Re: Deleting Columns from StringGrid based on Column Title
« Reply #1 on: December 15, 2012, 10:38:58 pm »
There is method
Code: [Select]
StringGrid1.DeleteCol(Index);If you do
Code: [Select]
StringGrid1.Columns[i].Delete;then Grid "does not know" that you deleted its column.
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

Gizmo

  • Hero Member
  • *****
  • Posts: 831
Re: Deleting Columns from StringGrid based on Column Title
« Reply #2 on: December 15, 2012, 11:56:29 pm »
Hi Blaazen

I had already tried that, and still got the same out of range errors. So I re-read http://wiki.freepascal.org/Grids_Reference_Page and it states, 1/3 of the way down :

Code: [Select]
// delete an existing column
    grid.columns.delete(0);           // Delete column 0

which is why I tried that route. Suffice to say that, so far, neither are working.

Blaazen

  • Hero Member
  • *****
  • Posts: 3241
  • POKE 54296,15
    • Eye-Candy Controls
Re: Deleting Columns from StringGrid based on Column Title
« Reply #3 on: December 16, 2012, 12:19:38 am »
Code: [Select]
        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;
This condition is always TRUE. Maybe you wanted to use AND instead of OR.

EDIT: Oh, enlightment comes to me. Your problems may be related to fixed columns. If your FixedCol is >0 then it can make problems when you try StringGrid.Columns.Delete(StringGrid.ColCount-1);
You have to use StringGrid.Columns.Count instead of StringGrid.ColCount. The first counts only non-fixed columns.
« Last Edit: December 16, 2012, 12:28:19 am by Blaazen »
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

Gizmo

  • Hero Member
  • *****
  • Posts: 831
Re: Deleting Columns from StringGrid based on Column Title
« Reply #4 on: December 16, 2012, 01:02:00 am »
a) good spot on the logic! Have changed them to AND.
b) An IntToStr(StringGrid.FixedCols); returns 0 following the loadfromCSV action. The object inspector also reports '0' for Fixed Cols. So there are no fixed columns.
c) StringGrid.ColCount -1 is reported to be the same number of columns in the file I am loading. Whereas StringGrid.Columns.Count returns 0 for the same file. So if I use StringGrid.Columns.Count  the loop doesn't execute at all because the counter starts and ends at zero. But, from what you're saying,  StringGrid.ColCount -1 can only be used with fixed columned grids?

So, despite fixing a), it still isn't working :-(
« Last Edit: December 16, 2012, 01:03:59 am by tedsmith »

Blaazen

  • Hero Member
  • *****
  • Posts: 3241
  • POKE 54296,15
    • Eye-Candy Controls
Re: Deleting Columns from StringGrid based on Column Title
« Reply #5 on: December 16, 2012, 01:16:26 am »
Quote
c) StringGrid.ColCount -1 is reported to be the same number of columns in the file I am loading. Whereas StringGrid.Columns.Count returns 0 for the same file. So if I use StringGrid.Columns.Count  the loop doesn't execute at all because the counter starts and ends at zero. But, from what you're saying,  StringGrid.ColCount -1 can only be used with fixed columned grids?
Yes, there are two ways how to use TStringGrid. With Columns or without them.
It seems that method LoadFromCSV does not use them.

So remove all Columns from code.
Use
Code: [Select]
StringGrid.ColCount
StringGrid.DeleteCol
StringGrid.Cells[i, 0]
instead of
Code: [Select]
StringGrid.Columns.Count
StringGrid.Columns.Delete
StringGrid.Columns[i].Title.Caption
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

Gizmo

  • Hero Member
  • *****
  • Posts: 831
Re: Deleting Columns from StringGrid based on Column Title
« Reply #6 on: December 16, 2012, 01:28:54 am »
Blaazen

You are my saviour again! That was it and now it works. All night I've meddled with this, only to have not realised that LoadFromCSVFile doesn't use the Columns atribute! D'Oh!!

Thanks again.

(For the benefit of other:

Code: [Select]
for i := StringGrid1.ColCount -1 downto 0 do 
      begin
        if (StringGrid1.Cells[i, 0] <> 'Value to Keep1') and
           (StringGrid1.Cells[i, 0] <> 'Value to Keep2') and
           (StringGrid1.Cells[i, 0] <> 'Value to Keep3')    and
           (StringGrid1.Cells[i, 0] <> 'Value to Keep4')    then
           begin
             StringGrid1.DeleteCol(i);  // Otherwise, delete the column
           end;
      end;             
)

 

TinyPortal © 2005-2018