Lock the first column of a DBGrid?

I am trying to lock the first column of a DBGrid so that when the user scrolls to the right, the first column will stay in view.  Is there an easy way to do this without writing a new component?  If you can help, I'd appreciate it.

Thanks,
Scott
LVL 1
millerwAsked:
Who is Participating?
 
OzBodConnect With a Mentor Commented:
The easiest way to solve your problem is to make a descendant of TDBGrid which gives access to the FixedCols property. This is a simple one line adjustment in the descended Grid as shown below :


unit Mydbgrid;

interface

uses
  SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,Forms, Dialogs, Grids,DBGrids,DB,DBTables;

type
  TMYDBGRID = class(TDBGrid)
  private
    { Private declarations }

  protected
    { Protected declarations }

  public
    { Public declarations }

    property FixedCols;   {this is the only change needed}


  published
    { Published declarations }
   


  end;

procedure Register;

implementation

procedure Register;
begin
  RegisterComponents('DBControls', [TMYDBGRID]);
end;


end.


I would recommend creating a descendant, initially starting with the FixedCols property. This will form a kickstart to making other modifications down the line to compensate for some of the shortfalls in TDBGrid that you will invariably come across with further exposure.

Note : Do not include the FixedCols property in the published section. This temptation to be able to set FixedCols at design time comes unstuck if at runtime the Table attached to the Grid is not opened. This results in an error message which states that 'FixedCols greater than total columns' or something to that effect. If you programmatically set FixedCols after you are sure the Table is opened then this will not cause a problem
0
 
millerwAuthor Commented:
Thanks.  It does work.  I finally figured out that the FixedCols must be set to the value of the column AFTER the column you want to make fixed.  Other than that little missing information your explaination was excellent.  

Thanks again,
Scott
0
 
OzBodCommented:
Not quite sure what you meant by setting the value of the column after the column you want fixed ??? Did you allow for the cloumn that shows the position indicator, which would be column 0

Anyway, glad it was of use
0
 
millerwAuthor Commented:
Yes I took it into consideration.  What I meant is I was trying to set the value to 1 for my first column so that it would remain fixed.  That didn't work, so I tried 0 and that told me "Index out of Range."  Having nothing else to try but a bigger number, I tried 2 and that made the columns 2 on NOT fixed and the columns under 2 FIXED (e.g. my first column and the position indicator).  That is the only catch to using what you told me.  It only took me about 10 min with all the compiling, testing, and exception handling to figure it out.

Thanks once again.

BTW, mind if I call on you sometime for help later?  I am writing my first database program in Delphi and the docs suck when it comes to database stuff.  I've had no problems with other stuff but the database if giving me problems.  Do you have any hints/books/suggestions on where i can find come good information or what to watch out for in database progamming?  One last thing, do you have any data-aware components that you have written so that I can look at how you did it?  I have read the book but I'm still not sure if I have all the information I need.

Talk to you later,
Scott
0
 
OzBodCommented:
Sure, contact me anytime. I can't 'guarantee' that I can answer all your questions but certainly will if I can. I should mention that I almost exclusively use dBase files for a variety of reasons and have skipped using BDE by buying Apollo by Successware. As an ex-Clipper programmer using dBase files I find Apollo shortcuts all my problems with databases and has some lovely little features that save me a stack of time in re-programming.

I've always had difficulty finding a good source that deals with database programming, unless you are using Clent/Server. One book I can recommend for topics that cover a wide range and is extremely useful is Niel Rubenking's "Delphi - Programming Problem Solver". Also suggest you check out the Freeware rxLib component collection which can be found at Delphi Super Page. Some lovely ones useful for DBAware components.

As for my own, they are a mix-mash of ideas from various sources which solve my immediate problems at the time. I am reticent to release them as I cannot guarantee them working on different development setups.

Anyway, keep on Delphi-ing... the final result is worth the effort.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.