First, in the OnCreate event for the Form object containing the DBGrid, specify what columns need to be auto-resized by assigning a non-zero value for the Tag property of the corresponding TField object.
procedure TForm1.FormCreate(Sender: TObject);
begin
//setup autoresizable columns by asigning
//Minimm Width in the Tag property.
//using fixed value: 40 px
Table1.FieldByName('FirstName').Tag := 40;
//using variable value: width of the
//default Column title text
Table1.FieldByName('LastName').Tag :=
4 + Canvas.TextWidth(
Table1.FieldByName('LastName').DisplayName);
end;
In the above code, Table1 is a TTable component linked to a DataSource component which is linked with the DBGrid. The Table1.Table property points to the DBDemos Employee table.
We have marked the columns displaying the values for FirstName and LastName fields to be auto-resizable.