Hide a StringGrid column so that it cannot be made visible by changing its width with mouse?

In my application I hide the first StringGrid Column by setting its ColWidth to -1.  It hides the column OK but the column can be made visible again by changing its width with a mouse.  I searched Delhi help for an event something like 'OnWidthChange' to prevent the width change but no success.  

So the question is:
How do you hide a StringGrid column so that it cannot be 'unhidden'?

Thanks
cj
LVL 1
czechmateAsked:
Who is Participating?
 
gary_williamsConnect With a Mentor Commented:
Create a new component derived from TStringGrid:

  TEnhStringGrid = class (TStringGrid)
  private
    FOnColWidthsChanged: TNotifyEvent;
  protected
    procedure ColWidthsChanged; override;
  published
    property OnColWidthsChanged: TNotifyEvent read FOnColWidthsChanged;
  end;

procedure TEnhStringGrid.ColWidthsChanged;
begin
  inherited;

  if (Assigned(OnColWidthsChanged)) then
    OnColWidthsChanged(Self);  
end;

Compile and install the component, then create an event handler on your form for OnColWidthsChanged, in which you force ColWidths[1] to -1.

0
 
gary_williamsCommented:
What would be the point of permanently hiding a column?  If you need to store metadata for each row somewhere, it would be conceptually cleaner to store it inside an object assigned to (for example) StringGrid.Objects[0, Row].
0
 
czechmateAuthor Commented:
I load csv file into the Grid.  The first column of csv file holds and ID string which is not relevant to the user.  I can easily ignore that data during the file load, in fact that's the way it has worked up to now.  But that means another customised reading of file data,  I thought it would be much cleaner to read the lot in a standard way then hide a column.  The same goes for saving the file.
0
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

 
gary_williamsCommented:
Trying to hide a column would be introducing a fragile hack that might break if the user does something unexpected, or the VCL changes when you upgrade your compiler.

How about modifying your CSV-importing procedure to accept a parameter containing a list of columns to ignore?
0
 
esoftbgCommented:
if you have StringGrid.ColCount := 8; // Columns from 0 up to 7
then you can store invisible information into the Column 8 :

example:

procedure TForm1.FormCreate(Sender: TObject);
begin
  StringGrid.Cells[8,1] := '8,1';
  StringGrid.Cells[8,2] := '8,2';
  StringGrid.Cells[8,3] := '8,3';
  StringGrid.Cells[8,4] := '8,4';
  StringGrid.Cells[8,5] := '8,5';
  StringGrid.Cells[8,6] := '8,6';
  StringGrid.Cells[8,7] := '8,7';
end;

procedure TForm1.SpeedButton1Click(Sender: TObject);
begin
  if StringGrid.ColCount=8 then
    StringGrid.ColCount := 9
  else
    StringGrid.ColCount := 8;
end;
0
 
calinutzCommented:
You should use KLib's free library with -  KStringGrid
 It allows you to hide any of the columns you want like this:

ks.Columns.Items[0].Caption:='1';
ks.Columns.Items[1].Caption:='2';
ks.Columns.Items[2].Caption:='3';
ks.AddRows(10);

ks.Columns.Items[1].Visible:=false;

where  ks: TKStringGrid;
You will find a lot of usefull stuff in this KStringGrid (a lot more options that the usual StringGrid). A lot more customisable.
You can find it on www.torry.net, or directly on the homepage:
http://www.korzh.com/download/kstrgrid.zip
Carefull...  kprocs library should be installed first. You can also find it there
Cheers.
0
 
czechmateAuthor Commented:
Garry I appreciate what you are saying, as I mentioned I do have procedure that loads the csv data without the first column.  The point is I would prefer to hide the column if it can be done.  I do have a TAdvStringGrid component that has the Hide property, but the component adds approx. 300kb to my application, I'd prefer to use Delphi's TStringGrid.
0
 
kretzschmarCommented:
usual i would do it this way (schematic):

- as a col is a stringlist, you can easily assign it to another stringlist (cache is needed)
- after caching you can move (also assign) all other cols in the grid
- decrement colcount

later a by head sample

meikl ;-)
0
 
czechmateAuthor Commented:
esoftbg,
am I right you mean:  if I resize the grid from let's say 8 columns to 7 and then back, the column 8 keeps the data it held before the column was dropped?  I think that's just incidental and may not work in future.
0
 
esoftbgCommented:
You don't need to resize from 8 down to 7 columns, you may use 8'th column when the ColCount is 7 ....
0
 
kretzschmarConnect With a Mentor Commented:
well, my by head sample (not tested, no delphi available)

procedure hideColumn(AStringGrid : TStringGrid; ACol : Integer; ACacheList : TStrings);
var i : integer;
begin
  if (assigned(AStringGrid)) and
     (assigned(ACacheList)) and
     (ACol > -1) and (ACol < AStringGrid.ColCount) then
  begin
    ACacheList.Assign(AStringGrid.cols[ACol]);  //save Data
    for i := ACol + 1 to AStringGrid.ColCount - 1 do  //move data
      AStringGrid.cols[i-1].assign(AStringGrid.cols[i]);
    AStringGrid.ColCount := AStringGrid.ColCount - 1;  //remove last column
  end
  else raise exception.Create('Invalid Parameters');
