Recent

Author Topic: [Solved] Sort DBGrid column by integer values - not alpha  (Read 8156 times)

bobonwhidbey

  • Hero Member
  • *****
  • Posts: 630
    • Double Dummy Solver - free download
[Solved] Sort DBGrid column by integer values - not alpha
« on: July 06, 2016, 01:21:51 am »
This WIKI article helped a lot, trying to sort a DBGrid column with alpha values.
http://wiki.freepascal.org/Grids_Reference_Page#Sorting_columns_or_rows_in_DBGrid_with_sort_arrows_in_column_header

How can I sort a column correctly if it contains all integers. For my SQLite db I would normally use a Cast(fieldname as integer) in a query but I don't know how to do that within a HeaderClick event.
« Last Edit: July 12, 2016, 12:27:53 am by bobonwhidbey »
Lazarus 4.6 FPC 3.2.2 x86_64-win64-win32/win64

jesusr

  • Sr. Member
  • ****
  • Posts: 499
Re: Sort DBGrid column by integer values - not alpha
« Reply #1 on: July 08, 2016, 10:55:53 pm »
Integer, floats, alpha here it works. It seems you don't have to do anything special.

bobonwhidbey

  • Hero Member
  • *****
  • Posts: 630
    • Double Dummy Solver - free download
Re: Sort DBGrid column by integer values - not alpha
« Reply #2 on: July 10, 2016, 07:06:20 pm »
You're right. I had to learn how to typecast the SQL statements. For example, this works great:

Code: Pascal  [Select][+][-]
  1.   srt := 'Select * FROM MyFile CAST(' + Column.FieldName + ' AS float)' ;
  2.   TSQLQuery(DataSource.DataSet).SQL.Text := srt;
  3.   TSQLQuery(DataSource.DataSet).Open;

With a StringGrid I can use a DoCompareCells function for specialized sorting.Is there any way to do something similar with a DBGrid?

Lazarus 4.6 FPC 3.2.2 x86_64-win64-win32/win64

jesusr

  • Sr. Member
  • ****
  • Posts: 499
Re: Sort DBGrid column by integer values - not alpha
« Reply #3 on: July 11, 2016, 07:23:23 pm »
No, a TStringGrid has all the data always available and it owns it, a DbGrid does not own the data, it merely show it, the data comes from the dataset. So it's not possible to do this.

Ericktux

  • Sr. Member
  • ****
  • Posts: 394
    • ericksystem software
Re: [Solved] Sort DBGrid column by integer values - not alpha
« Reply #4 on: July 12, 2016, 07:02:09 am »
you tried with event "OntitleClick"
regards  :)
I love desktop software
https://www.ericksystem.com

bobonwhidbey

  • Hero Member
  • *****
  • Posts: 630
    • Double Dummy Solver - free download
Re: [Solved] Sort DBGrid column by integer values - not alpha
« Reply #5 on: July 12, 2016, 03:34:47 pm »
yes
Lazarus 4.6 FPC 3.2.2 x86_64-win64-win32/win64

rvk

  • Hero Member
  • *****
  • Posts: 6991
Re: [Solved] Sort DBGrid column by integer values - not alpha
« Reply #6 on: July 12, 2016, 04:14:34 pm »
TDBGrid gets its sorting from the connected TDataset and that one gets it from the connected database through the correct query and index-setting. You can't change it with a custom-client-side procedure.

If you have a limited number of records you could read all records in a memory-dataset and sort it from there.

Otherwise you need to find a way to sort it correctly by the database-server. There are a number of options depending on database-server used (for example "COLLATE NOCASE ASC" in SQLite).

What kind of custom sorting are you trying to do?

bobonwhidbey

  • Hero Member
  • *****
  • Posts: 630
    • Double Dummy Solver - free download
Re: [Solved] Sort DBGrid column by integer values - not alpha
« Reply #7 on: July 12, 2016, 06:32:49 pm »
The difficult sort is on a column of bridge bids; e.g.
3NT
1S
2CX
3C
3S

