Delphi Programming

  1. Home
  2. Computing & Technology
  3. Delphi Programming

How to allow column resize but disable movement (in TDBGrid)

By Zarko Gajic, About.com

When column resizing is enabled for a DBGrid (by default, Options property includes the "dgColumnResize" flag) users of your application can also rearrange (move) the columns.

If you want to, for any reason, enable column resizing but disable column moving, you'll find out that the Options property of the TDBGrid does not provide a flag to (un)set.

In order to disable column move operation you'll need to hack the TDBGrid a little.

Place the next code in the Form's OnCreate event handler (suppose "DBGrid1" is on this form):

~~~~~~~~~~~~~~~~~~~~~~~~~
with TCustomGrid(DBGrid1) do
     Options := Options - [goColMoving];
~~~~~~~~~~~~~~~~~~~~~~~~~

Note 1: The TDBGrid inherits from TCustomGrid. The Options property of the TCustomGrid includes the "goColMoving" flag, but the Options property from the TDBGrid does not. If you down-cast the DBGrid to TCustomGrid you can remove the "goColMoving".
Note 2: this code uses the so-called Protected Hack.

Delphi tips navigator:
» Fixing the "Duplicate resource" error in Delphi
« How to check if a document in a TWebBrowser is located on a local (or network) drive

Zarko Gajic
Guide since 1998

Zarko Gajic
Delphi Programming Guide

Explore Delphi Programming

More from About.com

Delphi Programming

  1. Home
  2. Computing & Technology
  3. Delphi Programming
  4. Coding Delphi Applications
  5. Delphi Tips and Tricks
  6. 2005 Delphi Tips
  7. How to allow column resize but disable movement (in TDBGrid)

©2008 About.com, a part of The New York Times Company.

All rights reserved.