在TDBGrid中恢复水平滚动位置

时间:2009-11-28 11:23:58

标签: delphi tdbgrid

我写了一个简单的方法来对TDBGrid中的列进行排序。 如果Option.RowSelect设置为False,一切正常,但如果RowSelect为True,则水平位置滚动在排序列后不会恢复。 所以我尝试使用GetScrollPos和SetScrollPos恢复水平滚动位置,ScrollBar转到正确的位置但是TDBGrid没有滚动,这里是方法:

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;

这可以使用循环中的Perform(WM_HSCROLL,SB_LINERIGHT,0)来修复但不是一个好主意。 有人有更好的解决方案吗?

2 个答案:

答案 0 :(得分:3)

这是一种控制最左边列的方法:

type
  TGridFriend=class(TDBGrid);


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

答案 1 :(得分:0)

你可以在这里找到答案:

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

在文本中查找“SetScrollPos”。

也许ModifyScrollBar(Code,SB_THUMBPOSITION,Value)拥有解决方案。

相关问题