anchor.diff:    tmpSortConditions : TSortByConditions;
     i, idx : integer;
   begin
  -  tmpSortType := stAscending;
  -
  -  // remove every arrow from column captions
  -  for i := 0 to FDBGrid.Columns.Count - 1 do
  -    FDBGrid.Columns[i].Title.ImageIndex := -1;
  -
  -  // analize current filter
  -  if (FSortManager.GetSorted) and (FSortManager.GetSortByConditions.Count > 0) and (FSortManager.GetSortByConditions.Items[0].FieldName = Column.FieldName) then
  +  try
  +    tmpSortType := stAscending;
  +
  +    // remove every arrow from column captions
  +    for i := 0 to FDBGrid.Columns.Count - 1 do
  +      FDBGrid.Columns[i].Title.ImageIndex := -1;
  +
  +    // analize current filter
  +    if (FSortManager.GetSorted) and (FSortManager.GetSortByConditions.Count > 0) and (FSortManager.GetSortByConditions.Items[0].FieldName = Column.FieldName) then
  +    begin
  +      if FSortManager.GetSortByConditions.Items[0].SortType = stAscending then
  +        tmpSortType:= stDescending
  +      else
  +      begin
  +        FSortManager.ClearSort;
  +        exit;
  +      end
  +    end;
  +
  +    // set new sort condition
  +    FSortManager.GetSortByConditions.Clear;
  +    with FSortManager.GetSortByConditions.Add do
  +    begin
  +      FieldName:= Column.FieldName;
  +      SortType:= tmpSortType;
  +    end;
  +
  +    // do sort
  +    if FSortManager.Sort then
  +    begin
  +      if tmpSortType = stAscending then
  +        idx := 0
  +      else
  +        idx := 1;
  +      FDBGrid.Columns[Column.Index].Title.ImageIndex := idx;
  +    end;
  +  finally
  +    if Assigned(FOriginalOnTitleClick) then
  +      FOriginalOnTitleClick(Column);
  +  end;
  +end;
  +
  +procedure TmDBGridHelper.OnMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
  +var
  +  tmpCol, tmpRow : longint;
  +begin
  +  // https://www.codeproject.com/Articles/199506/Improving-Delphi-TDBGrid
  +  if Button = mbRight then
     begin
  -    if FSortManager.GetSortByConditions.Items[0].SortType = stAscending then
  -      tmpSortType:= stDescending
  +    if Y < FDBGrid.DefaultRowHeight then
  +    begin
  +       FDBGrid.PopupMenu := FHeaderPopupMenu;
  +       FDBGrid.MouseToCell(X, Y, tmpCol, tmpRow);
  +       FCurrentGridCol := tmpCol;
  +    end
       else
  -    begin
  -      FSortManager.ClearSort;
  -      exit;
  -    end
  -  end;
  -
  -  // set new sort condition
  -  FSortManager.GetSortByConditions.Clear;
  -  with FSortManager.GetSortByConditions.Add do
  +       FDBGrid.PopupMenu := FGridPopupMenu;
  +  end;
  +
  +  if Assigned(FOriginalOnMouseDown) then
  +    FOriginalOnMouseDown(Sender, Button, Shift, X, Y);
  +end;
  +
  +procedure TmDBGridHelper.BuildHeaderPopupMenu;
  +var
  +  tmpMenuItem : TMenuItem;
  +begin
  +  if not Assigned(FHeaderPopupMenu) then
     begin
  -    FieldName:= Column.FieldName;
  -    SortType:= tmpSortType;
  -  end;
  -
  -  // do sort
  -  if FSortManager.Sort then
  -  begin
  -    if tmpSortType = stAscending then
  -      idx := 0
  -    else
  -      idx := 1;
  -    FDBGrid.Columns[Column.Index].Title.ImageIndex := idx;
  -  end;
  -end;
  -
  -constructor TmDBGridHelper.Create;
  +    FHeaderPopupMenu:= TPopupMenu.Create(FDBGrid);
  +    tmpMenuItem := TMenuItem.Create(FHeaderPopupMenu);
  +    tmpMenuItem.Caption:= 'Filter values..';
  +    FHeaderPopupMenu.Items.Add(tmpMenuItem);
  +    tmpMenuItem := TMenuItem.Create(FHeaderPopupMenu);
  +    tmpMenuItem.Caption:= 'Add summary..';
  +    FHeaderPopupMenu.Items.Add(tmpMenuItem);
  +  end;
  +end;
  +
  +constructor TmDBGridHelper.Create(aGrid : TDBGrid);
   begin
     FSettings := TmGridColumnsSettings.Create;
     FSortManager := nil;
  +  FDBGrid := aGrid;
   end;
   
   destructor TmDBGridHelper.Destroy;
  
positive.code: 
  uses
    Classes,
    DBGrids, Controls,
    mGridColumnSettings, mXML, mGridSettingsForm, mSortConditions, mGridIcons,
    mDatasetInterfaces;
  
  
negative.code:   begin
      FGridIcons:= TmGridIconsDataModule.Create(FDBGrid);
      FDBGrid.TitleImageList := FGridIcons.GridImageList;
      FOriginalOnTitleClick := FDBGrid.OnTitleClick;
      FDBGrid.OnTitleClick:= Self.OnTitleClick;
      FSortManager:= aSortManager;
    end;
  end;
  
  procedure TmDBGridHelper.EnableHeaderPopupMenu(aGridPopupMenu : TPopupMenu);
  begin
    if not Assigned(FHeaderPopupMenu) then
      Self.BuildHeaderPopupMenu;
    FGridPopupMenu := aGridPopupMenu;
    FOriginalOnMouseDown := FDBGrid.OnMouseDown;
    FDBGrid.OnMouseDown:= Self.OnMouseDown;
  end;
  
  end.
  