Which Grid should I use?
lazarus / 31-LCL
----------------------------------------------------------------------
SteveF · 2010-12-01 19:14
I want to show the contents of a SQL table in a grid format on a form.
Here's the catch: I need to have all of the columns in the table
available for programming use, but I don't want to display all of
them. So, I need to be able to either selectively choose which
columns the grid uses, or be able to make certain columns "invisible"
or zero-width within the grid.
Which component best fits this use, and how do I go about setting the
component's properties to achieve this result?
TIA,
Steve
----------------------------------------------------------------------
BlueIcaro · 2010-12-01 22:53
Try Dbrid, you can find it on database control tab. You can add and
remove columns in design time and runtime.
/BlueIcaro
----------------------------------------------------------------------
Mando · 2010-12-01 23:13
I ASSUME YOU USE DBGRID.
I have the same problem. I have not yet implemented the solution but
this is the idea:
I made a table maintenance for my application, which uses a firebird
database on a server. It is a form where there is a grid and some
buttons. After I create an inherited form for each table I want to
keep, therefore, desire an abstract design of this form. So the
solution is to create the columns of the grid dinamically in the
form's Create event.
Also I want the user to set the grid so as to see the columns you want
by choosing a few available that do not have to match the columns in
the table.
My idea is to create another table (or file ini / xml) with the
information of the columns that display for each table used by the
application and another table (or file ini / xml) in which to save
user settings.
for example, a table called "Items", with these fields
ID
Description
Unit
Price
Join Date
Date Modified
I want to look at grid
Description
Unit
Price
to get what we want, we will have a table "Available Fields" that has
these fields:
User
TableName
FieldName
ColumnTitle
Visible
....
This table will be recorded this data:
TableNameIdxFieldNameColumnTitleVisible-------------------------------
--------------------------------------------Items0DescriptionItem
DescriptionTrueItems1UnitutTrueItems2PricePriceTrue
... and the other table....
UserTableNameIdxColWidthVisible---------------------------------------
-----*Items00100True*Items11100True*Items22100TruePepeItems00100TruePe
peItems11100FalsePepeItems2250True
where * in second table means "default configuration".
User "Pepe" see first and third column , and he changed the withd of
the third.....
When the form is created, I create the columns according to the
default (*) and then apply the changes for the user.
As I said, I have not yet implemented, therefore certain that requires
some tinkering.
I hope that fits, at least for inspiration.
----------------------------------------------------------------------
SteveF · 2010-12-02 19:31
Quote from: BlueIcaro on December 01, 2010, 11:53:53 pmTry Dbrid, you
can find it on database control tab. You can add and remove columns in
design time and runtime.
/BlueIcaro
DBgrid is what I've tried already. Where and how in design time can I
control visible columns?
Just as a personal comment, I've been finding it consistent and
frustrating that I often get responses like this one -- "You can add
and remove columns in design time and runtime" -- that say something
can be done, but supply no specific instructions on how to do it.
There's a similar frustration in finding suitable documentation and
tutorials covering various subjects.
Steve
----------------------------------------------------------------------
IPguy · 2010-12-02 22:35
I agree with SteveF.
I wonder how we could get the extended Lazarus community involved in
enhancing the documentation.
I keep thinking we should have a tips, tricks & hints section added to
the documentation to capture the little hints (such as Mando
started). As I browse the forum, there are tons of hints given by
many folks that are worth saving and publishing.
That would go a long way towards easing my learning curve and
frustration.
----------------------------------------------------------------------
José Mejuto · 2010-12-02 23:44
Quote from: SteveF on December 02, 2010, 08:31:57 pmQuote from:
BlueIcaro on December 01, 2010, 11:53:53 pmTry Dbrid, you can find it
on database control tab. You can add and remove columns in design time
and runtime.
/BlueIcaro
DBgrid is what I've tried already. Where and how in design time can I
control visible columns?
Hello,
I will not answer the rest of the message, it is uneeded...
Do you actualy tried to "look for" the solution with the exposed text
?
DBGrid.Columns[?].Visible:=false;
? = Column Index.
----------------------------------------------------------------------
SteveF · 2010-12-03 20:12
Quote from: JoshyFun on December 03, 2010, 12:44:29 amHello,
I will not answer the rest of the message, it is uneeded...
Do you actualy tried to "look for" the solution with the exposed text
?
DBGrid.Columns[?].Visible:=false;
? = Column Index.
I will try this when I get to the machine that has the project under
construction. I will note, however that this appears to be a run-
time/code-based solution, not design-time. It also does not address
the issue of setting column widths.
Steve
----------------------------------------------------------------------
José Mejuto · 2010-12-04 12:46
Quote from: SteveF on December 03, 2010, 09:12:26 pmI will try this
when I get to the machine that has the project under construction. I
will note, however that this appears to be a run-time/code-based
solution, not design-time. It also does not address the issue of
setting column widths.
Steve
Hello,
At design time DBGrid -> Columns -> (Choose column) -> Visible :-?
About the size, in a DBGrid the size is usually controlled by the
FieldSize which is available right clicking the linked TSQLQuery and
choosing the fields to "show" and its properties.
----------------------------------------------------------------------
SteveF · 2010-12-06 16:07
OK, I tried out both of these suggestions. For me, neither of them
seem to behave the way they do for you.
At design time DBGrid -> Columns -> (Choose column) -> Visible :-?
Choosing the Columns property for the DBGrid shows “0 items”.
Clicking the … button opens the attached dialog. No columns are
visible, and clicking the “Add button” adds one or more lines as
shown, with no apparent way to edit them. Guidance, please…
Right-clicking the TSQL query displays the attached context menu. The
only selection on the menu that seems applicable here is “Edit
Fields”. Choosing this opens a dialog that looks very similar to the
one for DBGrid.Columns, except this one doesn’t even have the
Add/Delete/Up/Down buttons; right-click does nothing here. More
guidance, please!
BTW, I’m using Lazarus 0.9.28.2/FPC 2.2.4 on Windows XP Professional
SP3, if that’s relevant.
Also, pursuant to my rant above, once I get this solved, I'll write up
& post how I got there so that others can use it.
Steve
----------------------------------------------------------------------
DougNettleton · 2010-12-07 16:02
Did you notice when you chose 1 - Title in DB Grid.Columns Dialog in
your attachment, the "Object Inspector" changed to display the
properties of the column?
In the version of Lazarus I have installed there are 19 major
properties and some sub- properties. FieldName, Visible, Width,
Alignment, and Title are probably what you're most interested in.
If you don't define any columns, i.e. you leave the columns as 0
items, the grid will display all the fields on your query / file with
a horizontal scroll bar if they don't all fit in the grid width. As
someone mentioned the column widths will default depending on the
attributes of the respective fields and the field name will be the
column header.
HTH,
Doug
----------------------------------------------------------------------
SteveF · 2010-12-07 16:13
Quote from: DougNettleton on December 07, 2010, 05:02:27 pmDid you
notice when you chose 1 - Title in DB Grid.Columns Dialog in your
attachment, the "Object Inspector" changed to display the properties
of the column?
In the version of Lazarus I have installed there are 19 major
properties and some sub- properties. FieldName, Visible, Width,
Alignment, and Title are probably what you're most interested in.
If you don't define any columns, i.e. you leave the columns as 0
items, the grid will display all the fields on your query / file with
a horizontal scroll bar if they don't all fit in the grid width. As
someone mentioned the column widths will default depending on the
attributes of the respective fields and the field name will be the
column header.
HTH,
Doug
Thank you, Doug -- that was the bit I was missing! I'll go try that
out & post back here. If it works, I think we can consider this
"solved".
Steve
----------------------------------------------------------------------
SteveF · 2010-12-09 16:40
Well, some progress has been made: the DB Grid is now only displaying
the columns I want to be shown. Thanks, Doug!
It's still not complete, though: the solution to setting the width of
the grid columns still eludes me. Setting the Display Width/Size of
the columns in the TSQLquery that is the grid's data source has no
effect at design time, although if there is a run-time solution I
would consider that since that's the method that worked for me to link
the grid as a detail display to the master items elsewhere on the
form.
Even more frustrating: there is a Width property in the Columns
collection for the grid that would seem to be the perfect solution.
BUT: editing the property is not allowed! I type in a value and it is
not accepted -- it instantly reverts to the value present before the
edit.
SO: Detailed step-by-step procedure for setting the width of DB Grid
columns is needed.
TIA,
Steve
----------------------------------------------------------------------
jesusr · 2010-12-09 17:55
Quote from: SteveF on December 09, 2010, 05:40:39 pmEven more
frustrating: there is a Width property in the Columns collection for
the grid that would seem to be the perfect solution. BUT: editing the
property is not allowed! I type in a value and it is not accepted --
it instantly reverts to the value present before the edit.
Have you set the grid property AutoFillColumns=true?, if so, columns
with SizePriority property set to 1 are automatically resized by the
grid (so they will ignore the value you set), if you want to control
the size of individual columns set SizePriority to 0 on such columns.
When AutoFillColumns is false, columns widths are not constrained.
----------------------------------------------------------------------
SteveF · 2010-12-10 16:25
Now, the final piece of this design-time puzzle is on place. Thank
you, jesusr for pointing to AutoFillColumns!
My DB Grid now looks & behaves the way I want it.
I've got more things to do on this project, so it's quite likely that
I'll be posting more questions; but this one is now solved.
Steve