Recent

Author Topic: Are there any friendly grid controls?  (Read 28042 times)

Nebula

  • Jr. Member
  • **
  • Posts: 88
Are there any friendly grid controls?
« on: September 18, 2012, 03:15:04 pm »
From what I've found so far, it seems that even the simplest of changes to a grid control require a whole new event procedure to modify the cell drawing, by hand. That brings back bad memories of a nightmare reporting product I used to work with, and I'm not keen to suffer that level of mucking about again!

In contrast, in vb.net I can plonk a DataGridView control on a form, set the control to display records from a SQL Select with one line  : DataGridView1.DataSource = sqlDataTable

.. I can set alternating line colours with one property
DataGridView1.AlternatingRowsDefaultCellStyle.BackColor = Color.Lavender

.. and format a numerical column or set the alignment with single lines of code, like
DataGridView1.Columns(2).DefaultCellStyle.Format = "dd.MM.yyyy"

PLEASE tell me there's something as friendly to use in Lazarus!  :)
Newbie testing Lazarus v1.0 - very impressed
Win 7 at work, XP and Linux Mint at home.
It all started with a ZX80 on a b/w telly........
Code: [Select]
Uses Smiles, GoodHumour, WantsToHelp;
{ never liked C - curly brackets are for comments! }

Dibo

  • Hero Member
  • *****
  • Posts: 1057
Re: Are there any friendly grid controls?
« Reply #1 on: September 18, 2012, 03:48:32 pm »
You can try KGrid demos. Never tested but screens looks promising:
http://www.tkweb.eu/en/delphicomp/kgrid.html
Or virtualtreeview with virtualDBtreeview extension:
http://wiki.freepascal.org/VirtualTreeview

patyi

  • Full Member
  • ***
  • Posts: 168
Re: Are there any friendly grid controls?
« Reply #2 on: September 18, 2012, 03:50:45 pm »
Yes !
It is DBGrid (Data Controls tab), you can define property almost exactly that way ...    :D

Knipfty

  • Full Member
  • ***
  • Posts: 232
Re: Are there any friendly grid controls?
« Reply #3 on: September 18, 2012, 04:18:03 pm »
I find that DBGrid works very well.
64-bit Lazarus 2.2.0 FPC 3.2.2, 64-bit Win 11

Dibo

  • Hero Member
  • *****
  • Posts: 1057
Re: Are there any friendly grid controls?
« Reply #4 on: September 18, 2012, 05:01:47 pm »
I have no objections to TDBGrid, I just posted some alternatives which are not included in standard lazarus build.
Personally I think that TDGBrid works very well. OnCustomDrawn event should not scare anyone. Changing cell / columns color in this event is just two lines of code (or one without conditions  :) )

Nebula

  • Jr. Member
  • **
  • Posts: 88
Re: Are there any friendly grid controls?
« Reply #5 on: September 19, 2012, 11:17:17 am »
Thanks, I'll take a look at TDBGrid... I just wish there were more code snippets floating around online.
I guess I've been spoiled with vb.net, having MSDN, stackoverflow.com, sundry forums and blogs - so many people chipping in to the public knowledgebase. I hope more Laz/FPC/OP code examples get onto the net for people to crib from.

For instance, in vb.net I can connect to an MS-SQL server and show a SQL query results table as easily as this, having added a DataGridView control to the form :

Code: [Select]
Dim sqlConnection As New SqlClient.SqlConnection("server=TESTSERVER; database=TESTDB; Integrated Security=SSPI")
Dim sqlAdapter As New SqlClient.SqlDataAdapter("SELECT * from TestTable", sqlConnection)
Dim sqlDataTable As New DataTable
sqlAdapter.Fill(sqlDataTable)
' fill in the grid and tweak some appearances ...
DataGridView1.DataSource = sqlDataTable
DataGridView1.AlternatingRowsDefaultCellStyle.BackColor = Color.Lavender
DataGridView1.Columns(2).DefaultCellStyle.Format = "dd.MM.yyyy"

I found all that from online searches, and verified for myself that it worked, well within one working day.
With Lazarus, I'm still searching and scratching my head!

