Board index » delphi » TDBGrid

TDBGrid

Are there any way to select whitch fields to be shown in the DBGrid.
(I do not whant all fields in the table)
/Stefan
 

Re:TDBGrid


Quote
u...@helsingborg.se (Stefan Andersson) wrote:
>Are there any way to select whitch fields to be shown in the DBGrid.
>(I do not whant all fields in the table)
>/Stefan

when you have dropped your TTable & TDatasource components on your
form, double-click on the TTable component.
click on the Add button of the new form (fields list), and add all the
fields of your table.
then, click on a field and set its visible attribute to false or true,
depending on if you want it to be visible or not.

then when you will set the datasource property of your DBGrid, only
the visible fields will appear.

hope this helps.

guillaume

Re:TDBGrid


This sems to be a good solution, but can this be don at runtime?
/Stefan

Re:TDBGrid


Quote
u...@helsingborg.se (Stefan Andersson) wrote:
>This sems to be a good solution, but can this be don at runtime?
>/Stefan

yes, if the fields are declared at design time.
ie : you have a table with two fields :

Table1
        field1 (String)
        field2 (Float)

in your code, you will have something like that :

        TFormx = class(TForm)
                  [... other stuff...]
                  Table1 : TTable;
                  Table1FIELD1 : TStringField;
                  Table1FIELD2 : TFloatField;
                  [... other stuff...]
        end;

Then, you can access any properties of the field.

To declare these field in your code, just double-click on the TTable
component.

guillaume

Re:TDBGrid


In <4c6106$...@news.helsingborg.se> u...@helsingborg.se (Stefan
Quote
Andersson) writes:

>Are there any way to select whitch fields to be shown in the DBGrid.
>(I do not whant all fields in the table)
>/Stefan

Double click the tTable object.  Select ADD and add all the fields.
When the screen comes back remove those fields you don't want.  In the
case of the primary key just highlight that field and go to the Object
Inspector.  Set the Visible property to false.  You can also change the
Display lable in the Object Inspector.
Ben
Arrow

Re:TDBGrid


Quote
In article <4c6106$...@news.helsingborg.se> u...@helsingborg.se (Stefan Andersson) writes:
>Are there any way to select whitch fields to be shown in the DBGrid.
>(I do not whant all fields in the table)
>/Stefan

The fields of a grid are defined by Grid.Fields[I].

To eliminate the field from the grid set Grid.Fields[I].Visible to true.
To set a new width set Grid.Fields[I].Displaywidth.

WARNING:  Only visible fields are included in the Grid.Fieldcount, and
disabling a field _immediately_ removes it from the list, ie the index you
have used now points to the next field.  Similarly, changing the .Index
property changes the order immediately.

How you get fields back, I don't know.  Anyone?

Andrew
==+==+==+==+==+==+==+==+==+==+==+==+==+==+==+==+==+==+==+==+==+==+==
Andrew Gabb                     Andrew.G...@dsto.defence.gov.au
Information Technology Division
Defence Science and Technology Organisation
Ph  +61 8 259 5505    Fax +61 8 259 5980
171 Labs ITD/DSTO, P.O. Box 1500, Salisbury S.A., Australia 5108

Re:TDBGrid


In <Andrew.Gabb.563.30EC8...@dsto.defence.gov.au>

Quote
Andrew.G...@dsto.defence.gov.au (Andrew Gabb) writes:

>In article <4c6106$...@news.helsingborg.se> u...@helsingborg.se (Stefan
Andersson) writes:

>>Are there any way to select whitch fields to be shown in the DBGrid.
>>(I do not whant all fields in the table)
>>/Stefan

>The fields of a grid are defined by Grid.Fields[I].

>To eliminate the field from the grid set Grid.Fields[I].Visible to
true.
>To set a new width set Grid.Fields[I].Displaywidth.

>WARNING:  Only visible fields are included in the Grid.Fieldcount, and
>disabling a field _immediately_ removes it from the list, ie the index
you
>have used now points to the next field.  Similarly, changing the
Index
>property changes the order immediately.

