Solved

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

Posted on 2004-09-07
23
4,493 Views
Last Modified: 2009-10-10
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
0
Comment
Question by:czechmate
  • 8
  • 5
  • 4
  • +4
23 Comments
 
LVL 2

Expert Comment

by:gary_williams
ID: 11996178
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
 
LVL 1

Author Comment

by:czechmate
ID: 11996228
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
 
LVL 2

Expert Comment

by:gary_williams
ID: 11996279
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
 
LVL 12

Expert Comment

by:esoftbg
ID: 11996324
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
 
LVL 11

Expert Comment

by:calinutz
ID: 11996366
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
 
LVL 1

Author Comment

by:czechmate
ID: 11996370
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
 
LVL 27

Expert Comment

by:kretzschmar
ID: 11996429
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
 
LVL 1

Author Comment

by:czechmate
ID: 11996433
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
 
LVL 12

Expert Comment

by:esoftbg
ID: 11996541
You don't need to resize from 8 down to 7 columns, you may use 8'th column when the ColCount is 7 ....
0
 
LVL 27

Assisted Solution

by:kretzschmar
kretzschmar earned 100 total points
ID: 11996573
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
 
LVL 12

Assisted Solution

by:esoftbg
esoftbg earned 100 total points
ID: 11996578
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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 12

Expert Comment

by:esoftbg
ID: 11996586
By above code you may hide not only one column, but a set of columns !!!!
0
 
LVL 1

Author Comment

by:czechmate
ID: 11997242
This is beginning to look good guys, let me do some testing now, I'll come back later...
0
 
LVL 2

Expert Comment

by:gary_williams
ID: 11997709
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
 
LVL 22

Expert Comment

by:Ferruccio Accalai
ID: 11997741
--> if (ACol in [1,3,5]) then
    StringGrid.ColWidths[ACol] := -1;

Good shot esoftbg :)

F68 ;-)
0
 
LVL 1

Author Comment

by:czechmate
ID: 11998093
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
 
LVL 1

Author Comment

by:czechmate
ID: 11998163
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
 
LVL 4

Expert Comment

by:mottor
ID: 11998308
May be i didn't understand something,
Why don't do the column fixed. You cannot resize fixed column with mouse

Regards,
mottor
0
 
LVL 2

Accepted Solution

by:
gary_williams earned 100 total points
ID: 11999950
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
 
LVL 1

Author Comment

by:czechmate
ID: 12000564
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
 
LVL 22

Expert Comment

by:Ferruccio Accalai
ID: 12000669
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
 
LVL 2

Expert Comment

by:gary_williams
ID: 12000735
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
 
LVL 1

Author Comment

by:czechmate
ID: 12001609
Not sure how the points split works, hope I'll figure it out:)
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now