Would anyone please care to be a star and translate the above into Lazarus-speak?  :D
Newbie testing Lazarus v1.0 - very impressed
Win 7 at work, XP and Linux Mint at home.
It all started with a ZX80 on a b/w telly........
Code: [Select]
Uses Smiles, GoodHumour, WantsToHelp;
{ never liked C - curly brackets are for comments! }

BigChimp

  • Hero Member
  • *****
  • Posts: 5740
  • Add to the wiki - it's free ;)
    • FPCUp, PaperTiger scanning and other open source projects
Re: Are there any friendly grid controls?
« Reply #6 on: September 19, 2012, 12:21:39 pm »
Want quicker answers to your questions? Read http://wiki.lazarus.freepascal.org/Lazarus_Faq#What_is_the_correct_way_to_ask_questions_in_the_forum.3F

Open source including papertiger OCR/PDF scanning:
https://bitbucket.org/reiniero

Lazarus trunk+FPC trunk x86, Windows x64 unless otherwise specified

ttomas

  • Sr. Member
  • ****
  • Posts: 250
Re: Are there any friendly grid controls?
« Reply #7 on: September 19, 2012, 03:34:17 pm »
Thanks, I'll take a look at TDBGrid... I just wish there were more code snippets floating around online.
You can watch this video tutorials
http://www.youtube.com/watch?v=pq2oCiJePHo
Also you can install RxLib and look TRxDBGrid, advance version of TDBGrid, with footer, sum functions etc.
« Last Edit: September 19, 2012, 03:36:49 pm by ttomas »

Nebula

  • Jr. Member
  • **
  • Posts: 88
Re: Are there any friendly grid controls?
« Reply #8 on: September 21, 2012, 02:30:43 pm »
Thanks, but can I populate a grid with data from a SQL Query, without a data control on the form (which seems weird to me unless using data-aware controls?)

I've got a manual ODBC SQL query working, and query.Fields is populated, etc.

This compiles :
Code: [Select]
dbgrid1.DataSource.DataSet := query.Fields.Dataset; - as both are the same type, but it crashes when I run it.

So is there an equiv. to VB's simple one-liner "DataGridView1.DataSource = myDataTableQueryResults" or would I need to manually loop around the query.Dataset and append each row to the grid?

Thanks
Newbie testing Lazarus v1.0 - very impressed
Win 7 at work, XP and Linux Mint at home.
It all started with a ZX80 on a b/w telly........
Code: [Select]
Uses Smiles, GoodHumour, WantsToHelp;
{ never liked C - curly brackets are for comments! }

BigChimp

  • Hero Member
  • *****
  • Posts: 5740
  • Add to the wiki - it's free ;)
    • FPCUp, PaperTiger scanning and other open source projects
Re: Are there any friendly grid controls?
« Reply #9 on: September 21, 2012, 03:12:46 pm »
A dbgrid *is* a data-aware control.
A normal stringgrid isn't.

You could try something like (untested, don't even now if it compiles)
Code: [Select]
dbgrid1.datasource:=query
Otherwise, throw a datasource between the grid and the query and connect them up (see e.g. sqldb tutorial1 wiki page).
Want quicker answers to your questions? Read http://wiki.lazarus.freepascal.org/Lazarus_Faq#What_is_the_correct_way_to_ask_questions_in_the_forum.3F

Open source including papertiger OCR/PDF scanning:
https://bitbucket.org/reiniero

Lazarus trunk+FPC trunk x86, Windows x64 unless otherwise specified

Nebula

  • Jr. Member
  • **
  • Posts: 88
Re: Are there any friendly grid controls?
« Reply #10 on: September 21, 2012, 04:38:13 pm »
Thanks, I'm making progress with TStringGrid, bit of a tedious loop to copy in the query results but that can be farmed out to a subroutine  :D

Had a spot of crashing bother with RecordCount being always 10, but fixed that!
Code: [Select]
query.PacketRecords := -1;  // otherwise we only get .RecordCount = 10!

I've even got the alternating colour I wanted... yay!
Code: [Select]
stringgrid1.AlternateColor:= RGBToColor(248,248,255);
All I need now is to work out column formatting and auto column sizing and I'm v. happy!
« Last Edit: September 21, 2012, 04:40:22 pm by Nebula »
Newbie testing Lazarus v1.0 - very impressed
Win 7 at work, XP and Linux Mint at home.
It all started with a ZX80 on a b/w telly........
Code: [Select]
Uses Smiles, GoodHumour, WantsToHelp;
{ never liked C - curly brackets are for comments! }

garlar27

  • Hero Member
  • *****
  • Posts: 652
Re: Are there any friendly grid controls?
« Reply #11 on: September 21, 2012, 06:42:26 pm »
Thanks, I'm making progress with TStringGrid, bit of a tedious loop to copy in the query results but that can be farmed out to a subroutine  :D

Had a spot of crashing bother with RecordCount being always 10, but fixed that!
Code: [Select]
query.PacketRecords := -1;  // otherwise we only get .RecordCount = 10!

I've even got the alternating colour I wanted... yay!
Code: [Select]
stringgrid1.AlternateColor:= RGBToColor(248,248,255);
All I need now is to work out column formatting and auto column sizing and I'm v. happy!
>:D >:D >:D >:D >:D

You need a Form with a TDataSource and a TDBGrid in it

Code: [Select]
//set
DBGrid1.AlternateColor := clYellow; {you can do it from object inspector  ;) }
DBGrid1.DataSource := DataSource1; {you can do it from object inspector  ;) }
//set
DataSource1.DataSet := query; {you can do it from object inspector if you have a TDataSet descendant in your form  ;) }


