dbgrideh自适应列宽
DBGridEH自适应宽度
1.设AutoFitColWidths=true;它的列宽会跟据整个Grid的宽度自动调整且每一列(TColumnEh)都有AutoFitColWidth属性,设为true,该列宽度自动调整,经测试,这种自适应不能满足要求。
2.OptionsEh中有dghDblClickOptimizeColWidth,当双击表缝时,前一列按当前数据宽度自动调整宽度,这种方式基本上能满足要求,但很不方便。
3.
- dbgrdh1.AutoFitColWidths:=True;
- for ii:=0 to dbgrdh1.Columns.Count-1 do
- with dbgrdh1.Columns[ii] do
- OptimizeWidth;
4.另外写过程
- function DBGridRecordSize(mColumn: TColumnEh): Boolean;
- { 返回记录数据网格列显示最大宽度是否成功 }
- begin
- Result := False;
- if not Assigned(mColumn.Field) then Exit;
- mColumn.Field.Tag := Max(mColumn.Field.Tag,
- TDBGridEh(mColumn.Grid).Canvas.TextWidth(mColumn.Field.DisplayText));
- Result := True;
- end; { DBGridRecordSize }
- function DBGridAutoSize(mDBGrid: TDBGridEh; mOffset: Integer =20): Boolean;
- { 返回数据网格自动适应宽度是否成功 }
- var
- I: Integer;
- begin
- Result := False;
- if not Assigned(mDBGrid) then Exit;
- if not Assigned(mDBGrid.DataSource) then Exit;
- if not Assigned(mDBGrid.DataSource.DataSet) then Exit;
- if not mDBGrid.DataSource.DataSet.Active then Exit;
- for I := 0 to mDBGrid.Columns.Count - 1 do begin
- if not mDBGrid.Columns[i].Visible then Continue;
- if Assigned(mDBGrid.Columns[i].Field) then
- mDBGrid.Columns[i].Width := Max(mDBGrid.Columns[i].Field.Tag,
- mDBGrid.Canvas.TextWidth(mDBGrid.Columns[i].Title.Caption)) + mOffset
- else mDBGrid.Columns[i].Width :=
- mDBGrid.Canvas.TextWidth(mDBGrid.Columns[i].Title.Caption)+ mOffset;
- // ShowMsg(IntToStr(mDBGrid.Canvas.TextWidth(mDBGrid.Columns[i].Title.Caption))+'_'+IntToStr( mDBGrid.Columns[i].Width)+mDBGrid.Columns[i].Title.Caption);
- mDBGrid.Refresh;
- end;
- Result := True;
- end; { DBGridAutoSize }
在DrawColumnCell过程中添加
DBGridRecordSize(Column) ;
在button的click事件中
BGridAutoSize(dbgrdh1);即可
记录Coding学习过程中的点点滴滴,记性不好,特别需要这么一个博客 逸'Coding'Blog