Board index » delphi » add columns to dbgrid @ runtime

add columns to dbgrid @ runtime

How does one ADD columns to the dbgrid at runtime? I have scoured the
on-line help and the only mention there is to use dbgrid1.columns.add
method. Well, HOW does one do this (there are no specific examples in the
help NOR in the Developer's Guide)?!
My specifc situation is this:
The first three fields of the ACCESS database Table1 are known at
design-time. However, the user will open a database that will have
additional fields in that table. Table2 in the database will have the
fieldnames as record values and in the order we want them displayed in the
dbgrid (the order they appear in Table1 may not be that order). It is easy
enough to have ALL fields displayed (and in the order they appear in the
ACCESS table), but How to setup the dbgrid at runtime to add just the
additional columns and in the order we desire (the *fields* already exist
and do not need to be added)?
Any suggestions?

Thanks in advance.
Bill D. Richards
bill_richa...@nic.edu

 

Re:add columns to dbgrid @ runtime


Quote
"Bill D. Richards" wrote:
> How does one ADD columns to the dbgrid at runtime? I have scoured the
> on-line help and the only mention there is to use dbgrid1.columns.add
> method. Well, HOW does one do this (there are no specific examples in the
> help NOR in the Developer's Guide)?!
> My specifc situation is this:
> The first three fields of the ACCESS database Table1 are known at
> design-time. However, the user will open a database that will have
> additional fields in that table. Table2 in the database will have the
> fieldnames as record values and in the order we want them displayed in the
> dbgrid (the order they appear in Table1 may not be that order). It is easy
> enough to have ALL fields displayed (and in the order they appear in the
> ACCESS table), but How to setup the dbgrid at runtime to add just the
> additional columns and in the order we desire (the *fields* already exist
> and do not need to be added)?
> Any suggestions?

The columns of a dbGrid must be persistent for you to manipulate them at
runtime.  You do this using the "column editor", double clicking on the grid
at design time.  Here you can set the first three columns.

At runtime you can use the Grid.Colums property to add TColumn objects to the
grid.  Then you can change the properties of the newly created columns.

Something like this

{ Here your dbGrid already has 3 columns (from 0 to 2) }

YourGrid.Colums.Add;

{ Now it has one more col for a total of 4, from 0 to 3 }

YourGrid.Colums[3].FieldName := 'FieldName';
YourGrid.Colums[3].Alignment := taCenter;
...etc

Victor H. Ulloa
Tierra Fertil S.A.

Re:add columns to dbgrid @ runtime


Quote
Brian Bushay TeamB wrote in message <36dcf13d.37603973@floyd>...

>>How does one ADD columns to the dbgrid at runtime? I have scoured the
>>on-line help and the only mention there is to use dbgrid1.columns.add
>>method. Well, HOW does one do this

>//This one probably does not need an explanation

>dbgrid1.columns.add;

>--
>Brian Bushay (TeamB)
>Bbus...@NMPLS.com

Well, I'm sorry, but it *does*; for example, does one use:
a). dbgrid.columns.add(NameOfColumnToAdd)
or
b). dbgrid.columns.add      with some pre-defined set of
columns-somewhere-not-mentioned

etc.
...

Re:add columns to dbgrid @ runtime


Quote
>How does one ADD columns to the dbgrid at runtime? I have scoured the
>on-line help and the only mention there is to use dbgrid1.columns.add
>method. Well, HOW does one do this

//This one probably does not need an explanation

dbgrid1.columns.add;

--
Brian Bushay (TeamB)
Bbus...@NMPLS.com

Re:add columns to dbgrid @ runtime


On Sun, 28 Feb 1999 20:08:01 -0800, "Bill D. Richards"

Quote
<bill_richa...@nic.edu> wrote:

[...]

Quote
>Well, I'm sorry, but it *does*; for example, does one use:
>a). dbgrid.columns.add(NameOfColumnToAdd)
>or
>b). dbgrid.columns.add      with some pre-defined set of
>columns-somewhere-not-mentioned

The TDBGridColumns.Add method does two things: 1) adds a column to a
TDBGrid and 2) returns a reference to the new TColumn object.

The newly added column object is devoid of any property values and so is
not associated with any table column. You need to do that yourself after
calling the Add method. Set the various properties of TColumn so that the
newly added column is associated with either a table's column or a
persistent field object

One way to reference the newly added grid column is through the Add method
return value and a With..End construct:

  with DBGrid1.Columns.Add do begin
    Field := Table1LastName;
    FieldName := 'LastName';
    Title.Caption := 'Last Name';
    ...
  end;

Alternately, you can reference it through the TDBGrid.Columns property.
After the TDBGrid.Columns.Add method is called, the TDBGrid.Columns.Count
property's value is incremented. This number minus one (TDBGrid.Columns is
a zero-based collection) will point to the last created TColumn object.

  DBGrid1.Columns.Add;
  with DBGrid1.Columns[DBGrid1.Columns.Count - 1] do begin
    Field := Table1LastName;
    FieldName := 'LastName';
    Title.Caption := 'Last Name';
    ...
  end;

//////////////////////////////////////////////////////////////////////////
Steve Koterski                    "My problem lies in reconciling my gross
Technical Publications            habits with my net income."
INPRISE Corporation                             -- Errol Flynn (1909-1959)
http://www.borland.com/delphi

Re:add columns to dbgrid @ runtime


Quote
Steve Koterski wrote in message <36dae28f.8770...@forums.inprise.com>...

>The TDBGridColumns.Add method does two things: 1) adds a column to a
>TDBGrid and 2) returns a reference to the new TColumn object.
>[clip]

Thanks for the explanation. As a long-time Fortran programmer, I have yet to
really grasp the intuitive parts of ObjectPascal. However, your explanation
is right-on-the-mark for what I need to accomplish!
Regards,

Bill

Other Threads