How to Sort Records in Delphi DBGrid

Delphi DBGrid … what a powerful component! If you are developing data aware applications you are probably using the DBGrid component every day. 

In this article you'll see how to add some more nitty gritty features to your database applications – your users will love it!

Following the concepts set in the Beginners Guide to Delphi Database Programming, examples below use ADO components (AdoQuery/AdoTable connected to ADOConnection, DBGrid connected to AdoQuery over DataSource) to display the records from a database table in a DBGrid component.


All the component names were left as Delphi named them when dropped on the form (DBGrid1, ADOQuery1, AdoTable1, …)


First, let's see how to change the mouse pointer while it moves over the DBGrid title area. All you have to do is to add the code to the OnMouseMove event for the DBGrid component. The code below simply uses the MouseCoord property of the DBGrid component to "calculate" where the mouse pointer is – if over DGBrid title area, the pt.y equals 0, which is the first row in the DBGrid – the title area (displaying column/field titles).

procedure TForm1.DBGrid1MouseMove
  (Sender: TObject; Shift: TShiftState; X, Y: Integer);
  pt: TGridcoord;
  pt:= DBGrid1.MouseCoord(x, y);

  if pt.y=0 then


The next piece of code is my favorite.

If you are using ADO approach to Delphi database development, and want to sort the records in the dataset, you need to set the Sort property of your AdoDataset (ADOQuery, AdoTable). The Sort property is the widestring value indicating the "ORDER BY" part of the standard SQL query – of course you do not need to write the SQL query to be able to use the Sort property.


You simply set the Sort property to the name of a single field or to a comma-separated list of fields, each following the sort order. Something like:

ADOTable1.Sort := 'Year DESC, ArticleDate ASC'

The OnTitleClick event of the DBGrid component has a Column parameter indicating the Column the user has clicked on. Now, each Column (object of type TColumn) has a Field property indicating the Field (TField) represented by the Column – and the Field in its FieldName property holds the name of the field in the underlying dataset. Therefore, to sort an ADO dataset by field/column a simple line can be used:

with TCustomADODataSet(DBGrid1.DataSource.DataSet) do
Sort := Column.Field.FieldName; // + ' ASC' or ' DESC'

Below you can find the code for the OnTitleClick even handler that sorts the records by column click. The code, as always, extends the idea.

First we want to, in some way, mark the column that is currently used for sort order. Next, if we click on a column title and the dataset is already sorted by that column we want to change the sort order from ASC (ascending) to DESC (descending) and vice versa. And finally, when we sort the dataset by another column we want to remove the mark from the previously selected column.


For the sake of simplicity, to mark the column that "sorts" the records we'll simply change the font style of the column title to Bold, and remove it when dataset is sorted using another column.

procedure TForm1.DBGrid1TitleClick(Column: TColumn);
 const PreviousColumnIndex : integer = -1;
  if DBGrid1.DataSource.DataSet is TCustomADODataSet then
  with TCustomADODataSet(DBGrid1.DataSource.DataSet) do
      DBGrid1.Columns[PreviousColumnIndex].title.Font.Style :=
      DBGrid1.Columns[PreviousColumnIndex].title.Font.Style - [fsBold];

    Column.title.Font.Style := 
    Column.title.Font.Style + [fsBold];
    PreviousColumnIndex := Column.Index;

    if (Pos(Column.Field.FieldName, Sort) = 1)
    and (Pos(' DESC', Sort)= 0) then
      Sort := Column.Field.FieldName + ' DESC'
      Sort := Column.Field.FieldName + ' ASC';

Note: the code above uses typed constants to preserve the value of the previously "selected" column for sort order.

That's it. Simple and powerful – this is why you have picked Delphi. 


  1. Nearly all of the things you articulate is astonishingly accurate and it makes me ponder the reason why I hadn’t looked at this with this light before. Your article really did turn the light on for me as far as this specific issue goes. Nonetheless at this time there is one position I am not really too cozy with and while I try to reconcile that with the actual main idea of your point, permit me see exactly what the rest of the readers have to say.Nicely done.

  2. I do not even understand how I stopped up here, however I assumed this submit was once great. I do not recognize who you might be however definitely you’re going to a famous blogger when you are not already 😉 Cheers!

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.