Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 335
  • Last Modified:

how to sort a dbgrid when clicking on the title?

I want to sort the columns of a dbgrid by clicking on the the column-title as in explorer. I know it is not hard to do, but I can't manage it. Thank you.
0
SCHUCla74
Asked:
SCHUCla74
  • 5
  • 3
1 Solution
 
nestoruaCommented:
HI,
Use onTitleClick property.
Sincerely,
Nestorua.
0
 
nestoruaCommented:
I mean OnTitleClick event of TDBGrid.
0
 
SCHUCla74Author Commented:
I knew already that. But must I then do a new sql-query with the order set to that field I clicked on ? Could you give me an code exeample ?
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
nestoruaCommented:
It depends on what Dataset you link your DBGrid.Datasource
with.
For example, if this is TTable and it has Index created on the corresponding Field you simple write:
"YourTableName.IndexName:=ThisFieldIndexName".
Or You can use TTable IndexFieldNames property to do sorting: "YourTableName.IndexFiledNames:=ThisFieldName".

0
 
SCHUCla74Author Commented:
I am using a query component and so I have no property IndexFieldName. Sorry.
0
 
nestoruaCommented:
Then I see two ways:
1.Put a TClientDataSet and TDatasetProvider components on your Form/DataModule and link DatasetProvider with your Query and ClientDataset with DataSetProvider and DataSource
which was linked with Query link with ClientDataSet.
Then you can use TClientDataSet.IndexFieldName property for your sorting and you need not to write additional sql-queries.
2.In OnTitleClick event of your DBGrid you rewrite your
query's SQL property indicating the following: order by
"the corresponding Field".
Choose yourself what you like to do.
0
 
SCHUCla74Author Commented:
I'm sorry but now I have the problem with the second solution to find the " corresponding " Field ? ( I also tried the first example, but I think the second will do the job, if...)
0
 
nestoruaCommented:
The corresponding Field you can find the following way:
s:=Column.Field.FieldName;//this is it
...
'ORDER BY '+s;//this must be added in SQL instead of
              //the previos one
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now