Restore horizontal scroll position in TDBGrid

5.4k views Asked by At

I wrote a simple method to sort column in TDBGrid. If Option.RowSelect set to False everything works fine, but if RowSelect gets True the horizontal position scroll doesn't restore after sort column. So I try GetScrollPos and SetScrollPos to restore horizontal Scroll position, the ScrollBar goes to the right position but TDBGrid didn't scroll, here is the method:

procedure TDBGrid.TitleClick(Column: TColumn);
var
  CurrenctPosition: TBookmark;
  PosScroll: Integer;
begin
  inherited TitleClick(Column);
  if FAllowTitleClick and (Assigned(DataSource))
  and (Assigned(DataSource.DataSet))
  and (DataSource.DataSet.Active)
  and (Assigned(Column.Field))
  and (Column.Field.FieldKind <> fkLookup) then
  begin
    //Get position scroll
    PosScroll := GetScrollPos(Handle, SB_HORZ);
    CurrenctPosition := DataSource.DataSet.GetBookmark;
    FPaintInfo.ColPressed := False;
    FPaintInfo.ColPressedIdx := -1;
    if ValidCell(FCell) then
      InvalidateCell(FCell.X, FCell.Y);
    SortColumn(Column);
    DataSource.DataSet.GotoBookmark(CurrenctPosition);
    //Set position scroll
    SetScrollPos(Handle, SB_HORZ, PosScroll, True);//<- need to be refreshed
  end;
end;

This can maybe fixed using Perform(WM_HSCROLL, SB_LINERIGHT, 0) in loop but isn't good idea. Anybody have better solution?

2

There are 2 answers

1
X-Ray On BEST ANSWER

here's a way to control what is the leftmost column:

type
  TGridFriend=class(TDBGrid);


procedure TForm1.Button2Click(Sender: TObject);
begin
  // scroll to right by one column
  TGridFriend(DBGrid1).leftCol:=TGridFriend(DBGrid1).leftCol + 1;
end;
0
Domus On

You might find an answer here:

http://www.species.net/Aves/Cassowary/delphi.htm

Look for "SetScrollPos" in the text.

Maybe ModifyScrollBar(Code, SB_THUMBPOSITION, Value) holds the solution.