end;

procedure showColumn(AStringGrid : TStringGrid; ACol : Integer; ACacheList : TStrings);
var i : integer;
begin
  if (assigned(AStringGrid)) and
     (assigned(ACacheList)) and
     (ACol > -1) and (ACol <= AStringGrid.ColCount) then
  begin
    AStringGrid.ColCount := AStringGrid.ColCount + 1;  //add last column
    for i := AStringGrid.ColCount - 1 downto ACol do  //move data
      AStringGrid.cols[i].assign(AStringGrid.cols[i-1]);
    ACacheList.Assign(AStringGrid.cols[ACol]);  //restore Data
  end
  else raise exception.Create('Invalid Parameters');
end;
 
 
//usage (for moving);
var
  sl : TStringList;  
begin
  sl := TstringList.Create;  
  try
    hideColumn(StringGrid1,2,sl);
    showColumn(StringGrid1,4,sl);
  finally
    sl.free
  end;
end;
   
   
hope this helps

meikl ;-)
0
 
esoftbgConnect With a Mentor Commented:
a fantastic solution is:

procedure TForm1.StringGridDrawCell(Sender: TObject; ACol, ARow: Integer; Rect: TRect; State: TGridDrawState);
begin
  if (ACol in [1,3,5]) then
    StringGrid.ColWidths[ACol] := -1;
end;
0
 
esoftbgCommented:
By above code you may hide not only one column, but a set of columns !!!!
0
 
czechmateAuthor Commented:
This is beginning to look good guys, let me do some testing now, I'll come back later...
0
 
gary_williamsCommented:
If you really want to "clamp" the ColWidths to -1 at runtime, there is a protected procedure on the grid called ColWidthsChanged which you can override (and call "inherited" within), and then you can place your code there instead of in the OnDrawCell handler.  But the hassle may not be worth it to you.
0
 
Ferruccio AccalaiSenior developer, analyst and customer assistance Commented:
--> if (ACol in [1,3,5]) then
    StringGrid.ColWidths[ACol] := -1;

Good shot esoftbg :)

F68 ;-)
0
 
czechmateAuthor Commented:
gary_williams,
yes, I want the hassle:)  can you show me how to do it?

esoftbag,
you are on the right track, this is essentially what I wanted.  

0
 
czechmateAuthor Commented:
kretzschmar,
yours is an interesting exercise in TStrings usage, I appreciate it.  It works well (almost:), damn good head shot.  One has to add to it management (save and restore) of column width...it then becomes a bit of too much effort:)
0
 
mottorCommented:
May be i didn't understand something,
Why don't do the column fixed. You cannot resize fixed column with mouse

Regards,
mottor
0
 
czechmateAuthor Commented:
garry_williams,
I tried your stuff it compiles OK, but the OnColWidthChanged event does not show in object inspector.  Obviously I'm doing something daft, this is what I have:

~~~~~~~~~~~~~~~~~~~~~~~~~~
unit EnhStringGrid;

interface

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

type
  TEnhStringGrid = class(TStringGrid)
  private
    FOnColWidthsChanged: TNotifyEvent;
  protected
    procedure ColWidthsChanged; override;
  public
   {public declarations}
  published
    property OnColWidthsChanged: TNotifyEvent
      read FOnColWidthsChanged;
  end;


procedure Register;

implementation

procedure TEnhStringGrid.ColWidthsChanged;
begin
  inherited;

  if (Assigned(OnColWidthsChanged)) then
    OnColWidthsChanged(Self);
end;

procedure Register;
begin
  RegisterComponents('CJs Stuff', [TEnhStringGrid]);
end;
end.
~~~~~~~~~~~~~~~~~~~~~~~~~~

I can place the component on a form and compile the form,  I am not sure what is missing.  I have zero experience with components.  Can you help me with this?

 I will increase the points and share them between contributors so nobody comes short.  Thanks for the effort guys.
cj
0
 
Ferruccio AccalaiSenior developer, analyst and customer assistance Commented:
i'm not looking for points with this, it's just to help.

change to the the following:

type
  TEnhStringGrid = class(TStringGrid)
  private
    FOnColWidthsChanged: TNotifyEvent;
  protected
    procedure ColWidthsChanged; override;
  public
   {public declarations}
  published
    property OnColWidthsChanged: TNotifyEvent
      read FOnColWidthsChanged write FOnColWidthsChanged; //here the write were missed
  end;
0
 
gary_williamsCommented:
Oops!

I forgot the "write" clause of the property.

    property OnColWidthsChanged: TNotifyEvent read FOnColWidthsChanged write FOnColWidthsChanged;

Make this change and reinstall the component, then you can set the property.

Don't worry about the "points".  I am just as likely to answer a 1-point question as a 500-point question.  In EE's silly system, answer points have no value whatsoever.
0
 
czechmateAuthor Commented:
Not sure how the points split works, hope I'll figure it out:)
0
All Courses

From novice to tech pro — start learning today.