Bookstore

Recent

Author Topic: SOLVED: How to sort DESC in DBGrid?  (Read 900 times)

SaraT

  • Jr. Member
  • **
  • Posts: 81
  • A little student
SOLVED: How to sort DESC in DBGrid?
« on: January 30, 2020, 05:38:01 am »
Hi guys :)
To sort ASC in DBGrid, I use the code:
Code: Pascal  [Select]
  1. SQLQuery1.IndexFieldNames := Column.FieldName;

Results of the above code:
1
2
3
...

How can I sort DESC the column in DBGrid?
i.e:
...
3
2
1

« Last Edit: February 09, 2020, 03:37:07 am by SaraT »

mangakissa

  • Hero Member
  • *****
  • Posts: 981
Re: How to sort DESC in DBGrid?
« Reply #1 on: January 30, 2020, 08:43:55 am »
SQLQuery1.IndexFieldNames := Column.FieldName DESC
Lazarus 2.06 (64b) / FPC 3.0.4 / Windows 10
stucked on Delphi 10.3.1

SaraT

  • Jr. Member
  • **
  • Posts: 81
  • A little student
Re: How to sort DESC in DBGrid?
« Reply #2 on: January 30, 2020, 09:36:49 am »
SQLQuery1.IndexFieldNames := Column.FieldName DESC

Thanks but you know very well that code doesn't work.  :(

madref

  • Hero Member
  • *****
  • Posts: 772
  • ..... A day not Laughed is a day wasted !!
    • Nursing With Humour
Re: How to sort DESC in DBGrid?
« Reply #3 on: January 30, 2020, 11:14:51 am »
Make your query DESC with 'ORDER BY Your_Field DESC'.


then sql looks like this

Code: MySQL  [Select]
  1.   cSQL := 'SELECT Seizoen AS Sei_ID, tbl_Seizoenen.Sei_Periode, Sei_Letter FROM qry_Overzicht_Evaluaties ' +
  2.           'INNER JOIN tbl_Seizoenen ON tbl_Seizoenen.SEI_ID = qry_Overzicht_Evaluaties.Seizoen ' +
  3.           'GROUP BY Seizoen ' +
  4.           'UNION SELECT 0, "(All)" As Sei_Periode, "ZZ" ' +
  5.           'FROM tbl_Seizoenen ' +
  6.           'ORDER BY Sei_Letter DESC;';
You treat a disease, you win, you lose.
You treat a person and I guarantee you, you win, no matter the outcome.

Lazarus 2.0.6 / FPC 3.0.4
Lazarus Trunc / FPC Trunc
Mac OS X Mojave

johnmc

  • New Member
  • *
  • Posts: 31
Re: How to sort DESC in DBGrid?
« Reply #4 on: January 30, 2020, 11:42:18 am »
I use this form of query:
Code: Pascal  [Select]
  1. SQLQuery.Close;
  2.         SQLQuery.SQL.Text :=
  3.           ' SELECT * from wine_TBL ORDER BY WineName COLLATE NOCASE DESC ';
  4.         SQLQuery.Open;

mangakissa

  • Hero Member
  • *****
  • Posts: 981
Re: How to sort DESC in DBGrid?
« Reply #5 on: January 31, 2020, 09:17:57 am »
SQLQuery1.IndexFieldNames := Column.FieldName DESC

Thanks but you know very well that code doesn't work.  :(
In Delphi does. It should be compatible with lazarus  %)
Lazarus 2.06 (64b) / FPC 3.0.4 / Windows 10
stucked on Delphi 10.3.1

rvk

  • Hero Member
  • *****
  • Posts: 3943
Re: How to sort DESC in DBGrid?
« Reply #6 on: January 31, 2020, 12:36:21 pm »
SQLQuery1.IndexFieldNames := Column.FieldName DESC
Thanks but you know very well that code doesn't work.  :(
In Delphi does. It should be compatible with lazarus  %)
Without ; after the line?
DESC isn't really a reserved word so that doesn't work either.

Did you mean: ?
Code: Pascal  [Select]
  1. SQLQuery1.IndexFieldNames := Column.FieldName + ' DESC';

Of maybe you meant:
Code: Pascal  [Select]
  1. FDQuery1.IndexFieldNames := 'order_date;customer_name:D';
(like mentioned here http://docwiki.embarcadero.com/Libraries/Rio/en/FireDAC.Comp.Client.TFDMemTable.IndexFieldNames)
But that's not Lazarus compatible.
« Last Edit: January 31, 2020, 12:39:52 pm by rvk »