1. Computing

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

By

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

©2014 About.com. All rights reserved.