maybe help you ... but exists many other way to do it...
implementation
{$R *.dfm}
type THackDBGrid = class(TDBGrid);
procedure TForm1.Button1Click(Sender: TObject); var bmCurrentRecord: TBookmark; iRecNo: Integer; iVisibleRows: Integer; iCurrentRow: Integer; iCharWidth: Integer; iWidthMax: Integer; i: Integer; begin // REMEMBER: // TableXXXfieldXXX.SIZE := xxx on Fields Editor can help you, too! // ex.: Table1CountryName.Size := 40;
{ // use direct!!! if DBGrid1.Columns[4].Visible then DBGrid1.Columns[4].Width := n; } // // Another way is use separated (another TQuery for example) "SQL expression" to find "what is the max value-string-on-records" //
// DBGrid1.DataSource.DataSet.RecNo := iRecNo; // go back to original record // DBGrid1.DataSource.DataSet.GotoBookmark(bmCurrentRecord);// on Table // DBGrid1.DataSource.DataSet.Bookmark := bmCurrentRecord; // on Table // Table1.EnableControls; end; end;
end.
----------------------------------------------
The higher the degree, the greater the respect given to the humblest!RAD 11.3
Another way but works for "Cell on DBGrid" not for "PickList at general... ----------
procedure TForm7.DBGridDrawColumnCell(Sender: TObject; const Rect: TRect; DataCol: Integer; Column: TColumn; State: TGridDrawState); Var w : Integer; begin // XXXXX is a arbitrary value for better look ( margin-left + text + margin-right of cell) // XXXXX 是一个更好看的任意值(margin-left + text + margin-right of cell) w := DBGrid.Canvas.TextExtent(Column.Field.DisplayText).cx + XXXXX; if w>column.Width then Column.Width := w; end;
procedure TForm7.FormActivate(Sender: TObject); // prefere use "OnCreate event" ... 首选使用“OnCreate 事件”... Var i : Integer; begin // Initialize width for I := 0 to DBGrid.Columns.Count - 1 do DBGrid.Columns[i].Width := 5 + DBGrid.Canvas.TextWidth(DBGrid.Columns[i].title.caption) end;
----------------------------------------------
The higher the degree, the greater the respect given to the humblest!RAD 11.3
You can use a "QUERY" to check what is the largest text that is stored in the desired field, and in this way know what value to calculate the width of the column "lookup" in the DBGrid. This can be done using a separate "QUERY" so as not to disrupt or degrade the performance of your data in the main table. Then you can close the "Query" and use the value in your code... Note: If you need to recalculate the width again, it's easy. Just run the "Query" again and get the value.
Example: //myQuery is a TQuery component or class; myQuery.SQL.Text := 'Select MAX(col_string_values) from tableX'; myQuery.Open; myColWidht := myQuery.Fields[0].asInteger; myQuery.Close;
DBGrid1.Columns[4].Width := (myColWidht * iCharWidth);
----------------------------------------------
The higher the degree, the greater the respect given to the humblest!RAD 11.3
procedure TForm1.Button1Click(Sender: TObject); var i: integer; iTextMoreLong: integer; iCharWidth: integer; begin // if the "PickList" on "Columns on DBGrid" it should be used, instead the text stored on fields-column // 如果应该使用“DBGrid 上的列”上的“PickList”,而不是存储在字段列上的文本
if (DBGrid1.Columns[2].PickList.Count > 0) then begin iTextMoreLong := Length(DBGrid1.Columns[2].PickList.Strings[0]); // for i := 0 to (DBGrid1.Columns[2].PickList.Count - 1) do begin if (iTextMoreLong < Length(DBGrid1.Columns[2].PickList.Strings[i])) then iTextMoreLong := Length(DBGrid1.Columns[2].PickList.Strings[i]); end; // iCharWidth := DBGrid1.Canvas.TextWidth('A'); DBGrid1.Columns[2].Width := (iCharWidth * iTextMoreLong); end;
end;
----------------------------------------------
The higher the degree, the greater the respect given to the humblest!RAD 11.3
Select LENGTH( TRIM(col_string_values) ) from tableX Order by 1
or
// Firebird db Select FIRST 1 LENGTH( TRIM(col_string_values) ) from tableX; Order by 1
or
Select FIRST 1 CHAR_LENGTH( TRIM(col_string_values) ) from tableX; Order by 1
----------------------------------------------
The higher the degree, the greater the respect given to the humblest!RAD 11.3