While the suits will sort correctly (clubs, diamonds, hearts, spades) "no trump" or "N" should sort after spades. Of course it doesn't alphabetically. The sloppy solution I've come up with is to have the database hold "T" rather than an "N" for no trump bids - and then make sure that the DrawCell displays "T" correctly as an "N".
Lazarus 4.6 FPC 3.2.2 x86_64-win64-win32/win64

rvk

  • Hero Member
  • *****
  • Posts: 6991
Re: [Solved] Sort DBGrid column by integer values - not alpha
« Reply #8 on: July 12, 2016, 07:03:26 pm »
The difficult sort is on a column of bridge bids; e.g.
3NT
1S
2CX
3C
3S

While the suits will sort correctly (clubs, diamonds, hearts, spades) "no trump" or "N" should sort after spades. Of course it doesn't alphabetically. The sloppy solution I've come up with is to have the database hold "T" rather than an "N" for no trump bids - and then make sure that the DrawCell displays "T" correctly as an "N".
(I take it the order you want is 1S, 2CX, 3C, 3S, 3NT)

Your solution is a valid one. But there is still a possibility to sort NT before S, H, D, C. Or C, D, H, S and NT if you prefer, low to high.

Code: SQL  [Select][+][-]
  1. SELECT *
  2. FROM BIDDINGS
  3. ORDER BY
  4.   SUBSTR(BID,1,1),
  5.   CASE SUBSTR(BID,2,1)
  6.     WHEN 'C' THEN 0
  7.     WHEN 'D' THEN 1
  8.     WHEN 'H' THEN 2
  9.     WHEN 'S' THEN 3
  10.     WHEN 'N' THEN 4
  11.     ELSE 5
  12.   END
I haven't tested this but it should sort by number of tricks (first character in BID) and inside that in order of C, D, H, S and NT.

bobonwhidbey

  • Hero Member
  • *****
  • Posts: 630
    • Double Dummy Solver - free download
Re: [Solved] Sort DBGrid column by integer values - not alpha
« Reply #9 on: July 13, 2016, 08:02:56 am »
WOW. That's really clever RVK. Yes - it works perfectly. Thank you. In case others would like to see the complete code, here's the important stuff :)

Code: Pascal  [Select][+][-]
  1. procedure TScoreBoard.GridTitleClick(Column: TColumn);
  2. var
  3.   srt, dir: string;
  4. begin
  5.   if Column.Index = 7 then
  6.     with Grid do begin
  7.       if FmtOpts.SortCol = Column.Index then
  8.         FmtOpts.SortUp := not FmtOpts.SortUp
  9.       else
  10.         FmtOpts.SortUp := True;
  11.       FmtOpts.SortCol := Column.Index;
  12.       if FmtOpts.Sortup then
  13.         dir := ''
  14.       else
  15.         dir := 'DESC';
  16.  
  17.       srt := 'SELECT * FROM ScoreBd ORDER BY SUBSTR(Result,1,1) '+dir+',' +
  18.         ' CASE SUBSTR(Result,2,1) WHEN ''C'' THEN 1 ' +
  19.         'WHEN ''D'' THEN 2 WHEN ''H'' THEN 3  WHEN ''S'' THEN 4 ' +
  20.         '  WHEN ''N'' THEN 5 ELSE 0 END '+dir;
  21.  
  22.       TSQLQuery(DataSource.DataSet).Close;
  23.       TSQLQuery(DataSource.DataSet).SQL.Text := srt;
  24.       TSQLQuery(DataSource.DataSet).Open;
  25.     end
  26.   else begin  
  27.  
  28. etc.
  29.  
« Last Edit: July 13, 2016, 08:09:43 am by bobonwhidbey »
Lazarus 4.6 FPC 3.2.2 x86_64-win64-win32/win64

 

TinyPortal © 2005-2018