Sorting records in Delphi DBGrid by Clicking on Column Title

Just put this code on “OnTitleClick” event of your DBGrid

procedure TFrm_Main.DBGrid_TrcrosTitleClick(Column: TColumn);
begin
inherited;

if (Column.Field.Calculated = False ) and (Column.Field.Lookup = False ) then
   TClientDataSet(Column.Grid.DataSource.DataSet).Ind  exFieldNames := Column.FieldName
   else if (Column.Field.Lookup = True ) then
      TClientDataSet(Column.Grid.DataSource.DataSet).Ind  exFieldNames := Column.Field.LookupKeyFields;
end;