Continuing with the project started in:
How to auto fit/scale DBGrid's (or other similar) columns widths according to its contents?
I used the @alzaimar answer to auto fit the columns according to their content width, but he showed me how to increase the width, but not how to decrease, so I complemented the code as shown above:
procedure TRecordsBrowserFrameBase.JvDBGrid2DrawColumnCell(Sender: TObject; const Rect: TRect; DataCol: Integer; Column: TColumn; State: TGridDrawState);
var
h, p, g, r, w, t : Integer;
const
colSpc = 10;
begin
{ ajusta as colunas de uma grade de acordo com o conteúdo das células }
h := Column.DropDownRows;
p := (Sender as TJvDBGrid).CurrentDrawRow;
g := Column.Width;
r := (Sender as TJvDBGrid).VisibleRowCount;
w := colSpc + (Sender as TJvDBGrid).Canvas.TextExtent(Column.Field.DisplayText).cx;
t := colSpc + (Sender as TJvDBGrid).Canvas.TextExtent(Column.Title.Caption).cx;
{$WARNINGS OFF}
// increase column width if needed
if (w > g) then Column.Width := w;
if (g < t) then Column.Width := t;
if (p < r) and (h < w) then Column.DropDownRows := w;
// decrease column size if needed (10th line)
if (p = r) then
begin
h := Column.DropDownRows;
g := Column.Width;
if (h > t) and (h < g) then Column.Width := Column.DropDownRows;
Column.DropDownRows := 0;
end;
{$WARNINGS ON}
end;
So, now when I scroll with the down arrow key, the selected row color does not always is in the correct position as you can see in this picture:
What I am doing wrong?