Recent

Author Topic: Which Grid should I use?  (Read 12284 times)

SteveF

  • Jr. Member
  • **
  • Posts: 92
Which Grid should I use?
« on: December 01, 2010, 08:14:00 pm »
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

  • Hero Member
  • *****
  • Posts: 792
    • Blog personal
Re: Which Grid should I use?
« Reply #1 on: December 01, 2010, 11:53:53 pm »
Try Dbrid, you can find it on database control tab. You can add and remove columns in design time and runtime.
/BlueIcaro

Mando

  • Full Member
  • ***
  • Posts: 181
Re: Which Grid should I use?
« Reply #2 on: December 02, 2010, 12:13:55 am »
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 DescriptionTrue
Items1UnitutTrue
Items2PricePriceTrue

... and the other table....

UserTableNameIdxColWidthVisible
--------------------------------------------
*Items00100True
*Items11100True
*Items22100True
PepeItems00100True
PepeItems11100False
PepeItems2250True

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.
« Last Edit: December 02, 2010, 09:47:48 am by Mando »

SteveF

  • Jr. Member
  • **
  • Posts: 92
Re: Which Grid should I use?
« Reply #3 on: December 02, 2010, 08:31:57 pm »
Try 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?

<rant on>
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.
</rant off>

Steve

IPguy

  • Sr. Member
  • ****
  • Posts: 385
Re: Which Grid should I use?
« Reply #4 on: December 02, 2010, 11:35:30 pm »
<rant on>
I agree with SteveF. 
<rant off>

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

  • Full Member
  • ***
  • Posts: 136
Re: Which Grid should I use?
« Reply #5 on: December 03, 2010, 12:44:29 am »
Try 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

  • Jr. Member
  • **
  • Posts: 92
Re: Which Grid should I use?
« Reply #6 on: December 03, 2010, 09:12:26 pm »
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.

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

  • Full Member
  • ***
  • Posts: 136
Re: Which Grid should I use?
« Reply #7 on: December 04, 2010, 01:46:49 pm »
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

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

  • Jr. Member
  • **
  • Posts: 92
Re: Which Grid should I use?
« Reply #8 on: December 06, 2010, 05:07:44 pm »
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

  • Jr. Member
  • **
  • Posts: 84
Re: Which Grid should I use?
« Reply #9 on: December 07, 2010, 05:02:27 pm »
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

  • Jr. Member
  • **
  • Posts: 92
Re: Which Grid should I use?
« Reply #10 on: December 07, 2010, 05:13:06 pm »
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

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

  • Jr. Member
  • **
  • Posts: 92
Re: Which Grid should I use?
« Reply #11 on: December 09, 2010, 05:40:39 pm »
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

  • Sr. Member
  • ****
  • Posts: 484
Re: Which Grid should I use?
« Reply #12 on: December 09, 2010, 06:55:09 pm »
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.

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

  • Jr. Member
  • **
  • Posts: 92
Re: Which Grid should I use?
« Reply #13 on: December 10, 2010, 05:25:20 pm »
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

 

TinyPortal © 2005-2018