>How you get fields back, I don't know.  Anyone?

>Andrew
>==+==+==+==+==+==+==+==+==+==+==+==+==+==+==+==+==+==+==+==+==+==+==
>Andrew Gabb                         Andrew.G...@dsto.defence.gov.au
>Information Technology Division
>Defence Science and Technology Organisation
>Ph  +61 8 259 5505    Fax +61 8 259 5980
>171 Labs ITD/DSTO, P.O. Box 1500, Salisbury S.A., Australia 5108

Double click the tTable component, add all the fields then use the
object inspector by highlighting a field to select its width and
visiblity.  You can also remove fields form the list (not the primary
key) when you double click the tTable component.
Ben
Arrow

Re:TDBGrid


The easiest way is to double clickthe table or sql icon and then click
add. This gives a list of fields and selecting them allows you to
transfer the data to a variable the properties and events you can set.
One of the propoerties is visible so this can be set bot at design and
run time. Display properties and formating are also available.

Hope this helps

In article <Andrew.Gabb.563.30EC8...@dsto.defence.gov.au>, Andrew Gabb
<Andrew.G...@dsto.defence.gov.au> writes

Quote
>In article <4c6106$...@news.helsingborg.se> u...@helsingborg.se (Stefan
>Andersson) writes:

>>Are there any way to select whitch fields to be shown in the DBGrid.
>>(I do not whant all fields in the table)
>>/Stefan

>The fields of a grid are defined by Grid.Fields[I].

>To eliminate the field from the grid set Grid.Fields[I].Visible to true.
>To set a new width set Grid.Fields[I].Displaywidth.

>WARNING:  Only visible fields are included in the Grid.Fieldcount, and
>disabling a field _immediately_ removes it from the list, ie the index you
>have used now points to the next field.  Similarly, changing the .Index
>property changes the order immediately.

>How you get fields back, I don't know.  Anyone?

>Andrew
>==+==+==+==+==+==+==+==+==+==+==+==+==+==+==+==+==+==+==+==+==+==+==
>Andrew Gabb                            Andrew.G...@dsto.defence.gov.au
>Information Technology Division
>Defence Science and Technology Organisation
>Ph  +61 8 259 5505    Fax +61 8 259 5980
>171 Labs ITD/DSTO, P.O. Box 1500, Salisbury S.A., Australia 5108

--
William M. Buist

Re:TDBGrid


What is the most correct way of solving the TDBGrid DisplayWidth problem?

A. By changing Grid.Fields
or
B. By using then Fields Editor and change the Display width of a TField
descendant.

Harald

Re:TDBGrid


Quote
>WARNING:  Only visible fields are included in the Grid.Fieldcount, and
>disabling a field _immediately_ removes it from the list, ie the index you
>have used now points to the next field.  Similarly, changing the .Index
>property changes the order immediately.

>How you get fields back, I don't know.  Anyone?

>Andrew

What you says about the Grid.Fields[|] and Grid.Fields[|].DisplayWidth worked
out on my computer. And when Visible is set to FALSE it's removed from the
fields list.
So what I wanted was a way to toggle a fields "visibility".

Here comes the solution:

Imagine a DBGrid displaying all the columns from the CUSTOMERS.DB in from the
DBDEMOS alias.
I put up a TTable, TDataSource, DBGrid, and a panel bottom aligned under the
grid. Then I placed one checkbox on the panel, one for each field.
My goal was to toggle the checkmarks and trace the OnClick event which I put
equal for all the checkboxes.
The event handler checks every checkbox's Checked property, which toggles the
desired field/columns visible property.
Did I say toggle? In order to acctualy toggle the visible property and bring
the Grid.Fields[|].Visible back and from I needed to create a TField
descendant, one for each field in the table, and instead toggle the TField
objects Visible property. This will refresh the DBGrid fields.

Harald

Other Threads