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?
here's a way to control what is the leftmost column: