Link to home
Start Free TrialLog in
Avatar of SCHUCla74
SCHUCla74

asked on

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.
Avatar of nestorua
nestorua

HI,
Use onTitleClick property.
Sincerely,
Nestorua.
I mean OnTitleClick event of TDBGrid.
Avatar of SCHUCla74

ASKER

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 ?
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".

I am using a query component and so I have no property IndexFieldName. Sorry.
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.
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...)
ASKER CERTIFIED SOLUTION
Avatar of nestorua
nestorua

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial