DELPHI盒子
!实时搜索: 盒子论坛 | 注册用户 | 修改信息 | 退出
检举帖 | 全文检索 | 关闭广告 | 捐赠
技术论坛
 用户名
 密  码
自动登陆(30天有效)
忘了密码
≡技术区≡
DELPHI技术
lazarus/fpc/Free Pascal
移动应用开发
Web应用开发
数据库专区
报表专区
网络通讯
开源项目
论坛精华贴
≡发布区≡
发布代码
发布控件
文档资料
经典工具
≡事务区≡
网站意见
盒子之家
招聘应聘
信息交换
论坛信息
最新加入: laidabin
今日帖子: 1
在线用户: 4
导航: 论坛 -> DELPHI技术 斑竹:liumazi,sephil  
作者:
男 yuanlai (lai) ▲▲△△△ -
普通会员
2021/6/11 15:16:46
标题:
DBGrid->Columns->PickList->下拉視窗寬度如何改變 浏览:1230
加入我的收藏
楼主: 各位先進 大家好:
如題 求教
此帖子包含附件:
JPEG 图像
大小:23.1K
----------------------------------------------
-
作者:
男 emailx45 (emailx45) ▲▲▲▲△ -
普通会员
2021/6/12 2:36:55
1楼: 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"
  //

  // try calculate using "visible rows on DBGrid"
  Table1.DisableControls;
  //
  iWidthMax := Length(DBGrid1.Columns[4].Field.AsString);
  iCharWidth := DBGrid1.Canvas.TextWidth('W');
  //
  // bmCurrentRecord := DBGrid1.DataSource.DataSet.Bookmark; // GetBookmark;
  iRecNo := DBGrid1.DataSource.DataSet.RecNo;
  try
    iCurrentRow := THackDBGrid(DBGrid1).Row;
    iVisibleRows := THackDBGrid(DBGrid1).VisibleRowCount;

    // showMessage(THackDBGrid(DBGrid1).Row.ToString +' - ' + iVisibleRows.toString);
    for i := 1 to (iVisibleRows) do
    begin
      // Memo1.Lines.Add(Format('RecNo=%d, Value=%s', [ { }
      // DBGrid1.DataSource.DataSet.RecNo, { }
      // DBGrid1.Columns[4].Field.Value { }
      // ]) { }
      // );

      if (iWidthMax < Length(DBGrid1.Columns[4].Field.AsString)) then
        iWidthMax := Length(DBGrid1.Columns[4].Field.AsString);
      //
      DBGrid1.DataSource.DataSet.Next;
    end;
  finally
    //
    // THackDBGrid(DBGrid1).Row := iCurrentRow; // on DBGrid
    //
    Caption := Format('iWidthMax=%d, iCharWidth=%d = %d',
      [iWidthMax, iCharWidth, (iWidthMax * iCharWidth)]);
    //
    DBGrid1.Columns[4].Width := (iWidthMax * iCharWidth);

    //
    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
作者:
男 emailx45 (emailx45) ▲▲▲▲△ -
普通会员
2021/6/12 6:27:34
2楼: 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
作者:
男 emailx45 (emailx45) ▲▲▲▲△ -
普通会员
2021/6/12 6:32:55
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;

----------

您可以使用“查询”来检查存储在所需字段中的最大文本是什么,并以这种方式知道计算 DBGrid 中“查找”列的宽度的值。
这可以使用单独的“查询”来完成,以免破坏或降低主表中数据的性能。 然后您可以关闭“查询”并在您的代码中使用该值...
注意:如果您需要再次重新计算宽度,这很容易。 只需再次运行“查询”并获取值。

例子:
//myQuery 是一个 TQuery 组件或类;
myQuery.SQL.Text := 'Select MAX(col_string_values) from tableX';
myQuery.Open;
myColWidht := myQuery.Fields[0].asInteger;
myQuery.Close;

----------
iCharWidth := DBGrid1.Canvas.TextWidth('A');

DBGrid1.Columns[4].Width := (myColWidht * iCharWidth);
----------------------------------------------
The higher the degree, the greater the respect given to the humblest!RAD 11.3
作者:
男 emailx45 (emailx45) ▲▲▲▲△ -
普通会员
2021/6/12 6:53:58
4楼: 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
作者:
男 emailx45 (emailx45) ▲▲▲▲△ -
普通会员
2021/6/12 7:45:48
5楼: NOTE:
Select MAX(col_string_values) from tableX

it cannot works... try this:

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
作者:
男 yuanlai (lai) ▲▲△△△ -
普通会员
2021/6/12 10:06:35
6楼: 感謝 emailx45 先進 回覆!!
----------------------------------------------
-
作者:
男 zyp1984 (小李他妈的飞刀) ★☆☆☆☆ -
普通会员
2021/6/12 14:09:36
7楼: dropdown form可以任意改变大小。
----------------------------------------------
山外青山楼外楼,能人背后有能人弄..
作者:
男 yuanlai (lai) ▲▲△△△ -
普通会员
2021/6/12 14:17:26
8楼: 感謝emailx45先進,仔細拜讀程式碼後,感覺會是依據Packlist的內容,動調整 Columns
的寬度,但是我並不想改變 Columns 的寬度,只想改變下拉視窗的寬度。
----------------------------------------------
-
作者:
男 yuanlai (lai) ▲▲△△△ -
普通会员
2021/6/12 14:26:41
9楼: 感謝 zyp1984 先進 回覆!!
dropdown form ,不解其意為何?
----------------------------------------------
-
作者:
男 zyp1984 (小李他妈的飞刀) ★☆☆☆☆ -
普通会员
2021/6/13 9:15:38
10楼: dbgrideh有一个新功能就是dropdownform,可以给单元格设计下拉窗口。下拉窗口的内容你可以任意设计,其实就是下拉了一个你设计好的form.很棒,在demo中看下,很好用。
----------------------------------------------
山外青山楼外楼,能人背后有能人弄..
作者:
男 zyp1984 (小李他妈的飞刀) ★☆☆☆☆ -
普通会员
2021/6/13 9:19:33
11楼: demo中的
此帖子包含附件:
JPEG 图像
大小:256.9K
----------------------------------------------
山外青山楼外楼,能人背后有能人弄..
作者:
男 yuanlai (lai) ▲▲△△△ -
普通会员
2021/6/13 16:07:15
12楼: 感謝 zyp1984 先進 回覆!!
第三方元件,得再研究研究。
----------------------------------------------
-
作者:
男 ksrsoft (cb168) ★☆☆☆☆ -
普通会员
2021/6/13 16:26:18
13楼: ehlib的dropdown form 好用
----------------------------------------------
-
作者:
男 czlixu (czlixu) ★☆☆☆☆ -
普通会员
2021/6/13 18:28:21
14楼:  @zyp1984 @ksrsoft
请问,ehlib的dropdown form 可以直接用Button调用?
----------------------------------------------
-
作者:
男 zyp1984 (小李他妈的飞刀) ★☆☆☆☆ -
普通会员
2021/6/14 10:03:48
15楼: 应该可以。
----------------------------------------------
山外青山楼外楼,能人背后有能人弄..
信息
登陆以后才能回复
Copyright © 2CCC.Com 盒子论坛 v3.0.1 版权所有 页面执行95.70313毫秒 RSS