?
Solved

how to sort a dbgrid when clicking on the title?

Posted on 2003-03-29
8
Medium Priority
?
284 Views
Last Modified: 2010-04-04
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
Comment
Question by:SCHUCla74
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
8 Comments
 
LVL 4

Expert Comment

by:nestorua
ID: 8229883
HI,
Use onTitleClick property.
Sincerely,
Nestorua.
0
 
LVL 4

Expert Comment

by:nestorua
ID: 8229884
I mean OnTitleClick event of TDBGrid.
0
 

Author Comment

by:SCHUCla74
ID: 8229891
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!

 
LVL 4

Expert Comment

by:nestorua
ID: 8229913
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
 

Author Comment

by:SCHUCla74
ID: 8229964
I am using a query component and so I have no property IndexFieldName. Sorry.
0
 
LVL 4

Expert Comment

by:nestorua
ID: 8229983
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
 

Author Comment

by:SCHUCla74
ID: 8230352
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
 
LVL 4

Accepted Solution

by:
nestorua earned 80 total points
ID: 8230489
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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses
Course of the Month14 days, 3 hours left to enroll

801 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question