DBGrid Column自动调整宽度,加行号,以及Override DrawColumnCell
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
{
--Author Ming
--Version V01.001
--Creation Date 2009/03/20
--Last Updated Date 2009/03/20
--Last Updated Date 2009/04/02 Add procedure AutoAdjustColumnWidth
}
unit unitDBGrid;
interface
uses Windows, Messages, Forms, Sysutils, Db, Math, Grids, Dbgrids, Controls,
DBClient, Graphics;
type
Publicity = class
private
public
end;
function DBGridRecordSize(aColumn: TColumn): Boolean;
function DBGridAutoSize1(aColumn: TDBGrid; aOffset: Integer = 15): Boolean;
procedure DBGriodColumnOrderBy(aClientDataSet: TClientDataSet; Column: TColumn; OrderFlag: Boolean);
procedure PublicDrawColumnCell(Sender: TObject; const Rect: TRect; DataCol: Integer; Column: TColumn;State: TGridDrawState);
procedure OnMouseWheel(aDBGrid: TDBGrid; var Msg: TMsg; var Handled: Boolean);
procedure AutoAdjustColumnWidth(aDBGrid: TDBGrid);
implementation
function DBGridRecordSize(aColumn: TColumn): Boolean;
begin
Result := False;
if not Assigned(aColumn.Field) then
Exit;
aColumn.Field.Tag := Max(aColumn.Field.Tag,
TDBGrid(aColumn.Grid).Canvas.TextWidth(aColumn.Field.DisplayText));
Result := True;
end;
function DBGridAutoSize1(aColumn: TDBGrid; aOffset: Integer = 15): Boolean;
var
I: Integer;
begin
Result := False;
if not Assigned(aColumn) then
Exit;
if not Assigned(aColumn.DataSource) then
Exit;
if not Assigned(aColumn.DataSource.DataSet) then
Exit;
if not aColumn.DataSource.DataSet.Active then
Exit;
for I := 0 to aColumn.Columns.Count - 1 do
begin
if not aColumn.Columns[I].Visible then
Continue;
DBGridRecordSize(aColumn.Columns[i]);
if Assigned(aColumn.Columns[I].Field) then
aColumn.Columns[I].Width := Max(aColumn.Columns[I].Field.Tag,
aColumn.Canvas.TextWidth(aColumn.Columns[I].Title.Caption)) + aOffset
else
aColumn.Columns[I].Width :=
aColumn.Canvas.TextWidth(aColumn.Columns[I].Title.Caption) + aOffset;
aColumn.Refresh;
end;
Result := True;
end;
procedure AutoAdjustColumnWidth(aDBGrid: TDBGrid);
var
i, iWidth: Integer;
aiWidth: array of Integer;
bm: TBookMark;
begin
with aDBGrid do
begin
SetLength(aiWidth, Columns.Count);
Screen.Cursor := crHourGlass;
for i := Low(aiWidth) to High(aiWidth) do
aiWidth[i] := 0;
if DataSource <> nil then
if DataSource.DataSet <> nil then
if DataSource.DataSet.Active then
begin
bm := DataSource.DataSet.GetBookmark;
DataSource.DataSet.DisableControls;
DataSource.DataSet.First;
while not DataSource.DataSet.Eof do
begin
for i := Low(aiWidth) to High(aiWidth) do
begin
iWidth := Canvas.TextWidth(Fields[i].DisplayText);
if iWidth > aiWidth[i] then
aiWidth[i] := iWidth;
end;
DataSource.DataSet.Next;
end;
DataSource.DataSet.GotoBookmark(bm);
DataSource.DataSet.EnableControls;
DataSource.DataSet.FreeBookmark(bm);
end;
for i := Low(aiWidth) to High(aiWidth) do
begin
iWidth := Canvas.TextWidth(Columns.Items[i].Title.Caption);
if iWidth > aiWidth[i] then
aiWidth[i] := iWidth;
Columns.Items[i].Width := aiWidth[i] + 6;
end;
Screen.Cursor := crDefault;
end;
end;
procedure OnMouseWheel(aDBGrid: TDBGrid; var Msg: TMsg; var Handled: Boolean);
begin
if Msg.message = WM_MouseWheel then
begin
if Msg.wParam > 0 then
begin
if aDBGrid.Focused then
SendMessage(aDBGrid.Handle, WM_VSCROLL, SB_PAGEUP, 0);
end
else
begin
if aDBGrid.Focused then
SendMessage(aDBGrid.Handle, WM_VSCROLL, SB_PAGEDOWN, 0);
end;
Handled := True;
end;
end;
procedure DBGriodColumnOrderBy(aClientDataSet: TClientDataSet; Column: TColumn;
OrderFlag: Boolean);
procedure OrderByAsc(pFieldName: string);
begin
with aClientDataSet do
begin
IndexDefs.Clear;
AddIndex(pFieldName + 'IdxAsc', pFieldName, [ixPrimary], '', '', 0);
IndexName := pFieldName + 'IdxAsc';
IndexDefs.Update;
First;
end;
end;
procedure OrderByDesc(pFieldName: string);
begin
with aClientDataSet do
begin
IndexDefs.Clear;
AddIndex(pFieldName + 'IdxDesc', pFieldName, [ixDescending], '', '', 0);
IndexName := pFieldName + 'IdxDesc';
IndexDefs.Update;
First;
end;
end;
begin
if OrderFlag then
OrderByAsc(Column.FieldName)
else
OrderByDesc(Column.FieldName);
end;
procedure PublicDrawColumnCell(Sender: TObject;
const Rect: TRect; DataCol: Integer; Column: TColumn;State: TGridDrawState);
begin
if Column.FieldName = 'SalesStatus' then
begin
if Column.Field.AsString = 'K_RELEASED' then
begin
(Sender as TDBGrid).Canvas.brush.Color := clMoneyGreen;
(Sender as TDBGrid).DefaultDrawDataCell(Rect, Column.Field, State);
end
else if Column.Field.AsString = 'K_RECEIVED' then
begin
(Sender as TDBGrid).Canvas.brush.Color := clTeal;
(Sender as TDBGrid).DefaultDrawDataCell(Rect, Column.Field, State);
end
else
begin
(Sender as TDBGrid).Canvas.brush.Color := clBackGround;
(Sender as TDBGrid).DefaultDrawDataCell(Rect, Column.Field, State);
end
end;
if (Sender as TDBGrid).DataSource.DataSet.RecNo > 0 then
begin
//if Uppercase(Column.FieldName) = 'NO' then
if Column.Index = 0 then
(Sender as TDBGrid).Canvas.TextOut(Rect.Left + 2, Rect.Top, IntToStr(((Sender as TDBGrid).DataSource.DataSet.RecNo)));
end;
end;
end.