and thats all!! 8-)

jamhitz

  • Jr. Member
  • **
  • Posts: 83
Re: Are there any friendly grid controls?
« Reply #12 on: September 21, 2012, 08:21:48 pm »
Code: [Select]
//set
DBGrid1.AlternateColor := clYellow; {you can do it from object inspector  ;) }
DBGrid1.DataSource := DataSource1; {you can do it from object inspector  ;) }
//set
DataSource1.DataSet := query; {you can do it from object inspector if you have a TDataSet descendant in your form  ;) }


and thats all!! 8-)

...in other words you can do all this without writing a single line of code. How about that!?

BigChimp

  • Hero Member
  • *****
  • Posts: 5740
  • Add to the wiki - it's free ;)
    • FPCUp, PaperTiger scanning and other open source projects
Re: Are there any friendly grid controls?
« Reply #13 on: September 21, 2012, 10:28:19 pm »
@jamhitz: yes, no idea why he felt he should do it the hard way...

Otherwise, throw a datasource between the grid and the query and connect them up (see e.g. sqldb tutorial1 wiki page).
Want quicker answers to your questions? Read http://wiki.lazarus.freepascal.org/Lazarus_Faq#What_is_the_correct_way_to_ask_questions_in_the_forum.3F

Open source including papertiger OCR/PDF scanning:
https://bitbucket.org/reiniero

Lazarus trunk+FPC trunk x86, Windows x64 unless otherwise specified

Nebula

  • Jr. Member
  • **
  • Posts: 88
Re: Are there any friendly grid controls?
« Reply #14 on: September 22, 2012, 12:15:32 pm »
>:D >:D >:D >:D >:D
You need a Form with a TDataSource and a TDBGrid in it

Whoah, easy Tiger!  :D

Some of us prefer the complete-control method of doing it by hand, knowing exactly what's going on, the 'decoupling' argument, etc - i.e. http://stackoverflow.com/questions/4722563/using-delphi-data-aware-components-pros-and-cons

It was never a Big Thing with VB6 developers, so if you want to attract hordes of MS defectors to Lazarus, go easy on us!
 
However, I've also just found this link in favour - http://edn.embarcadero.com/article/28156
.. so I'm going to sit down for a good read, take another look at the issue and all the pros and cons. All part of the ever-continuing learning process!  :)

Cheers

(btw Column width 'auto-sizing' turned out to be easy, but the numerical formatting and right alignment still looks like a nightmare of cell writing event hooks... yuk.
And then I need to look into Hidden Columns too, which can be useful.... any quick tips there gratefully received.
... all part of the Joys of Grids regardless of whether it's data-aware or not )
Newbie testing Lazarus v1.0 - very impressed
Win 7 at work, XP and Linux Mint at home.
It all started with a ZX80 on a b/w telly........
Code: [Select]
Uses Smiles, GoodHumour, WantsToHelp;
{ never liked C - curly brackets are for comments! }

 

TinyPortal © 2005-2018